• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Primitive Casting

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this not give error ?
int i = l;
long l = 10l;
int j = i << l;
Should not the RHS expression be promoted to long and throw a casting error ?
int j = l; // this throws error !
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
???


[This message has been edited by Sahir Shah (edited December 06, 2000).]
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bharatesh,
in ur code u r assigning "l"(L) to int i. Make it 1 or whatever number u want & everything will be fine.

Aruna
[This message has been edited by Aru Ven (edited December 06, 2000).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharatesh,
Shift operators are subject to unary numeric promotion not binary; so <code>long</code> values do not force conversion of <code>int</code> values.
See JLS §5.6.1
<quote>
Each operand, separately, of a shift operator >>, >>>, or << (�15.19); therefore a long
shift distance (right operand) does not promote the value being shifted (left operand)
to long
</quote>
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the following :
int i = l;
long l = 10l;
double d = 10.0;
int j = i << l;
int k = i << d; // this is throwing error ?
Even in the above the last line should not throw error.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharatesh,
Sorry for the late reply.
The line <code>int k = i << d</code> is throwing an error because 'd' is a 'double'. Shift operators don't work with floating-poing values.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic