• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats the output of this statement please!!!

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the output of System.out.println((int)(char)(byte)-1); and why??

Thanks in advance,
csr.
 
Fritz Urling
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you put it in your code, compile and run it? That should tell you what the ouput is. Then tell us why you think it is what it is, or why you think it should be different. A healthy discussion can then take place.
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the result is 65535.
but i dont know why its 65535?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then now tell us what you had expected instead of 65535 and why.
 
Dan Longest
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm preparing for SCJP and am no Java God I'll take a shot at this. That being said, I would have gotten it wrong without knowing the right answer. I was prepared to say the statement would yield 0.

The casting operator is right associative.
So in the statement (int)(char)(byte)-1, it will attempt to cast -1 to a byte, then cast that to a character, then cast that to an integer.

The cast of -1 to byte is fine. The problem is that char is an unsigned type. Proper values of char range from 0 to 65535. -1 is outside the allowable bounds of char. The compiler will allow this cast, but -1 will (apparently) "wrap around", like will happen when doing arithmetic with signed types. My problem is that I thought this was an impossibility given char being unsigned. At any rate, casting this result to integer is no big deal, giving the result. So the cast of a negative to char is what causes the difficulty. Is this behavior undefined, or will this statement always yield the same result?

Regards,
Dan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Longest:
... So the cast of a negative to char is what causes the difficulty. Is this behavior undefined, or will this statement always yield the same result? ...

Good discussion! The conversion from byte to char is both widening and narrowing. According to JLS - 5.1.4: "First, the byte is converted to an int via widening primitive conversion, and then the resulting int is converted to a char by narrowing primitive conversion."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Good discussion! The conversion from byte to char is both widening and narrowing. According to JLS - 5.1.4: "First, the byte is converted to an int via widening primitive conversion, and then the resulting int is converted to a char by narrowing primitive conversion."


Good catch. That explains the behaviour - it's fully defined.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
... Good catch...

Well, I didn't realize how this worked until I checked the JLS, so I would have gotten this question wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic