This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Behavior of primitive types when incremented beyond their max limit
Tresa P Anthony
Greenhorn
Joined: Aug 14, 2006
Posts: 10
posted
0
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
posted
0
byte b = (byte)(127 + 1); //1
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
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
posted
0
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: 2341
posted
0
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
posted
0
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
posted
0
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
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.
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
posted
0
Hi all,
Thanks a lot for the explanations.
Tresa.
varungoyal goyal
Ranch Hand
Joined: May 31, 2006
Posts: 37
posted
0
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
posted
0
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.