*
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
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
Author

What is the Initial value of char literal?

Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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%
Working on SCWCD/OCPJWCD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Greenhorn

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

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
sscce.org
Milan Sutaria
Ranch Hand

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

Joined: Mar 20, 2008
Posts: 20
char initial literal value is \u0000
 
Don't get me started about those stupid light bulbs.
 
subject: What is the Initial value of char literal?
 
Similar Threads
can default value of char be used as an index of array?
Array element
doubt on char default value...
'if'block ??
Char default value - Enthuware question