wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes integers initialization - Interesting !!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "integers initialization - Interesting !!!!" Watch "integers initialization - Interesting !!!!" New topic
Author

integers initialization - Interesting !!!!

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi, I thinks this comment is quite interesting.
This is a basic initialization, which of course compiles.


Naturally, as you know, the following code doesn't compile.
Block 1:

You can note that I exceeded by one unity the limits for the given data types.

The following is more interesting. It doesn't compile for the first 3 cases, but it compiles for the last one.
Block 2:


It'd have been very easy to guess that the fourth option wouldn't compile, but it actually does.
The point is that in Block 1 initializations the compile error for the first 3 is a casting error (cannot convert from int to short, char, etc...). But the compile error for the last case in Block 1 is an out of range error.

Then I guess that in block 2, the compiler is not smart enoght to realize that it adding one to the int limit will became out of range, am I right???


Leandro Melo
SCJP 1.4, SCWCD 1.4
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Leandro Melo:



In the above expression, the you have a literal constant of 2147483648 which is outside the limit.

Below however, you have an expression which when evaluated will result to -2147483648, which is still within the limit of an integer type.
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 82
Originally posted by Leandro Melo:

The following is more interesting. It doesn't compile for the first 3 cases, but it compiles for the last one.
Block 2:


It'd have been very easy to guess that the fourth option wouldn't compile, but it actually does.
The point is that in Block 1 initializations the compile error for the first 3 is a casting error (cannot convert from int to short, char, etc...). But the compile error for the last case in Block 1 is an out of range error.

Then I guess that in block 2, the compiler is not smart enoght to realize that it adding one to the int limit will became out of range, am I right???


Well, the only explanation I see is compiler doesn't perform value checking, only type checking. The result of + operator on numbers is always typed at least int, therefore first 3 lines cannot compile without the casts. Fourth line is typed int, so it compiles. Of course the 'smart' compiler could check the value (I'm not sure what JLS says about it), but what in case of some other compiler that does not do it?


Adam<br /> <br /><i>SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5</i>
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
I think Alton has clarified this.
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: integers initialization - Interesting !!!!
 
Similar Threads
int -> float -> int and loss of precision
implicit conversion
question on Integer data type
Reg. maximim int. value
Clarification needed on max value of primitive data types...