Forums Register Login

Casting between int and byte

+Pie Number of slices to send: Send
Hi,
When assigning an int value to a byte variable, my understanding is that we have to case explicitly as:
int i=9;
byte b= (int)i;
but when i run following piece of code(with modifier FINAL before int declaration and assignment it works fine without throwing any cast exceptions:
class Test {
public static void main(String args[])
{
final int i = 12;
byte b = i;
System.out.println(b);
}

}
Why is it so? I expected the code to throw caste exception.
+Pie Number of slices to send: Send
i wang to know the answer . wang_let@sina.com.cn
+Pie Number of slices to send: Send
provided final int i=12 being there, any sebsequent occurances of i will be substituded with 12 at compile time.
so in your case you have

which is actually the same as
+Pie Number of slices to send: Send
please also refer to this thread
+Pie Number of slices to send: Send
Hi Sumeer
A good question and the answer is that if you make a variable as final your are making it a compile time constant. So that means that the compiler can know it at compile time that the value would fit into the variable or not.
Try this
char a=1000,b=200;
char c = a-b; // cast required
this would require a cast as the result of this operation would be an int. But if i make a,b,c as final that is make the compiler sure that under no circumstances this value won't change then the compiler would be happy to it without the cast.
final char a=1000,b=200;
final char c = a-b; // no cast required
similarly consider this
char c = 1000-200; // no cast required
now consider this
final char c =1000-200; // no cast required
I hope that helps.
[ May 15, 2003: Message edited by: Anupam Sinha ]
+Pie Number of slices to send: Send
The final key word means that the value cannot be modified, therefore the java compiler is able to perform compile time checking to see if the value assigned is within the range.
Take for examle the following code, it will result in a compiler error because the final value of i is outside the range of a byte type variable.
public class test {
final int i=128;
byte b=i;
}
the error output is:
C:\temp>javac test.java
test.java:3: possible loss of precision
found : int
required: byte
byte b=i;
^
1 error
This can only be fixed with casting:
byte b = (byte)i;
+Pie Number of slices to send: Send
128 is beyond the range of byte....you will also get the same compiler error when you do the following :
byte b=128;
A cast is needed in this case, definitely.
No more fooling around. Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 992 times.
Similar Threads
primitive assignments using char : Pg193 of K & B
final variable
Java Primitive Literals
Plz explain me the answer of this code........
why I cann't assign FINAL INT to Byte
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:15:25.