*
The moose likes Beginning Java and the fly likes Is zero is different from (char)0 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is zero is different from (char)0 ?" Watch "Is zero is different from (char)0 ?" New topic
Author

Is zero is different from (char)0 ?

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
an array of primitives is
automatically initialized to zero for numeric types, (char)0 for
char, and false for boolean.

Is zero is different from (char)0 ?
If so, how ?
thanks
siva
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Siva kandasamy:
Is zero is different from (char)0 ?

No
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Billybob Marshall:

No

I think I have to disagree. Although they both have the exact same bit-wise value in memory, there are a few subtle differences. First, 0 has an int type and (char)0 has a char type. IIRC, there are some implicit conversions made between the two, they are still not the same type. This leads the the second difference, they take different amounts of memory. I don't remember the exact numbers, but I think a char is 2 bytes whereas an int is 4 bytes.
I am willing to bet that these subtle differences can cause problems if they are completely ignored. There may also be other differences that I haven't thought about at the moment.
HTH
Layne


Java API Documentation
The Java Tutorial
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
I knew some yahoo would come along and refute it. However, I believe that in the context of the question (which didn't involve "how many bytes does it take", etc), the simple "No" answer suffices.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I decided to "ask" the ultimate authority, so composed the following amazing program:

The answer? ... Does (char)0 == 0 ?: true
Remember this is "Java in General (beginner)".
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Wayne L Johnson:
Remember this is "Java in General (beginner)".

Point.
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Thank you.
All make sense. - Siva
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And yet:

prints:

Depending on your system, you may see a little box, or a blank, or maybe a ?, at the end of the second line. Not a 0 though.
Remember this is "Java in General (beginner)".
I think the beginner-level answer is that they're usually considered the same, but not always. The details of how and when they're different may not be beginner-level, but that doesn't mean differences don't exist.
[ February 23, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is zero is different from (char)0 ?