aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit Narrowing Conversion 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 "Implicit Narrowing Conversion" Watch "Implicit Narrowing Conversion" New topic
Author

Implicit Narrowing Conversion

V Bose
Ranch Hand

Joined: Jul 10, 2003
Posts: 113


If I understand right, in the above code, a copy of the value of c in Line 1 is copied into the final variable c in the method parameter in Line 2.
If Line 3 is uncommented, the compiler complains that c is a final variable.
However when I return c (In line 4), the implicit narrowing conversion from a 'compile time constant char' to a byte, does not happen. Why ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The compiler does not know that every character c passed into the method m1 is going to fit in to a byte. So it gives the "possible loss of precision" error.

The compiler does not "run" your code to see that you do final char c = 'a'; //Line 1 before you do the call m1(c);


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jerry Zhang
Greenhorn

Joined: May 15, 2004
Posts: 6
From char to any of byte or short, and vice verse, you need explicit cast
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Jerry Zhang:
From char to any of byte or short, and vice verse, you need explicit cast


Well that is not entirely accurate. See the following code



The compiler does an implicit cast if the RHS is a compile time constant.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Jerry Zhang
Greenhorn

Joined: May 15, 2004
Posts: 6
Vicken,
thanks.
jerry
V Bose
Ranch Hand

Joined: Jul 10, 2003
Posts: 113
Originally posted by Barry Gaunt:
The compiler does not know that every character c passed into the method m1 is going to fit in to a byte. So it gives the "possible loss of precision" error.

But in the case of

the compiler does not worry that every character c may not fit into a byte?
Am I the only one who feels that the behaviour was not very intiutive Oh well, I'll just remember the scenario for the exam...
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by V Bose:

the compiler does not worry that every character c may not fit into a byte?


Yes, that is correct, because 'c' is a compile time constant.

This might help you understand compile time constant more
http://www.coderanch.com/t/193475/java-programmer-SCJP/certification/strange-things-happen-Check
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Vee wrote:
Am I the only one who feels that the behaviour was not very intuitive


Nope, no way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit Narrowing Conversion