105
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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
user1298336
  • 1,075
  • 2
  • 8
  • 6

14 Answers14

121

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
32

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

Chris
  • 10,337
  • 1
  • 38
  • 46
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • if you are on an interview and you need to check if char is default or not, don't try to memorize `\u0000`. If to convert default char to numeric using `Character.getNumericValue`, it will return -1. To extend the code from the answer, these two statements will both print -1 ```System.out.println(Character.getNumericValue(t.c)); System.out.println(Character.getNumericValue(c1));``` – walv Nov 03 '21 at 18:34
12

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

}
Yogesh Manware
  • 1,843
  • 1
  • 22
  • 25
6

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

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
roshan
  • 2,410
  • 2
  • 25
  • 37
3

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

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
0

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

char NULLCHAR = (char) 0;

char NULLCHAR = '\0';

The Coordinator
  • 13,007
  • 11
  • 44
  • 73
0

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.

Mateen
  • 1,631
  • 1
  • 23
  • 27
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.

Luis Parada
  • 109
  • 1
  • 4
  • 3
    The question already has an accepted answer plus several other answers. For your answer to be useful it should add useful new information or new insights to the problem. Just repeating information in other answers is not helpful. – AdrianHHH Dec 10 '14 at 16:17
0

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.

Choo
  • 1
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");
Anish Sharma
  • 314
  • 3
  • 18
-1

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

Johan B
  • 890
  • 3
  • 23
  • 39
Bcom
  • 1
  • @JohannesH.- this looks like an answer to me. Flagging this as 'not an answer' is not correct IMO. – Krease Jun 06 '16 at 05:34
  • Have you tried `char c = '';`? I except you'll get a `error: empty character literal`. Also 5 zeroes `'\u00000'` gets you `error: unclosed character literal` – Scratte Jan 29 '21 at 17:33
-1

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

chillworld
  • 4,207
  • 3
  • 23
  • 50
-1

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.

lft93ryt
  • 948
  • 1
  • 16
  • 32
-2

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:

javapedia.net
  • 2,531
  • 4
  • 25
  • 50