aspose file tools*
The moose likes Beginning Java and the fly likes whats the output of this statement please!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "whats the output of this statement please!!!" Watch "whats the output of this statement please!!!" New topic
Author

whats the output of this statement please!!!

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Whats the output of System.out.println((int)(char)(byte)-1); and why??

Thanks in advance,
csr.
Fritz Urling
Greenhorn

Joined: Jan 09, 2006
Posts: 13
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

Joined: Jun 03, 2005
Posts: 228
I know the result is 65535.
but i dont know why its 65535?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Ok, then now tell us what you had expected instead of 65535 and why.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dan Longest
Greenhorn

Joined: Apr 28, 2005
Posts: 5
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


"If I want a group decision I'll look in a mirror" - Warren Buffett
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats the output of this statement please!!!