• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Satou kurinosuke i have one more question for you

 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean y= (a1=false)&&(a2=true)||(a3=true);
accordingly this should return false na
i.e y=false a1 false a2 false a3 false if a1 a2 a3 are static fields in a class but it is printing true false false true.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bnkiran kumar:
boolean y= (a1=false)&&(a2=true)||(a3=true);
accordingly this should return false na
i.e y=false a1 false a2 false a3 false if a1 a2 a3 are static fields in a class but it is printing true false false true.


y is true because
the following line:
y= (a1=false)&&(a2=true)||(a3=true); =>
y= ((a1=false)&&(a2=true))||(a3=true);

is equal to y = ((false) && (true)) ||(true);=>
y = false || true => y = true

Edited to correct the values.
[ March 14, 2006: Message edited by: KJ Reddy ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if in && short circuit operator if left hand operand is false then right will not be evaluated na....
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct but "&&" operator do not stop evaluating "||" operator and in Java the expression will be evaluated from left. So first
(a2=true)||(a3=true) will be evaluate which returns true.
Then (a1=false)&& true will be evaluated which returns false.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry reddy i did get you,
actually in the question b=(x=true)||(y=true)&&(z=true);
in this || is preventing y and z from evaluating as it is a shortcircuit operator(for || if lefhand operand is true and for && if left hand operand is false, then right will not be evaluated na)


as for above b=(x=true)||((y=true)&&(z=true));so answer is justified,for my quesion which asked previously ,result should be false na as left hand operand itself is false!!!
plz reply.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I am not getting you. Can you post your entire code so that I can understand what you are trying to do.

y is flase in your question.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}}

after seeing this i think you get an idea of my original question i asked i.e is x=(a=false)&&(b=true)||(c=true);
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above program will print true, true, true.


boolean y= (a1=false)&&(a2=true)||(a3=true);
If you analyse above line,

move from left to right
boolean y= (a1=false)&&(a2=true)||(a3=true);
boolean y= ((a1=false)&&(a2=true))||(a3=true);
boolean y= false ||(a3=true); => a1 = false, a2 = false as a2 is not getting evaluated becaue of && and it retains orginal value.
boolean y= true => a3 = true;

I assume that a1, a2, a3 values are false initially
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you reddy.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my initial comments are not clear and I modified my comments
[ March 14, 2006: Message edited by: KJ Reddy ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks KJ.
Sorry bnkiran, I was not logged in anymore
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic