| Author |
Implicite casting from int to byte
|
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
Local variables:
final int a = 10;
byte b = a;
That works fine.
But:
final long a = 10;
byte b = a;
Doesn't work. Why?
|
 |
Sridhar Gudipalli
Ranch Hand
Joined: Nov 02, 2005
Posts: 120
|
|
Interesting...
1) final int aInt=127;
2) byte aByte=aInt;
If aInt value < -128 or >127, then the 2nd line gives a compilation error.
I have referred the below link:
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25363
But still I am confused. Can anyone please explain?
Thanks for your time.
|
Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
If aInt is <-128 or >127 it is out of the range of a byte. The only way to assign something <-128 or >127 to a byte is to use an explicit cast, and then you will be losing precision.
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Sridhar Gudipalli
Ranch Hand
Joined: Nov 02, 2005
Posts: 120
|
|
Smith,
Thankyou for your quick reply.
I understood the range issue.
But the below code gives the compilation error:
final long aLong=120;
byte aByte=aLong;
Here aLong value 120 is in byte range only!! Still it needs explicit casting to narrow. Is it due to bit representation?
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
Sridhar Gudipalli wrote:Smith,
Thankyou for your quick reply.
I understood the range issue.
But the below code gives the compilation error:
final long aLong=120;
byte aByte=aLong;
Here aLong value 120 is in byte range only!! Still it needs explicit casting to narrow. Is it due to bit representation?
To be honest, I'm not sure. I've been watching this thread to see if there is an answer too .
|
 |
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
In my opinion the answer lies in the bit representation. Does anyone know the correct answer? It's obvious that ranges <-128 and >127 need explicite casting. But what about the type of long?
final long a = 10; //the compiler knows that the value can not be changed
byte b = a; //compiler error
It must be connected with the bit representation.
What do you think about it?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
This is actually specificially mentioned in the JLS, section 5.2 (assignment conversions).
In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char or int :
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
Basically, the compile time constant, check if it fits, and allow the assigment, rule doesn't apply to long. Or any other primative type not mentioned in that quote.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
It must be connected with the bit representation.
What do you think about it?
The bit represention of byte, short, int, and long, is two's complement. To widen any type, you do a sign extension. To narrow, you truncate. The rules are the same going from any of the four to any other of the four types.
Henry
|
 |
Sridhar Gudipalli
Ranch Hand
Joined: Nov 02, 2005
Posts: 120
|
|
|
Got it. Thank you Henry!!
|
 |
Lukas Sieradzki
Ranch Hand
Joined: Jun 09, 2009
Posts: 32
|
|
OK. But on the other hand it isn't logical. Why did the Java's designers go that way?
long is something special? I cannot understand that.
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
Mason Storm wrote:OK. But on the other hand it isn't logical. Why did the Java's designers go that way?
long is something special? I cannot understand that.
I suspect it has something to do with the size of a long. If you have a value that requires a long I would say the odds that you would need to cast it to a byte would be small, or, if your value is small enough, it should be an int.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
"Mason Storm", please check your private messages for an important administrative matter.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: Implicite casting from int to byte
|
|
|