|
|
Example :
As per the First Example, make a new java class , and name it dataType.java
The learnings in Java of how to make a class from apache can be found here.
Next , after creating the class as specified , type the following code :
Incorrect use of data type and the error with it :
Make a new Java class and name it errorDataType.java Then type the following
code:
package iknowjava;
/** * @author Deepak * */
public class errorDataType
{
/** * @param args */
public static void main(String[] args)
{
int errorWithVariable;
System.out.println("Will it compile" + errorWithVariable);
}
}
On Running the above program , an error is thrown forward as :
"Exception in thread "main" java.lang.Error: Unresolved compilation problem: The
local variable errorWithVariable may not have been initialized at
iknowjava.errorDataType.main(errorDataType.java:18) "
The Same can be rectified easily with a small change :
// Start the code
package iknowjava;
/** * @author Deepak * */
public class errorDataType
{
/** * @param args */
public static void main(String[] args)
{
int errorWithVariable =1 ;
System.out.println("Will it compile" + errorWithVariable);
}
}
// End of code
Run this Java Program now:
The Java program gives the result : Will it compile1
Hence uninitialized primitive data type variables throws an error. But not
always.
If the variable is declared as static , then also the above exception will not
be thrown. |
|