• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

a simple one!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) byte b = 127 +1; //it gives error becos, okay as it's out of range of byte.
b) int i = 2147483648; //gives error, which is quite okay as it's out of range.
c) int i = 2147483647 + 1; then it compiles well, why?
pl explain c) and what will be stored in i in this case? i think there is no difference between b and c.
ashok.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best guess is ...
In the expression byte b = 127 + 1; , the right hand side (source type) gets upgraded to an int and since the destination is a byte and there is no explicit casting, the compiler flags an error. Try byte b = (byte) (127 + 1); ... It'll work fine.
The mechanics of the second expression is pretty obvious. The value of the source does not fit into the destination type. The compiler need not evaluate any expression to determine this. And that's exactly why it flags an error.
Coming to the third, expressions are not evaluated at compile time. The compiler just checks whether the source type can fit into the destination type. It does so in this case which satisfies the compiler. When the expression is actually evaluated, the integer cup overflows and consequently, the odometer is set to the maximum negative integer, -2147483648.
Do note that source of an assignment operator must be atleast of the type int to be executed. Anything below int will be upgraded automatically. To work with types lower than int, an explicit cast is necessary.
Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 13, 2001).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gururaj,
It did not work with casting also so had 2 modify the code a little
public class OverFlow
{
public static void main(String[] args)
{
int i = 2147483647 + 1;
int j = 127+1;
byte b = (byte) j;
System.out.println(i+" "+j+" "+b);
}
}
results in

-2147483648 128 -128
Tool completed successfully
as expected but byte b = (byte)127+1 gives compiler error.

Originally posted by Shyamsundar Gururaj:
My best guess is ...
In the expression [b]byte b = 127 + 1;
, the right hand side (source) gets upgraded to an int and since the destination is a byte and there is no explicit casting, the compiler flags an error. Try byte b = (byte) (127 + 1); ... It'll work fine.
The second expression is pretty obvious. The source does not fit into the destination.
Coming to the third, expressions are not evaluated at compile time. The compiler just checks whether the source can fit into the destination. It does so in this case and the compiler is satisfied. When the expression is actually evaluated, the integer cup overflows and consequently, the odometer is set to the maximum negative integer, -2147483648.

Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 13, 2001).][/B]


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhatia,
Please take a close look at the syntax for the cast i had specified. It is byte b = (byte) (127 + 1) and not
byte b = (byte) 127 + 1. Notice that (127 + 1) MUST be within brackets.
Look at this code...



Hope this helps
Shyam

[This message has been edited by Shyamsundar Gururaj (edited September 13, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Gurujaj
>In the expression byte b = 127 + 1; , the right hand side >(source >type) gets upgraded to an int and since the destination >is a byte >and there is no explicit casting, the compiler flags >an error. Try >byte b = (byte) (127 + 1); ... It'll work fine.
I think this is evaluated to int 128 and thus it is not possible the especial narrowing primitive convertion given in JLS 5.2
If it were as you say why int i = 126 + 1 compiles ?

>Coming to the third, expressions are not evaluated at compile >time. The compiler just checks whether the source type can fit >into the destination type.
I din't know that. It is useful. But don't you think that in the previous example the compiler needs to know the value of the RHS.
Besides:
Constant expressions as described in JLS 15.28 must be evaluated at compile time. Otherwise they couln't be used as case values in the switch statement.
What do you think?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic