wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators 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 "Operators" Watch "Operators" New topic
Author

Operators

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
I have a doubt:

final int i1 = 20;
short s1 = i1;

int i2 = 20;
short s2 = i2;

Why does the first one doesnt give a compiler error?

Well, according to me in the first case the compiler is sure that the calue of i1 will never change and also since 20 is in the range of short, so it doesn't display an error
But thats not the case in second case

Please tell me whether i am right or not

Thanks
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Did you try compiling a small program with the above code? I bet you will get a compile time error saying that there is a possible loss of precision. Check that once again please


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Thats what i mean, In the first case i am not getting any compiler error, but for the second case i am getting a compiler error as u said
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Animesh you are absolutely right .
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
I looked through the language specifications. I could not find anything under the sections "5.1.3 Narrowing Primitive Conversions" that talk about a narrowing down conversion from an int to a short. So I think you are probably correct. I am not sure though
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
There is an implicit narrowing conversion from a byte, short, char, or int to a byte, short, or char when the source is a constant expression that can be evaluated at compile time and the value fits in the target. This does not apply to arguments in method invocations.

See JLS 5.2 Assignment Conversion.
[ January 07, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operators