Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the dafault value for char?

 
Lee Xu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Lee
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the default value for a char is null. int's are the only data type that get an initial value other than null. However, you never know what the value is so it is always a good idea to give your int's an initial value. I think the compiler will even error out on a statement like
int j;
if j is never given a value like
j = 0;
 
Lee Xu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg,
Thanks for your reply.
I think the default value for int is 0. If you define an int i as class member, the compiler will initialize it to 0. But i forgot the defaut value for char.
Regards
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
I believe the default value for a char is null. int's are the only data type that get an initial value other than null.

nope -- here are the default values for member variables (not local variables)

[ July 24, 2002: Message edited by: Jessica Sant ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if it is a class member, however this guarantee doesn�t apply to �local� variables�those that are not fields of a class. Thus, if within a function definition you have:
int x;
Then x will get some arbitrary value like in C and C++.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Yes, if it is a class member, however this guarantee doesn�t apply to �local� variables�those that are not fields of a class. Thus, if within a function definition you have:
int x;
Then x will get some arbitrary value like in C and C++.

Actually I think you'll get a compiler error/warning saying that the variable hasn't yet been initialized.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A char is a primitive data type, not a reference type. So null (in the Java sense) is not a possible value. In other languages a 0 might be referred to as NULL or null, but in Java this would just create confusion.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, which is what I had originally stated:
I think the compiler will even error out on a statement like
int j;

I just forgot about scope and how it only applied to local variables. Thanks Jessica.

[ July 24, 2002: Message edited by: Gregg Bolinger ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
A char is a primitive data type, not a reference type. So null (in the Java sense) is not a possible value. In other languages a 0 might be referred to as NULL or null, but in Java this would just create confusion.

But setting a local data type to null as in
char c = null;
will allow the code to compile, though you can't retrieve and use a null value. But you can check for null as in:
if c == null....
Right?
 
Lee Xu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I got it now. But what does '\u0000' mean?
Lee
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg - you should try to compile your examples.
Lee - it means a character whose Unicode value is 0000 in hexadecimal, which is 0. This character is called the "NUL" character - in other languages, it might be used to indicate the end of a string. In Java it just indicates you haven't put any useful value into your char field yet.
It's important to realize that null and NUL (or '\u0000' or 0) are not the same thing. The following code will not compile:

But this does:

And this has the exact same effect:
 
Lee Xu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Thank you very much for your detail expl. If I try to output that value '\u0000', what should be the result? Sorry, i should try it by myself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic