In my opinion, the default value for the int variable is 0. However, in my this demo, the variable num3
does not have the initialized value.
class Solution
{
static int num1;
int num2;
public static void main(String[] args){
int num3;
int[] nums = new int[5];
System.out.println("num1: " + num1);
Solution sol = new Solution();
System.out.println("num2: " + sol.num2);
System.out.println("num3: " + num3);
System.out.print("nums:");
for(int item : nums)
System.out.print(" " + item);
System.out.println("");
}
}
I ran my demo on Ubuntu16.04. The error I got is as follows:
Solution.java:12: error: variable num3 might not have been initialized
System.out.println(num3);
^
1 error
If I remove this line System.out.println("num3: " + num3);
, the results I have are as follows:
num1: 0
num2: 0
nums: 0 0 0 0 0
If anyone knows why, please let me know. Thanks.