• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Diff between two

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Why the line2 is giving compile time error.But why not line1



Thanks,scjp1.4
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the first constant can fit into an int.

0x80 = 128 which is too large to fit into a byte.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
but ox80000000=2147483648,then how is it possible.Doesn't it requires explicit cast
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi amulya:
Hi,
but ox80000000=2147483648,then how is it possible.Doesn't it requires explicit cast



See this,

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi amulya:
... but ox80000000=2147483648...


Yes, if converted directly to base 10.

But the binary representation of 0x80000000 is a one followed by 31 zeros, and that value assigned to a Java int is...
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Laxmi,

but ox80000000=2147483648,then how is it possible.Doesn't it requires explicit cast


There is not precision loss as such in this case the literal is already occupying all the 32 bits designated for integar literals.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi runchers,
i also want to focous on

In above code there is no error or fault.Actually , In java every operation is happened implicitly on 32 bit register means in int type.
So in above 0x80000000, compiler will treat it as (int)0x80000000.if there be any suffix like L or l then we have to cast it.

thanks,
Prashant Kumar Singh
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic