Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit and explicit casts give different results?

 
Prashanth Kayampady
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srilatha kareddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
kotha vijaybabu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
than q edisandro ..,

i got it

cheers

vijay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic