• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java bug? "byte" example...

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm currently busy preparing for the SCJP exam and came across something that I don't quite understand (unless it is a known bug in Java???)
Please consider the following code:
//
// Byte Test
//
public class ByteTest
{
public static void main(String args[])
{
byte b = 127;
b++;
System.out.println("The value of b: " + b);
}
}
I would expect a runtime exception here since the value range for a byte type can only be -128 thru 127. However the result of "b" is "-128"? Am I missing something here?
Thanks for your input...
Cheers,
Gene.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Gene.
It's not a bug, it's known Java behavior. From my RHE, Chapter 2:


For example, byte values can represent a range of -128 to +127, so if two particular bytes have the values 64 and 4 respectively, then multiplying them should, arithmetically, give a value of 256. Actually, when you store the result in a byte variable you will get a value of 0, since only the low-order eight bits of the result can be represented.


So your byte +127 is represented in binary as
01111111
When you add 1 to it, you get
10000000
and this value is equal to -128.
Java throws no exception, or even any indication, when this kind of "rollover" (from high positive values to low negative ones) occurs, so the lesson here is to choose the types of your primitives wisely.
Art
 
Gene Coetzee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Art, I sure will keep this in mind...
Cheers,
Gene.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gene Coetzee:
Hi,
I'm currently busy preparing for the SCJP exam and came across something that I don't quite understand (unless it is a known bug in Java???)
Please consider the following code:
//
// Byte Test
//
public class ByteTest
{
public static void main(String args[])
{
byte b = 127;
b++;
System.out.println("The value of b: " + b);
}
}
I would expect a runtime exception here since the value range for a byte type can only be -128 thru 127. However the result of "b" is "-128"? Am I missing something here?
Thanks for your input...
Cheers,
Gene.


i++ for byte works like byte((int)i+1);
and hence byte(128) which is -128;
check it out using bits
or think it as a biggest__smallest form of loop in truncation I;e after the biggest number say of integer next number to follow is the smallest.

sandeep
[This message has been edited by sandeep bagati (edited April 13, 2001).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"I would expect a runtime exception here since the value range for a byte type can only be -128 thru 127."
Java does no checks for runtime math overflows such as the one you suggest. Right off-hand, the only Exception I can think of that you can get with primitive math is ArithmeticException due to integer divide by zero.
Bill
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
"I would expect a runtime exception here since the value range for a byte type can only be -128 thru 127."
Java does no checks for runtime math overflows such as the one you suggest. Right off-hand, the only Exception I can think of that you can get with primitive math is ArithmeticException due to integer divide by zero.
Bill


I didnt get u Bill .Can u please clarify???
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,
See JLS§5.1.3 Narrowing Primitive Conversion
When a primitive type is assigned a result which is larger than the type size; the high order bits are discarded. No error is thrown.
For more information on hi-order, low-order bytes see Cup Size and Cat and Mouse Games with Bits and Bytes.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jane.
Yah u are right and i have read that part of JLS.But using i++ operator with a byte it works as i have said above.I forgot to tell for the Integer though u get the same result i:e truncation occurs but i suppose here the addition is done at integer level only and then the trunction of the long to integer while in case of byte first the byte is converted to int then the addition and then truncation to byte occurs.
that is why byte=byte + 1 gives a compile time error and not int=int+1 whatever the corresponding int or byte value is
Do correct me if i am wrong.
sandeep
[This message has been edited by sandeep bagati (edited April 14, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic