Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operators

 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Animesh you are absolutely right .
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic