File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Behavior of primitive types when incremented beyond their max limit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Behavior of primitive types when incremented beyond their max limit" Watch "Behavior of primitive types when incremented beyond their max limit" New topic
Author

Behavior of primitive types when incremented beyond their max limit

Tresa P Anthony
Greenhorn

Joined: Aug 14, 2006
Posts: 10
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 ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
byte b = (byte)(127 + 1); //1
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 14, 2006
Posts: 10
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 14, 2006
Posts: 10
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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
Tresa P Anthony
Greenhorn

Joined: Aug 14, 2006
Posts: 10
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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

    Joined: Aug 14, 2006
    Posts: 10
    Hi all,

    Thanks a lot for the explanations.

    Tresa.
    varungoyal goyal
    Ranch Hand

    Joined: May 31, 2006
    Posts: 37
    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

    Joined: Aug 14, 2006
    Posts: 10

    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Behavior of primitive types when incremented beyond their max limit
     
    Similar Threads
    expression involving int or smaller
    increment operator
    How to identify compile time constants
    doubt in instanceof operator..
    Why does'nt it get a default value..?