aspose file tools
The moose likes Beginning Java and the fly likes What happens during casting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What happens during casting" Watch "What happens during casting" New topic
Author

What happens during casting

Sathish Kumar Govindan
Greenhorn

Joined: Jan 23, 2007
Posts: 17
Hi all,
what happens during the following operation;

what will be the value of s? Pleasez, don't show me the output.My doubt is "what happens internally" how the two's complementation of the long value take place. what will happen to the Least significant bits.... Show me the bitwise calculation.....

Thanks in Advance...


SCJP 5.0, SCWCD JEE5
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
To represent the number 40002 in binary requires 16 bits - 1001110001000010. The long data type is 64 bits - hence no "two's complementation". It's a positive integer - 16 bits well and truly fits into 64 bits.

The short data type is 16 bits in length, so when the cast takes place, the binary representation does not change. However, since the leftmost bit is a "1", this number now becomes a negative. To find it's value using two's complement arithmetic, subtract 1 and then flip the bits and you get 25534.

Therefore, 40002 (long) is -25534 (short).

Not sure what you mean by the "internals". That's how I work these things out in my head (errh .. pen and paper - and perhaps a hex calculator).

Regards,
JD
[ January 23, 2007: Message edited by: John Dell'Oso ]
Steve Fahlbusch
Ranch Hand

Joined: Sep 18, 2000
Posts: 492
    
    2

Greetings

First don't be surprised if someone talks to you about your login name.

Also, for common items like this, searching is your friend - what just returned for mine was a reference to the language spec.

java language specification (conversion)
[ January 23, 2007: Message edited by: Steve Fahlbusch ]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

Here's my explanation, which is almost the same as John's, but maybe this clarifies a bit with the part "what happens internally".

The variable y is a long, which is 64 bits. So the value of y in binary is this:

0000000000000000000000000000000000000000000000001001110001000010

Now, when you cast this to a short, which is 16 bits, the upper 48 bits are simply discarded. So that's exactly "what happens internally" - the upper 48 bits are just thrown away and you end up with the lower 16 bits in the variable s:

1001110001000010

This bit pattern represents the value -25534.

Signed integer data types in Java (byte, short, int, long) store numbers in two's complement by definition.

So no "magic" happens internally - it just takes the lower 16 bits of the long value and stores it in the short.
[ January 23, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might be interested in JLS - 5.1.2 through 5.1.4.


"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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What happens during casting
 
Similar Threads
SCJP 1.5, Chapter 6 question 4
exceptions
Java Constructors
How many String Objects are created?
Type Casting.