Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Narrowing conversions doubt

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please explain why the first following code compiles fine ?

 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
maybe because its like
byte b = 100 ;
or the return type is a compile time constant ( it must be one of those reason )... you can try with 'return 128;' if you get an compiler error...(i am not home so i can't test )
arno
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arno,

I've just tried to use 128 and I got a compile error. "Possible loss of precision".

My doubt here is whether argument passing and returning values have different rules.

As per above code, it seems the rules are different.
[ April 14, 2006: Message edited by: Edisandro Bessa ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return value of #2 can be determined at compile time, since you use a constant value. The compiler determines that expression 100 is assigment compatible with the return type of the method, then the compiler does an automatic narrowing conversion.

The other case (#3) is different. Becuase you explicitly return a variable of type integer. The value of the variable at run time cannot be determined by the compiler. Hance, automatic narrowing conversion cannot be applied.

Regards,
Edwin Dalorzo.

PS.
See the Java Language Specification, look for "Assigment Conversion" in the second edition it is the 66 page.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this example of a compile time constant expression, which is a variation of your third case and which does compile:



In this case the value of the expression myInt is compile time constant expression (JLS 15.28) and its value can be determined at compile time.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your prompt replies.
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

original code:



I'm still not clear about the #3 case. doSomething() passes an int constant which is in range of the argument type byte...so should be ok the way case #1 works...BUT it isn't
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See JLS 5.2 under Assignment Conversion, and JLS 15.28 under Constant Expression.
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Edwin....I got it now!

The reasoning is explained nicely in JLS, in section 5.3 - Method Invocation Conversion. (http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25214)

---------
Ravinder
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic