• 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

Q on Operators in Dan Chisholm's site

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ppl,

this is a question on operators:

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}


What will be the output of the program:
the answer is given as true,false, false.

but before applying ||,&& the assignment expressions
in () shud be executed. what do ya say? then the output
should be true, true, true. But, i executed the program
it displays true, false, false.


can someone give me the logical explanation for this?


Sriram.
 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sriram,

your name 'IGNORAMUS' does not confirm with the name policy here at JavaRanch.

Your boolean a, b and c are all false by default. Since you use the short circuit logical operator || only the left side is evaluated. So a = true, b and c remain false. Whenever the left side is true the right will not be evaluated. That is the speciality of the short circuit logical operator ||.

Regards,
Darya
[ February 18, 2005: Message edited by: Darya Akbari ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow the link give below, the same question has been dicsussed before
here
 
sriRam gaddaM
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


i have got a small explanation for it. can u guys comment on it.
&&, ||, ?: are always evaluated from left to right irrespective of parentheses. but if you take the case of (a=2) * (b=3) * (c=4)
a,b,c will have 2,3,4. ain't it?

But, i got one more doutb here. && has more precedence than || so, it shud
be evaluated first before ||. But, it is not happening here?


P.S: Darya, Didn't get when u said my Uname doesn't conform with
the naming policy.
can u throw some light on that? (haven't read the policy in
detail... ))


Sriram.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& and || are Short Circuit operators.
Did u go thru the link i mentioned in my previous reply. Its clearly mentioned abt the usage of these Short Circuit operators.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sriram,

there is somewhere a link on JavaRanch about name policy let me look for it...

Here it is: name policy
 
sriRam gaddaM
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,


i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.

Sriram.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by IGNORAMUS:
hi guys,


i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.

Sriram.



Guess again. We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed. You can edit your display name here. Don't take too long - accounts with invalid display names get deleted.

thanks,
Dave.
Moderator.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wud request the forum mods to let me use it.



I don't know who wud is, but nope you cannot use that displayed name.
 
reply
    Bookmark Topic Watch Topic
  • New Topic