What is Default Value of Char in Java

What is default value of char in java , In this tutorial , We will see what is default value of char in java. To check char default value in java , we create unassigned char variable and print it’s value . default value for char in java is ‘\u0000’ . We will write java program for this verified it .

Note : The default value of char in java is ‘\u0000’ .

Program to find the default value of char in java

We will write a java program to print default value for char in java . We create a char value defaultChar and print it’s value .

public class DefaultCharValue {
    	    char defaultChar;

    		public static void main(String[] args) {
    			DefaultCharValue defaultCharValue = new DefaultCharValue();
    			defaultCharValue.printCharDefaultValues();
    		}

    		public void printCharDefaultValues() {
    			// print default value of char
    			System.out.println("Default value of char :" + defaultChar);
    		}

}

Output:

Default value of char :

In this output default value of char is ‘\u0000’ . for more details  java docs . Actually char default value in java is ‘u0000’ this is why blank is printed as default value.

Now we will verify that default value for char in java is ‘\u0000’. For this we create a java program in which we create unassigned char variable(defaultChar) and one more char variable(ch) and assigned ‘\u0000’ to ch variable . Now compare value of variable defaultChar and ch , which are equal here , It means char default value in java is ‘u0000’ .

public class DefaultCharValue2 {
	char defaultChar;

	public static void main(String[] args) {
		DefaultCharValue2 defaultCharValue2 = new DefaultCharValue2();
		defaultCharValue2.printCharDefaultValues();
	}

	public void printCharDefaultValues() {
		char ch = '\u0000';
		// print default value of char
		System.out.println("Default value of char :" + defaultChar);
		System.out.println("Value of ch :" + ch);
		boolean flag = ch == defaultChar ? true :false;
		System.out.println("value of ch and defaultChar are equal : "+ flag);
	}

}

Output:

Default value of char :
Value of ch :
value of ch and defaultChar are equal : true

In this tutorial we have seen that what is default value of char in java  , we have also written java program to verified char default value in java . You can see more java program for practice .

Leave a Reply

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

− 6 = 1