This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the Initial value of char literal?

 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char's initial value is '\0'. This is nothing but character null.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm i got it ... thanks to you all
 
Tejas V Shah
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char initial literal value is \u0000
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic