What's the default value of char?

JavaChar

Java Problem Overview


char c = '\u0000';

When I print c, it shows 'a' in the command line window.

So what's the default value of a char type field?

Someone said '\u0000' means null in Unicode; is that right?

Java Solutions


Solution 1 - Java

The default value of a char attribute is indeed '\u0000' (the null character) as stated in the Java Language Specification, section §4.12.5 Initial Values of Variables .

In my system, the line System.out.println('\u0000'); prints a little square, meaning that it's not a printable character - as expected.

Solution 2 - Java

'\u0000' is the default value for a character. Its decimal equivalent is 0.

When you are declaring some char variable without initializing it, '\u0000' will be assigned to it by default.

see this code

public class Test {
	char c;

	public static void main(String args[]) throws Exception {
		Test t = new Test();
		char c1 = '\u0000';
		System.out.println(t.c);
		System.out.println(c1);
		System.out.println(t.c == c1);
	}
}

This code will print true for the last print.

Solution 3 - Java

Default value of Character is Character.MIN_VALUE which internally represented as MIN_VALUE = '\u0000'

Additionally, you can check if the character field contains default value as

Character DEFAULT_CHAR = new Character(Character.MIN_VALUE);
if (DEFAULT_CHAR.compareTo((Character) value) == 0)
{

}

Solution 4 - Java

'\u0000' stands for null . So if you print an uninitialized char variable , you'll get nothing.

Solution 5 - Java

It's '\u0000'. See here for more information.

Solution 6 - Java

The default char is the character with an int value of 0 (zero).

char NULLCHAR = (char) 0;

char NULLCHAR = '\0';

Solution 7 - Java

its tempting say as white space or integer 0 as per below proof

char c1 = '\u0000';
System.out.println("*"+c1+"*");
System.out.println((int)c1);

but i wouldn't say so because, it might differ it different platforms or in future. What i really care is i ll never use this default value, so before using any char just check it is \u0000 or not, then use it to avoid misconceptions in programs. Its as simple as that.

Solution 8 - Java

The default value of a char data type is '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

You can see the info here.

Solution 9 - Java

Note that there is a distinct difference between null and zero. In http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html (referenced above), the statement is made :-

There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

That is why the following statements will give you an error and not the other :-

char a = null; //Type mismatch: cannot convert from null to char.

char b = 0; //Valid syntax.

Solution 10 - Java

\u0000 is the default value for char type in Java

As others mentioned, you can use comparison to check the value of an uninitialized variable.

char ch;
if(ch==0)
    System.out.println("Default value is the null character");

Solution 11 - Java

I think it is '\u00000' or just '' rather than '\u0000' (The 1st one has 5 zeros while the last one has four zeroes.)

Solution 12 - Java

Default value for char is \u0000

public class DefaultValues {
char varChar;
public static void main(String...l)
 {
	DefaultValues ob =new DefaultValues();
	System.out.println(ob.varChar=='\u0000');
 }	
}

This will return true

Solution 13 - Java

The default value of char is null which is '\u0000' as per Unicode chart. Let us see how it works while printing out.

public class Test_Class { 	
     char c;
     void printAll() { 	
       System.out.println("c = " + c);
    } 	
    public static void main(String[] args) { 	
    Test_Class f = new Test_Class(); 	
    f.printAll(); 	
    } }

Note: The output is blank.

Solution 14 - Java

The default value of a char primitive type is '\u0000'(null character) as stated in the Java Language Specification.

The shortcut for 'u0000' is '\0', So the null can be represented either by 'u0000' or '\0'.

The below Java program validates null representations using instance char field 'c'.

public class DefaultValueForchar {	
	char c;
	public static void main(String[] args) {
		char c0 = '\0';
		char cu0000 = '\u0000';
		DefaultValueForchar obj = new DefaultValueForchar();
		System.out.println(obj.c);
		System.out.println(c0);
		System.out.println(cu0000);
		System.out.println(c0==cu0000);
		System.out.println(obj.c==c0);
		System.out.println(obj.c==cu0000);
	}

}

output:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1298336View Question on Stackoverflow
Solution 1 - JavaÓscar LópezView Answer on Stackoverflow
Solution 2 - JavaChandra SekharView Answer on Stackoverflow
Solution 3 - JavaYogesh ManwareView Answer on Stackoverflow
Solution 4 - JavaroshanView Answer on Stackoverflow
Solution 5 - JavaMatt FenwickView Answer on Stackoverflow
Solution 6 - JavaThe CoordinatorView Answer on Stackoverflow
Solution 7 - JavaMateenView Answer on Stackoverflow
Solution 8 - JavaLuis ParadaView Answer on Stackoverflow
Solution 9 - JavaChooView Answer on Stackoverflow
Solution 10 - JavaAnish SharmaView Answer on Stackoverflow
Solution 11 - JavaBcomView Answer on Stackoverflow
Solution 12 - JavaShubham MalhotraView Answer on Stackoverflow
Solution 13 - Javalft93rytView Answer on Stackoverflow
Solution 14 - Javajavapedia.netView Answer on Stackoverflow