File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Narrowing conversions doubt 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 "Narrowing conversions doubt" Watch "Narrowing conversions doubt" New topic
Author

Narrowing conversions doubt

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi,

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



"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
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


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Jan 19, 2006
Posts: 584
Thank you for your prompt replies.
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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

Joined: Dec 31, 2004
Posts: 961
See JLS 5.2 under Assignment Conversion, and JLS 15.28 under Constant Expression.
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Narrowing conversions doubt