
There is a simple process to get the default value of data type. Declare data type without any value, try to access it and you will get your answer.
Step-1: Declare variables
Declare variables without any value and try to print the value of that variable.
public class Sample3 {
public static void main(String[] args) {
int intVar;
String strVar;
System.out.println(intVar);
System.out.println(strVar);
}
}
Step-2: Default value of data type
When we try to access or print the value for the variables which are not initialized, we got a compile-time error for these variables.
Observe that tiny red lines for the variables (intVar and strVar) under println() statements. Mouse hover over these variables and select the option “Initialize Variable” from the pop-up box.

As soon as you click on the “Initialize variable” link, all the respective variables get initialized with their default values as mentioned below. Observe that for int data type default value is 0 and for String data type default value is null.

Or maybe you can declare the variables outside the method to make them class variables which would automatically take default values. Something like this –
public class Sample3 {
// Here I have declared the variables outside the method
int intVar;
String strVar;
public static void main(String[] args) {
System.out.println(intVar);
System.out.println(strVar);
}
}
Hi Nick,
In this case, you have to declare variables with the ‘static’ keyword. Else you won’t be able to access the same in the main method. Here is the updated code:
public class Test {
static int i;
static String s;
public static void main(String[] args)
{
System.out.println(i);
System.out.println(s);
}
}