• 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

A question about primitive types?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following statement:
byte b;
b+=1;
will pass compiler,but
byte b;
b=b+1;
won't pass!
Why?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zhou,
Answer to your first code segment,
1: byte b=1;
b + = 1; // line 1
Java compiler reads the statement at line 1 as,
b = (byte)(b+1);
or in more general, Java reads,
var1 op = var2; // op is any binary operator NOT unary!!
as,
var1 = (type of var1)(var1 op var2);
As you have seen Java do this casting by it's self i.e "implicit cast".
Now answer to your second question.
Always remember that calculation in Java performs in "at least" 32 bits.Suppose,
byte b=1;
byte b= b+1; //line 1
In this case, at line 1 Java compiler will upgrade b(i.e of 8 bits) into int i.e 32 bits & then perform calculation.So, the result of calculation will also in 32 bits.And you are at line 1 trying to assing the 32 bit result in byte which is of 8 bits.So,
"explicit cast" is needed in this situatuion in order to remove compile time error.That is,
b = (byte)(b + 1);
Hope now it is clear to you,
After clearing this confusion try this code & suggest what happens..
long la = 7;
short sa = 3;
int ia = la + sa ;
Regards,
Hassan.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long la = 7;
short sa = 3;
int ia = la + sa ;
In this case la & sa will be promoted to int & the addition will be successful which will make the value of ia 10.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shrenik long is wider than int so the result of la + sa will be long and you have to explicitly cast it to int. The code you provided will not work and give a compile time error.

Originally posted by Shrenik Sakriya:
long la = 7;
short sa = 3;
int ia = la + sa ;
In this case la & sa will be promoted to int & the addition will be successful which will make the value of ia 10.


reply
    Bookmark Topic Watch Topic
  • New Topic