Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

implicit casting of final int to byte ???

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please would someone offer an explanation as to why an explicit byte cast is not required for a final int value in the following code?

I have tried replacing int with short, char and long but in these examples an explicit cast was required.
Similarly, final int was implicitly casted to types short and char without compile/runtime error.
Why is final int able to get away without casting?
This contradicts with the text book rules of casting.
chung
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do u agree that this is legal?
byte b=5;
this works right? because although 5 is an int it gets casted automatically when u use = to a byte.
(the only case it does).
when u declare an int final it is the same for the comoiler as writing a constant like 5 (cause final makes it a constant too)!
so
final int x=5;
byte b=x;
works the same as the example before (casting happens automatically).
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
roy: that's not the point.
it does not work for final short i = 5;
or char as well ?
why ? you need a cast now ? why ???

Originally posted by Roy Ben Ami:
do u agree that this is legal?
byte b=5;
this works right? because although 5 is an int it gets casted automatically when u use = to a byte.
(the only case it does).
when u declare an int final it is the same for the comoiler as writing a constant like 5 (cause final makes it a constant too)!
so
final int x=5;
byte b=x;
works the same as the example before (casting happens automatically).

 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the rule is:
u always need to cast if it a narrowing cast (ie from short to byte, int to byte, short to byte, long to int etc...) BUT in the case u need to put a constant number in a byte a short or a char etc u dont need to cast , the compiler does that for u.
for example:
byte b=10; (10 is an int but this works!)
short s=20; (20 is an int but this works too!)
etc...
instead of just wring a constant numbers (int) like 5 and 10 and 20, you could define a final int. so the complier while compiling changes the place of the variable final int, to a regular int number.
so this works too:
final int x=10;
byte b=x;
short s=x;
char s=x;
all those works!
but it wont work for shorts etc like:
final short x=10;
byte b=x; //wont work!!!
cause as stated above it only works for constant int.
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks Roy, I fully understand your points.
chung
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seems to work fine for me. (In case you're wondering, I'm using JDK 1.2.2.)
When you're assigning a literal value to a variable, the compiler checks to see if that value is within the range of the variable you're assigning to. If the value is within the range, an implicit cast is done and life goes on happily. If the value is outside of the range, an error is generated from the compiler, like this:

When you're trying to assign to a variable from another variable, however, the compiler can't determine what the value of that variable will be at run-time and, therefore, always requires a cast, like this:

However, if you make the variable that you're assigning from a final variable, then the compiler knows ahead of time what that value will be at runtime and can go back to checking to see if the value will fit in the variable to be assigned to, like this:

I don't see why this would be any different whether you're assigning from a short, a char, an int, or a long. However, when I tried compiling this code, I got errors on the following lines:

So, why does this work for int and short, but not for char or long? That doesn't seem to make any sense.
Thanks,
Corey
[ January 24, 2002: Message edited by: Corey McGlone ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like i said in my post before (read 2 above your post). the compiler only accepts a narrowing assignment without casting if the number is a constant int in an assignment.
byte b=10; (10 is an int)
u explained yourself very well that instead of writing 10 u could use a final int which is the same for the compiler (ie. final int x=10 ).
it doesnt work for short chat etc because thats the way the compiler works. narrowing conversions always require cast excepet from constant int (literal or final). thats the rule to follow!
the rest (short to byte , long to int, char to byte) require casting (final or no final).
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey,
I'm using JDK 1.2.2 on an NT machine and my compiler complains about the following piece of code:
final short x = 10;
byte b = x;
/*
Incompatible type for declaration. Explicit cast needed to convert short to byte.
*/
I fully agree with what you are saying Corey and what from what Roy has said does shed some light on this "grey" area of Java.
Although I haven't found any thing in the Java bible (JLS) with respect to final primitive casting, I am happy for the time being (and for the sake of my sanity) to accept Roy's statement:


u always need to cast if it a narrowing cast (ie from short to byte, int to byte, short to byte, long to int etc...) BUT in the case u need to put a constant number in a byte a short or a char etc u dont need to cast , the compiler does that for u.

 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually chang, as a result from your posts and
Corey post i came to doubt myself
this is what i got for the jls:
jls for assignemnt conversions
and according to the jls this supposed to work for int, short char and byte!!! not just short and int like corey example shows!
this is truly weird.......
im stumped as well guys.
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm now going home before I start to lose my sanity.
If anyone would like to have a stab at this then please go ahead. There's no shame in getting things wrong (honest!).
I just hope that this type of question doesn't appear in my exam 59 times.
chung
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gurus, bartenders, sheriffs, mayors etc etc please help.....

Originally posted by chung lee:
OK, I'm now going home before I start to lose my sanity.
If anyone would like to have a stab at this then please go ahead. There's no shame in getting things wrong (honest!).
I just hope that this type of question doesn't appear in my exam 59 times.
chung

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roy Ben Ami:
and according to the jls this supposed to work for int, short char and byte!!! not just short and int like corey example shows!
this is truly weird.......
im stumped as well guys.


Might this be a compiler issue? I'll have to try it on my home computer as I'm using JDK 1.3.1 there and perhaps a different version of the javac compiler will behave differently. Has anyone tried this with any other compilers (i.e. different versions of javac, jikes, etc.)? I'm interested to see if all compilers behave the same way or not. It would appear that they don't as, what compiles on my machine doesn't on chung's.
Stumped, :roll:
Corey
[ January 24, 2002: Message edited by: Corey McGlone ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it Corey Chung (and Mark!)
it works exactly like the jls says it would in my compiler jdk 1.4.0 beta

this works:
final int x=10;
byte b=x;
and this works too:
final char x=10;
byte b=x;
and this too!
final short x=10;
byte b=x;
jls is right again!
it will work for final int char short or byte when assigned to a byte char or short(assuming it is in the range).
TADA! riddle solved!
and btw , LOL chung i hope u wont get it 59 times!

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

TADA! riddle solved!


So all this over a compiler bug, huh? :roll:
At least it's solved now - thanks, Roy.
Corey
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chung,
the JLS spec regarding assignments changed between JDK 1.1 and 1.2...the second edition of the JLS allows for implicit narrowing converstions when assigning a constant expression to a variable of type bte, short, or char.
So pre-1.2 JDK, the behaviour your are seeing with your compiler is correct; post 1.2, the behaviour described in JSL 2nd Edition is correct.
I'm curious, since you have the 1.2.2 JDK...what happens when you write...
short s = 12;
Does this compile? I would expect you would get a compiler error with an older version of javac, based on an earlier JSL spec.
Rob
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im a little confused about these posts:
Regarding casting not required for ints only Rob stated first this wont compile

Then in the last post, all of the sudden this is legal. Whats up with that. What is the actual rule regarding ALL of the primitive types, excluding boolean. I'd appreciate it, cause I know a question like this is going to be on the test.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read my last post (the one before this one)
it is the correct one.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roy Ben
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the case where they arent final? Then the previous rules apply, dont need casting for ints only?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope.
if they arent final u always need casting for narrowing.
the only case u dont need casting is if you try to use assigment of a number (like 10 5 ) or a final int , char , short or byte and assign it to:
byte short or char (assimg it is in the allowed range).
just int wont cut it:
int x=5;
byte b=x; //wont compile!!
this will:
byte b=5;
and this :
final int x=5;
byte b=x;
or:
final short x=5;
byte b=x;
etc etc.. like i mentioned in the aboved posts.
u can recheck the jls for the complete round up.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Salerno:
Im a little confused about these posts:
Regarding casting not required for ints only Rob stated first this wont compile

Then in the last post, all of the sudden this is legal. Whats up with that. What is the actual rule regarding ALL of the primitive types, excluding boolean. I'd appreciate it, cause I know a question like this is going to be on the test.



I don't believe I ever posted this code above, please provide the link!! I think I wrote something like that but without the final modifier. The final modifier changes the nature of the assignment to a compile time constant.
These are two very different pieces of code regarding assignment conversion:
short x=10;
byte b=x; //wont work!!!
final short x=10;
byte b=x; //will work

Rob
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the implicit narrowing primitives convertion works in 1.3 and 1.1, but not in 1.2. There are three conditions that must be met in order for the code to get compiled.
look at this example:
final int i=10;
byte a=i;
1)expression is constant. i here is constant.
2)variable type must be of char, byte, or short.
3) the value of the expression is representable in the data type of the variable.
Example,
final int i=100000;
byte a=i;
This portion of the program wont compile because 100000 is not representable by the byte variable.
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A big THANK YOU for all your help guys and gals. I think Derek has summarised it very nicely.
chung
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does this rule not work for long?
final byte b=1;
final short s=1;
final char c=1;
final int i=1;
final long l=1;

byte bb;
bb=b;//work
bb=s;//work
bb=c;//work
bb=i;//work
bb=l;//not work??? compiler error
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cause it is not supposed to work for long.
read the jls (there is a link in the posts ).
 
reply
    Bookmark Topic Watch Topic
  • New Topic