*
The moose likes Beginning Java and the fly likes assign final int to byte works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assign final int to byte works" Watch "assign final int to byte works" New topic
Author

assign final int to byte works

Burkhardt Hufnagel
Greenhorn

Joined: May 08, 2001
Posts: 14
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).]


TANSTAAFL
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Burkhardt Hufnagel
Greenhorn

Joined: May 08, 2001
Posts: 14
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

Joined: Feb 06, 2001
Posts: 358
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Feb 06, 2001
Posts: 358
cindy i wanted to ask why does this happen with constants?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Feb 12, 2001
Posts: 1479
Mughal's certification book covers these type of details which is why its still the best certification book.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Mar 08, 2001
Posts: 192

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

Joined: Sep 29, 2000
Posts: 8521
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
 
 
subject: assign final int to byte works
 
Similar Threads
FINAL VARIABLE DECLARATION
casting doubt
Casting between int and byte
implicit casting of final int to byte ???
who can explain me this please ??