• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

type conversion

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some one plz help me
look at this
byte b=2;
b += 10; //1
b = b+10; //2

1. works fine but 2. does not.

i know that we have to explicitly narrow the conversion in 2. by adding byte on RHS but but how come 1. works as both are one and the same thing.
 
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sandeep,

byte b=2;
b += 10; //1
b = b+10; //2

1 and 2 are not same thing.
1 is identical to b=(byte) b+10; works fine.
2 does not compile because b is converted to int (due to + operator) and after adding 10, the result(int) can not be assigned to byte value.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Areca ...
b=(byte)b+10;wont work ...will give loss of precision ...i think u mean to say b=(byte)(b+10)..this will work fine .....

thanks & regards

srikanth
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ha, i meaned b=(byte)(b+10) you are right.
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic