• 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

which lines of code will compile without erroe

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following lines of code will compile without error
1)
int i=0;
if(i) {
System.out.println("Hello");
}
2)
boolean b=true;
boolean b2=true;
if(b==b2) {
System.out.println("So true");
}
3)
int i=1;
int j=2;
if(i==1| | j==2)
System.out.println("OK");
4)
int i=1;
int j=2;
if(i==1 &| j==2)
System.out.println("OK");
According to me answer is 2 and 4 but when I checked the answer it is 2 and 3. I think one can use short circuit operator only with boolean values. Am I right?
Thanks in advance
Ashu
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers are 2 and 3. Number 4 doesn't have a valid operator. AS far as I know &| doesn't exist in java. You are right, the short circuit operator can only be used with boolean values. The == operator produces a boolean result so the short circuit operator | | works fine in option 3.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ashu s
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
According to me answer is 2 and 4 but when I checked the answer it is 2 and 3. I think one can use short circuit operator only with boolean values. Am I right?
Thanks in advance
Ashu
</code>
How can u think abt. the option 4 .There is no operator like &|
in java .
thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic