This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

whats the output of this statement please!!!  RSS feed

 
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.
 
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?
 
Java Cowboy
Posts: 16084
88
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.
 
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
 
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."
 
author
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!