1.

In Java, what is the default value of the local variables?

Answer»

Local variables, PRIMITIVES, and object references are not INITIALISED to any default value in Java. A code snippet to illustrate the same is GIVEN below:

public class LocalVariablesExample{ public void foo() { int localVariable; localVariable = localVariable * 10; System.out.println("Local Variable value is : " + localVariable); } public STATIC void main(String args[]) { LocalVariablesExample obj = new LocalVariablesExample(); obj.foo(); }}

The following error is generated while compiling this code because the local variable in the FUNCTION foo is not initialised:

LocalVariablesExample.java:4:variable number might not have been initializedlocalVariable = localVariable * 10; ^1 error


Discussion

No Comment Found