wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion   " Watch "Confusion   " New topic
Author

Confusion

Priya Jotwani
Ranch Hand

Joined: Oct 30, 2002
Posts: 53
When I try to print the value of z, it gives 64 as result .Can anyone plz tell why ??
byte x=64;
byte y=5;
byte z= (byte)(x*y);
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Ideally with proper data type you should get 320 as answer but as it exceeds the capacity of a byte most significant bits (MSB) in binary representation is chopped of . Amount decimal reprn. of remaining bits is 64.
i.e. 101000000 is 320 but if you try to fit that in byte first 10 is chopped of so you get 1000000 which is 64 in dec.
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
Hi,
64 is getting printed correctly. Let me explain how,
z = 64 * 5 = 320 == 0x140
You are doing a type conversion (narrowing) on z, this drops the higher order bytes and takes only upto the bytes specified for the datatype,
byte - 1 byte
short - 2 bytes
int - 4 bytes
(byte)0x140 = 0x40 = 64 in decimal, so 64 is getting printed.
----------------------------
vijay


Thanks,<p>Vijay<p>The Hand that gives, Gathers.
Priya Jotwani
Ranch Hand

Joined: Oct 30, 2002
Posts: 53
Thanx Bhushan!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion
 
Similar Threads
byte range
byte range
how to know the numeric type value after type cast if it exceeds the range
inc / dec Operations precedence