This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Aug 24, 2001
Posts: 77
posted
0
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
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 ]
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++.
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: 18670
posted
0
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.
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
posted
0
Thanks. I got it now. But what does '\u0000' mean? Lee
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
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
posted
0
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.