What is the difference between
public static void main(String [] ar){
int var= 10;
System.out.println(var);
}
and
public static void main(String [] ar){
int var;
var= 10;
System.out.println(var);
}
moreover, what it reflects in Compiler/JVM?
There is no real difference, except the number of lines you used. It is a matter of style and for such simple case I would use the first example.
If the code were optimized to native code, the variable var
might disappear entirely.
int var; //reserves the memory location for an int data type named var
var = 10 //assigns the integer 10 to the above location
You can do these separately or in one step as you demonstrated. There is no difference to the compiler.
Your syntax in the first example does both steps at the same time, whereas your second example separates the steps.
There is no at all difference between in both of the snippets mentioned other than number of lines taken to do declaration & initialization.
The compiler will treat both of the things in the same fashion.
Since initialization is been done before using the local variable in both the cases, there are no chances of error like 'Variable Not Initialized' or so.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.