• 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

About shift operator.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends check out following code;

Why does the compiler gives an error at line 8 & not on line no.7?
What is difference between them?
But both gives same o/p for int.(i.e 127 & 31)


public class Test
{
public static void main(String args[])
{
byte b = 127 ; //same for short.
System . out . println ( b) ;
b>>=2; //doesn't gives an error. line7.
// b=b>>2; //gives an error,'possible loss of precision'. line 8
System . out . println (b) ;
}

}



Bye.
Take care.
Shubha.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 7, it's a compound assignment operator. Compound assignment operator lets you add to the value without doing the explicit cast. But in straight assignments operator (line 8) as right hand side results an "int", explicit cast is needed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic