[java] what's the default value of char?

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?

This question is related to java char

The answer is


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


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)
{

}

\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");

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:


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.


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


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


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

char NULLCHAR = (char) 0;

char NULLCHAR = '\0';


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.


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


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.


'\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.


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.