Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting between int and byte

 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
wang weixin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wang to know the answer . wang_let@sina.com.cn
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please also refer to this thread
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajinder Yadav
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic