• 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

Implicit narrowing conversion

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The implic narrowing conversion using the compound operators are equivalant to explicit conversion. I would like to share the following code sample:

class Test {
public static voice main(String[] args) {
byte b = Byte.MAX_VALUE;
byte b +=1; // compiles fine
System.out.println(b); // prints -128
byte b = b + 1; // compiler error.
}
}
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b += 1;
implicitly means,
b = (Type of b) (b + 1);

Thats why it compiled without error.

b = b + 1;

In the above operation , b + 1 results int result, which can't be assigned to a byte.

Hope this helps !
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep... I just wanted to share it with you guys, because we often talk about the implict widening conversion. Hope this helps other folks who are also preparing for the exam.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I expect the last line would give compile time error

but why its compiling fine???

 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, the last line would give a compiler error. Notice the comment after the line.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i+1 --- since both the operands are int , the result will be int .
b = b+1 -- since both the operands are less than int , the result will be int .
in a binary operator , if both the operands are less than int , then the result will become int . if one of the operands is wider than int , then the result will be WIDER of the two .
byte+short also results in int .
------------
Hope its clear .
 
reply
    Bookmark Topic Watch Topic
  • New Topic