Skip to toolbar

QnA | How do I get the default value of data type?

Frequently Asked Questions

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.

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.

int data type default value is 0 and for String data type default value is null.
Video: Getting default values of various data types in Java.

1507total visits,1visits today

2 thoughts on “QnA | How do I get the default value of data type?

  1. 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);
    }
    }

    1. 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);
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 4
  • Page views today : 4
  • Total visitors : 48,849
  • Total page view: 67,263

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.