aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the Initial value of char literal? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the Initial value of char literal?" Watch "What is the Initial value of char literal?" New topic

What is the Initial value of char literal?

Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
What is the Initial value of unassigned char literal?
I thought it to be null & so does the JLS says...JLS Initial Value of Variables

But then why does this runs without null pointer exception?

The output is false false.
But when you print ch as System.out.prinln(ch) it prints nothing on screen [ neither null nor 0]. Can someone explian?
Source: Enthuware

SCJP 6 83%
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

The "char" data type is a primitive, not a reference type, so you will never get a NullPointerException from one of those.

The initial value of a "char" variable is indeed the null character, as your link to the JLS says. But the null character and the null reference are not the same thing. A null character is (as that link says) simply a character whose Unicode value is zero.

And most output devices are set up to display nothing for a null character. That's why you are seeing nothing when you output it.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
char is a primitive type, not object, field of type char cannot be null
- it's initial value is 0 (character with code 0).
Justin Smith

Joined: Jul 24, 2008
Posts: 19
char's initial value is '\0'. This is nothing but character null.
marc weber

Joined: Aug 31, 2004
Posts: 11343

I would just add that a char can be widened to type int (or wider). So when an int is expected (for example, as an array index), the null character's numeric value of zero is converted to an int value of zero.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
hmm i got it ... thanks to you all
Tejas V Shah

Joined: Mar 20, 2008
Posts: 20
char initial literal value is \u0000
It is sorta covered in the JavaRanch Style Guide.
subject: What is the Initial value of char literal?