File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Satou kurinosuke i have one more question for you Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Satou kurinosuke i have one more question for you" Watch "Satou kurinosuke i have one more question for you" New topic
Author

Satou kurinosuke i have one more question for you

bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Mar 02, 2006
Posts: 176
but if in && short circuit operator if left hand operand is false then right will not be evaluated na....
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Mar 02, 2006
Posts: 176
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Mar 02, 2006
Posts: 176
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Mar 02, 2006
Posts: 176
thank you reddy.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Sorry my initial comments are not clear and I modified my comments
[ March 14, 2006: Message edited by: KJ Reddy ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thanks KJ.
Sorry bnkiran, I was not logged in anymore


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Satou kurinosuke i have one more question for you