wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the dafault value for char? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the dafault value for char?" Watch "What is the dafault value for char?" New topic
Author

What is the dafault value for char?

Lee Xu
Ranch Hand

Joined: Aug 24, 2001
Posts: 77
Thanks. Lee
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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;


GenRocket - Experts at Building Test Data
Lee Xu
Ranch Hand

Joined: Aug 24, 2001
Posts: 77
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

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Aug 24, 2001
Posts: 77
Thanks. I got it now. But what does '\u0000' mean?
Lee
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Aug 24, 2001
Posts: 77
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the dafault value for char?