I'm studying for my SCJP2 certification and ran across this code on a quiz:
Since i is an int and b is a byte, I assumed that the compiled would complain about the assignment. The quiz answer says it works and I tried it on my machine (Wintel) and it sure-enough did! I can only assume that the compiler is treating i as a constant and allowing the assignment because i is marked final. I haven't found any reference to this behavior in any of the Java books I've seen. Can anybody point me to a good reference that might cover it? Thanks! ------------------ TANSTAAFL (edited by Cindy to add formatting) [This message has been edited by Cindy Glass (edited May 30, 2001).]
Basically you made it SOOOOOOO easy for the compiler that it said "What the Heck - I can do this". And it did. From the JLS 5.2 Assignment Conversion
In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied: The expression is a constant expression of type byte, short, char or int. The type of the variable is byte, short, or char. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: May 08, 2001
Understood. Syntactically the assignment was the same as 'byte b = 100' -- I have no problem with that. I guess what really bugged me is that I hadn't seen anything in four certification books, or the JLS that indicated the compiler _would_ make that logical jump. BTW, do you know if this is implementation specific - or would you expect any Java compiler to accept the code as presented?
Cindy in your first statement you that it should be a constant value. WHats the reason behind it? because when I try to compile the code removing the final keyword compiler gives me an error saying "Explicit cast needed to convert int to byte"
Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Joined: Sep 29, 2000
parmeet, When you put the keyword final on a primitive variable it is resolved at compile time instead of runtime and BECOMES a constant - the reason? - because it CAN, so Sun said it must, and had the JLS and JVM say that it must. Burkhardt, No this is not implementation specific. The JLS is the specification on how the language MUST work in any implementation by any vendor. Well, I don't have the RHE certification book with me right now, but I am fairly sure that primitive narrowing conversions were covered with the other narrowing conversions.
Joined: Feb 06, 2001
cindy i wanted to ask why does this happen with constants?
Joined: Sep 29, 2000
Because the compiler knows that the constant is never going to change value (unlike object references). Since it knows the value that the constant is at compile time, it can easily determine whether or not the value will FIT into the narrower variable. Since it is capable logically of doing the narrowing conversion, Sun decided to allow it.
___________________________ P.S: I am studying for certification. If you want, you can e-mail me at: firstname.lastname@example.org Ciao Thomas (edited by Cindy to "translate" German to English. Big mistake - my German is NOT that good) [This message has been edited by Cindy Glass (edited June 04, 2001).]
Thomas, There is a rule at JavaRanch that ALL posting must be done in English. This is because the moderators can not moderate stuff written in other languages. We have had folks try to post in all SORTS of languages and dialects, and it is IMPOSSIBLE for us to know all those languages. So please translate your postings into English and remove the German(?) Or I will have to do it for you.