File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit and explicit casts  give different results? 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 "Implicit and explicit casts  give different results?" Watch "Implicit and explicit casts  give different results?" New topic
Author

Implicit and explicit casts give different results?

Prashanth Kayampady
Greenhorn

Joined: Jan 18, 2005
Posts: 7
public class TestImplicitCast
{
public static void main(String[] args)
{
byte a =127; //1
a = (byte)(a+1);//2
byte b = 127;//3
b=+1;//4
System.out.println("a=" + a +";b=" + b);
}
}

The o/p is a=-128;b=1

In the above code I am adding 1 to 127 and I am expecting the value for a and b to be same.
The only difference here is that I am doing explicit cast at line 2 and implicit cast at line 4.
Why the o/p is not same?
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
in the above code i am notgetting why after adding 1 to b in line 4 its not giving error of loss of precision but instead giving o/p 1.

and one more thing ,
at line 1 if u change the value of byte a to 126,
then in the next line its value will be 127 which is within the range ,then why its giving loss of precision error if we remove the casting in line 2 even though its value is in the range of byte


Harish<br />SCJP 1.4 (85%)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Prashanth Kayampady:
...The only difference here is that I am doing explicit cast at line 2 and implicit cast at line 4...

b=+1; means assign positive one to b.
b+=1; means add one to b and assign the result to b.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by harish shankarnarayan:
...at line 1 if u change the value of byte a to 126, then in the next line its value will be 127 which is within the range ,then why its giving loss of precision error if we remove the casting in line 2 even though its value is in the range of byte

The cast is required unless the value is a literal that falls within the range of the narrower primitive. a+1 is not a literal.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi guys,

This issue is not so clear for me yet. Could you please evaluate the statements below and tell me why some compiles and others don't ?

byte b =(byte)127 + 1; // Cannot compile
byte b =(byte)128; // Can compile
byte b =(byte)199; // Can compile
byte b =(byte)199+1000; // Cannot compile
byte b =(byte)199+100; // Can compile
int i = 1000000000; // Can compile
int i = 1E9; // Cannot compile. Maybe, here I know why.
int i = 10000000000; // Cannot compile
int i = (int)1E10; // Can compile

I am still very confused about implicit or explicit cast for primitives (byte, short, int ...).

Any comments are welcome.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
srilatha kareddy
Ranch Hand

Joined: Jan 12, 2006
Posts: 32
byte b =(byte)127 + 1; // Cannot compile
=byte+int(which is constant) (byte range -128 to 127)exceeds range
byte b =(byte)128; // Can compile
byte
byte b =(byte)199; // Can compile
byte
byte b =(byte)199+1000; // Cannot compile
byte+int=int(explicit cast required as it exceeds byte range)
byte b =(byte)199+100; // Can compile
byte+int(which is constant)= in range of byte so complies
******* final int i=8;
******* byte b= i;//complies as i is final
int i = 1000000000; // Can compile
constant in range of int
int i = 1E9; // Cannot compile. Maybe, here I know why.
double to int cast required (explicit cast required)
int i = 10000000000; // Cannot compile
NOT in range of int
int i = (int)1E10; // Can compile
explicit cast provided so complies
[ February 21, 2006: Message edited by: srilatha reddy ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi srilatha,

I think I understood your explanation.

Whenever you assign a literal value to a byte variable, as literals are always considered as int, so if it's out of the range of byte I must explicit cast the value to a byte, right ?

So, this is the why the first code below can compile and the second one cannot.

byte b =(byte)199+1000; // Cannot compile
byte+int=int(explicit cast required as it exceeds byte range)

byte b =(byte)199+100; // Can compile
byte+int(which is constant)= in range of byte so complies


However, please let me ask you about the following code :

byte b =(byte)199+180; // Can compile

But as per you explanation above it should be :
byte+int(which is constant)= out of the range of byte so doesn't compile

byte b =(byte)199+185; // Cannot compile
The same conclusion as above, byte + int (out of the byte's range).

Could you please clarify to me ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

However, please let me ask you about the following code :

byte b =(byte)199+180; // Can compile

But as per you explanation above it should be :
byte+int(which is constant)= out of the range of byte so doesn't compile


Believe it or not... it is in range, so it does compile...

Anyway, both 199 and 180, are out of range. So either by itself will not compile.

However, 199 is explicitly casted so it is forced to be an byte. This actually overflows (or underflows, lost precision, etc. You can decide what to call it), and generates a negative number. This negative number when added to 180 will get the expression, which is a constant into range.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Henry,

Finally I got it after your explanation !

For example, the value 128 when converted to byte will be -128

129 --> -127
130 --> -126
131 --> -125 and so on ...

So, the expression byte b = (byte)199 + 180 compiles because (byte)199 is converted to -57. When adding -57 to 180 the result is 123 (a value between the byte's range).

On the other hand, the code byte b = (byte)199 + 185 doesn't compile because when adding -57 to 185 the result is 128 (Out of the byte's range).

So now I think finally I got it.

Thank you very much for all posts here.
kotha vijaybabu
Ranch Hand

Joined: Jan 28, 2006
Posts: 77
hi edisandro....,

129 --> -127
130 --> -126
131 --> -125 and so on ...


i did not get u how the above code things happen
can u explain in briefly....plz...
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi kotha,

When you perform a narrower operation with numbers, I mean, when you explicitly cast a big number forcing it to fit in a short variable (int to byte for example), java discards the left most bits to assign the new number.

So, for instance, the allowed range for byte variables is from -128 to 127.

If you try to assign a value out of a byte range, the value 128 for example, it's like the value starts from the beginning.

127 is the last allowed value for bytes.

128 is the first out of range value and becomes -128 (the first allowed value for bytes)

129 is the second out of range value and becomes -127 (the second allowed value for bytes)

130 is the third out of range value and becomes -126 (the third allowed value for bytes)

and so on ...
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66
Binary representation of int 128 in binary 00000000000000000000000010000000
when we cast this int 128 to a byte it will take the right most 8 bits
that is 10000000
in 2' complement 10000000 represents -128

similarly for int 129 i.e binary 00000000000000000000000010000001
when we cast it to byte we will get 10000001 i.e byte -127




Thanks,<br />Murali...
kotha vijaybabu
Ranch Hand

Joined: Jan 28, 2006
Posts: 77
than q edisandro ..,

i got it

cheers

vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit and explicit casts give different results?