• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubt in primitive casting..

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i have a doubt in implicit casting.
the following code doesnt compile and all of us know the reason for that.we need explicitely cast the result(b+1) to byte.
byte b = 1;
b = b + 1;

but what about
byte b = 1;
b += 1;
This piece of code compiles..howcome??does implicit casting happens in this case??
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 1; //line 1
b = b + 1; //line 2

line 2 will evaluate to an integer (int) value of 32 bits, and knowing that a byte can only hold 8 bits, the compiler will flag an error.

b += 1;

In the preceding case, the compiler will add an implicit cast when this compound assignment operator is used.
 
Nibin Jacob Panicker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi marzo..

so in the case of b+=1 implicit casting is happening so same should be happening in the case of b++ also.

but my doubt is if java can do implicit casting for b+=1 and b++ why not for b = b + 1; ??? anyway by giving a compile time error its just making us to do the job..

thanks
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Java does implicit narrowing conversion when the right hand side of an expression is a constant or can be evaluated to be a constant during compile time.

so
byte b =1 //valid
byte b= 1+2 // valid

but byte b = b+1 // Invalid -- as it can not be evaluated during compile time

2. Above is valid only if the compile time constant is within the range of the type used.

byte b = 128; //Invalid -- compile time error

3. b+=1 is evaluated as b = (Type of b) b+1 by compiler.

So in the above case its b = (byte) b+1 which is correct since the integer value as a result of (b+1) is typecasted to byte.

Also note that :

byte b = 127;
b+=1; or b++ or ++b // Valid -- for the above reason


Hope this helps.

Regards,
Sakti
[ August 17, 2005: Message edited by: Sakti Singh ]
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but b=(byte) b + 1; is wrong
it is b+=1; -> b = (byte) (b+1);
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just noticed..isn't byte b=(byte)b+1;

return an int? if it has to be byte then..it should be byte b=(byte)(b+1);


Cecilia
 
Nibin Jacob Panicker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you buddies..but my doubt was why cant java do implicit casting for
byte b=1;
b= b+1;

why cant java compiler take it as b = (byte)(b+1) during compile time??
if it does that during compile time there shouldnt be any harm whatever the value of b would be ..rgt??
 
Cecilia Jane
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What is the difference between compile time and run time actually... If a value of a variable is already known before it runs...then it is compile time..so when u say

byte b= 1;//this is ok...coz value of b is 1 which is a constant so you dont have to say byte b=(byte)1...that is at compile time the value of b is already decided before it runs..right.

now let is c the following statement...

b=b+1;

the value of b can vary...coz it is not a final variable..and taking this statement individually...when u say b= b+1..the value of b can vary it cud be anything...so it is not a constant meaning the value of b is deduced at run time..so it throws an error at compile time..

ok lets for a moment assume that complier implitcitly casts this...so there is gonna b loss or precision right? complier wud do it even if u dont know actually what happened..but when u say b=(byte)(b+1); u r actually saying to the compiler hey...I know what am doing..even though the value is changed am aware of it. Actually this makes the program lot easier.

Cecilia
 
Sakti Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my apologies , please read it as b = (byte)(b+1)

thanks to all those who pointed this out

Regards,
Sakti
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic