aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting between int and byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting between int and byte" Watch "Casting between int and byte" New topic
Author

Casting between int and byte

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
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

Joined: May 15, 2003
Posts: 6
i wang to know the answer . wang_let@sina.com.cn


i love java and every one in the java ranch
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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


Meng Yi
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
please also refer to this thread


not so smart guy still curious to learn new stuff every now and then
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: May 13, 2003
Posts: 27
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;


When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting between int and byte