• 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

Behavior of primitive types when incremented beyond their max limit

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please explain the following?


On compilation, i get a compile time error only at line 1 but not at line 2 as i had expected. Please explain why compiler okays line 2.
I am using jdk 1.4.
Thanks
[ August 15, 2006: Message edited by: Tresa P Anthony ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = (byte)(127 + 1); //1
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tresa P Anthony:
Hi,

Could you please explain the following?


On compilation, i get a compile time error only at line 1 but not at line 2 as i had expected. Please explain why compiler okays line 2.
I am using jdk 1.4.
Thanks

[ August 15, 2006: Message edited by: Tresa P Anthony ]



The reason for the compile-time error in line 1 is that in a binary addition operation involving int and byte,char, or short, the other operand is promoted to an int, and the result of the operation is of type int. You cannot assign an int which is not a compile-time constant in the range of byte into a byte without an explicit cast.

In the second line, it only matters that the type of the expression is int.

The compiler does not carry out any calculations. It is only checking type in this case.
 
Tresa P Anthony
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the prompt reply.
Could you please confirm/clarify the following?

You cannot assign an int which is not a compile-time constant in the range of byte into a byte without an explicit cast.

In the second line, it only matters that the type of the expression is int.

The compiler does not carry out any calculations. It is only checking type in this case.



So in line 1, compiler computes the value (which is int) and complains as the value is outside the range of byte.
In line 2, compiler does not do any computation as we are trying to put an int value back into an int variable.

Please correct me if i got this wrong.

Thanks,
Tresa
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is not doing any computations.

The problem is not that 127 + 1 is outside the range of byte. The problem is that the type of the sum is int.
 
Tresa P Anthony
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if the compiler does not perform any computations, how does it distinguish between the following assignments as legal/illegal?

1) byte b = 126 + 1; //legal
2) byte b = 126 + 6; //not legal without explicit casting to byte

Thanks
Tresa
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
The compiler is not doing any computations.

The problem is not that 127 + 1 is outside the range of byte. The problem is that the type of the sum is int.



Yes it is.
127 + 1 is a constant expression that exceeds the range of byte. A byte is assignable to any constant expression that is within range.
byte b = 7; // fine
 
Tresa P Anthony
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the statement
byte b = 126 + 1;
the actual assignment of result to the variable 'b' happens at compile-time or is it at runtime?

Thanks
Tresa
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The actual assignment is at runtime, because the variable only really exists at runtime.

The value that you are assigning to the byte, "126 + 1", is calculated at compile time, because it's a constant. The compiler is smart enough to see that it's a constant, so it computes the value (127) while compiling.

Note that the compiler is also smart enough to see if the value fits into a byte or not. The range of byte is -128 ... +127. If the value of the constant is outside of this range, the compiler will give you an error because it doesn't fit in the byte.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
127+1 and Integer.MAX_VALUE+1 are compile time constatns, so compiler does perform the computation. Result is of type int in both cases (values are 128 and Integer.MIN_VALUE).
At runtime this results would be used (no more computation) and assigned to variables b and i.

The error on first line is caused by trying to assign int to byte.

Assigning int value to byte variable without explicit conversion is possible only if
  • The int value is compile-time constant
  • and the int value is between -128 and 127.

  • The second line assigns int to int which is OK.

    [ August 16, 2006: Message edited by: Vlado Zajac ]
    [ August 16, 2006: Message edited by: Vlado Zajac ]
     
    Tresa P Anthony
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi all,

    Thanks a lot for the explanations.

    Tresa.
     
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    but there is still an error with
    byte b = Byte.MAX_VALUE +1
    as compared to
    int i = Integer.MAX_VALUE + 1


    what is the reason..??
    thanks
     
    Tresa P Anthony
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Originally posted by Vlado Zajac

    The error on first line is caused by trying to assign int to byte



    As Mr.Zajac mentioned in an earlier post, the result of (Byte.MAX_VALUE +1) is of type int.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic