• 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

Serious Conversion Problem - Please HELP

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code compiles fine


because 11 (though an int) fits into byte range.

However the following code :


doesn't compile. Agreed b+7 results an int but we have assigned an int to byte in previous example.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a binary operation is applied where the operands are types byte, char, short, or int, both operands are promoted to int and the result is an int.

The compiler does not carry out the addition so it can't allow the int that results from the addition to be stored in a byte without an explicit cast.
[ April 27, 2006: Message edited by: Keith Lynn ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The least data type that can result from an arithmetic operation is an int. When you assign data types to a byte or short its different.

byte b = 10; will compile

but

byte b=3;
byte c = b+7;

and

Byte b = new Byte(10);

will not compile.

Methods and constructors also behave in this way. The least that they will accept is an int. If you want some other data type you can always cast. The exception to this rule is when you use compounded operators like ++ or +=.

b++ is valid and will compile.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, I understand it's not compiler job to carry out the addition but compiler knows that addition of a byte and an int literal will always result in an int so it can assign it in the same way it is doing like :


[ April 28, 2006: Message edited by: Sandeeep Vaid ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it can't.

The reason it can accept the int literal is that at compile-time the value of the int literal is known, and it is known that the value is compatible with a byte.

However, since the compiler does not carry out the addition, it cannot know what the value of the result will be. Therefore, it can't assign the value to a byte.

Consider this.

byte b = 1;

b = b + 1; // The compiler knows the result is an int,
// but it doesn't know if the result is
// compatible with a byte.

Notice that the following line is seen exactly the same by the compiler as the first addition.

b = b + 1000;
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot. I got it.......... Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic