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

assign final int to byte works

 
Burkhardt Hufnagel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhardt Hufnagel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cindy i wanted to ask why does this happen with constants?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mughal's certification book covers these type of details which is why its still the best certification book.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo Burkhardt,
Your problem is described here:
http://www.javaranch.com/ubb/Forum24/HTML/002584.html

___________________________
P.S: I am studying for certification.
If you want, you can e-mail me at:
tmarkl@hotmail.com
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 Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

http://www.javaranch.com/ubb/Forum24/HTML/003617.html
(edited by Cindy to remove German comment)
[This message has been edited by Cindy Glass (edited June 04, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Cindy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic