• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
Happily living in the valley of the dried frogs with a few tiny ads.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic