Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on Operators in Dan Chisholm's site

 
sriRam gaddaM
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • 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 ]
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic