Results 1 to 5 of 5
Thread: Doubt in Unboxing error
- 07-04-2011, 10:08 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
Doubt in Unboxing error
Got a code from Complete Reference
class UnBoxingError
{
public static void main(String s[])
{
Integer iOb=1000;
int i=iOb.byte.value();
System.out.println(i);
}
}
The output is -24. I am not clear about how come we are getting -24? In the book it written that we are getting garbage value. But there should be some reason of getting this value -24.
-
1000 is greater than byte's maximum value of 127, and so you're getting an expected result.
- 07-04-2011, 11:08 PM #3
For those who like bits and bytes:
Notice the rightmost two nibbles(1 byte): e8Java Code:int ix = 1000; System.out.println("1000=" + Integer.toHexString(ix)); // 1000=3e8 ix = -24; System.out.println("-24=" + Integer.toHexString(ix)); //-24=ffffffe8
- 07-04-2011, 11:44 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Think of it as if it wraps, and when a value goes over, it goes to the minimum value. 128 is greater than a bytes maximum value, so it 'wraps around' to -126. 1000 'wraps around' multiple times, and ends up at -24. It's not exactly what happens, and Norm's example is more complete, but it's an easy way to think of it when you're trying to figure out what happens when a numeric value goes over the maximum of its type.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-04-2011, 11:55 PM #5
Similar Threads
-
Best Practice wrt Boxing / Unboxing
By johnnyforeigner in forum Advanced JavaReplies: 2Last Post: 06-05-2011, 12:10 PM -
a doubt
By Thia in forum New To JavaReplies: 13Last Post: 11-24-2009, 02:26 AM -
have doubt
By ras_pari in forum Advanced JavaReplies: 3Last Post: 10-01-2009, 09:51 AM -
doubt
By Chris555 in forum NetworkingReplies: 0Last Post: 08-05-2009, 02:58 PM -
Doubt
By winkodes in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 12-20-2008, 08:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks