Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is zero is different from (char)0 ?

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Siva kandasamy:
Is zero is different from (char)0 ?

No
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wayne L Johnson:
Remember this is "Java in General (beginner)".

Point.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
All make sense. - Siva
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic