• 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

Operators

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how this code gives a particular answer. Can any one help me figuring out the answer.



class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}


The answer is
BFCTAT
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First off all your display name is not correct please change it before the bartender catches you.

Now comming to the question.

This is result is because of Short Circuit OR ( || ) operator.
First the call to m1("B",false) gets executed, it returns false,
Next the call to m1("C",true)gets executed it returns true,
So the next call m1("D",false) wont get executed.
Now the main m1 ( .... ) will get executed hence the result.
[ June 16, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
Hi,
First off all your display name is not correct please change it before the bartender catches you.



Exactly. "sri m", please change your displayed name via the "your profile" link. We require a display name in the format <first name><space><family name>, preferably your real names.

Our JavaRanch Naming Policy explains in more detail.

Thanks
-Barry
 
srinivas m
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the wonderful replies. I appreciate you guys for the prompt response.
 
reply
    Bookmark Topic Watch Topic
  • New Topic