Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is this??? I m confused

 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually my first post, so hello to all
I m preparing for SCJP 1.6, and i was going all fine until i tried this question
can sumbody explain me the solution
code 1:


public class prac1 {
public static void main(String [] args){
if(a()|b()&c())
System.out.println("pass");
else System.out.println("fail");
}
static boolean b(){
System.out.println("b");
return true;
}
static boolean a(){
System.out.println("a");
return true;
}
static boolean c(){
System.out.println("c");
return false;
}

}

output:
a
b
c
pass

 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when i changed the condition to a|b&&c it gave me a different answer, which actually makes sense
code 2:
public class prac1 {
public static void main(String [] args){
if(a()|b()&&c())
System.out.println("pass");
else System.out.println("fail");
}
static boolean b(){
System.out.println("b");
return true;
}
static boolean a(){
System.out.println("a");
return true;
}
static boolean c(){
System.out.println("c");
return false;
}

}

output:
a
b
c
fail

But what happended in code 1? can sumbody exlain?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankush! Welcome to javaranch.

Ankush we have a rule here that when you post a question, you have to QuoteYourSources. So please tell us from where did you get this question so that we can help you...
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Hi Ankush! Welcome to javaranch.

Ankush we have a rule here that when you post a question, you have to QuoteYourSources. So please tell us from where did you get this question so that we can help you...


I tried it myself.
If somebody had posted this question elsewhere then i dont know.
If not then source is mine
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true) && (false) )

I hope this clears
and see what other will say about it

[Reason for Editing of text : extra "|" in second if at the end of true keyword deleted]
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:
In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true|) && (false) )

I hope this clears
and see what other will say about it


when it evaluates the condition (a|b&c)
it gives the output a,b,c so it follows left-right associativity
and no precedence
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankush Baveja wrote:This is actually my first post, so hello to all

output:
a
b
c
pass



output shows what m talking about
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my knowledge in java expression evaluates from left to right and also operator precedence matters a lot
so don't think "no operator precedence"
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:As per my knowledge in java expression evaluates from left to right and also operator precedence matters a lot
so don't think "no operator precedence"

yeah i know it matters
i was just reffering to the problem in hand
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kulkarni wrote:
In First case if statement evaluates to following order:
if( true | (true&false) )
In Second case if statement evaluates to following order:
if( (true|true) && (false) )

I hope this clears
and see what other will say about it

[Reason for Editing of text : extra "|" in second if at the end of true keyword deleted]

I think you are right. i was confused a bit on the order of execution of the functions
i thought operands with high precendence operators are evaluated first .

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
 
Rohan Amin
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a debugger and you will see that the functions are evaluated first. So it prints out a,b,c and then it evaluates the if condition which by the way has
(true|true&false). Referring to operator precedence true & false is false and true | false is true so it prints out pass.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a similar kind of example from Devaka's Exam....


OUTPUT : Sub1 2
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankush,

Please use code tags and indent your code properly.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on the SCJP 6 exam you will need to understand logical operators (short circuit and not), but you will NOT have to remember the kinds of precedence rules being discussed in this thread. In the older exams there were precedence questions like this, but these have been removed.

hth,

Bert
 
teja dharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankush Baveja wrote:This is actually my first post, so hello to all
I m preparing for SCJP 1.6, and i was going all fine until i tried this question
can sumbody explain me the solution
code 1:


public class prac1 {
public static void main(String [] args){
if(a()|b()&c())
System.out.println("pass");
else System.out.println("fail");
}
static boolean b(){
System.out.println("b");
return true;
}
static boolean a(){
System.out.println("a");
return true;
}
static boolean c(){
System.out.println("c");
return false;
}

}

output:
a
b
c
pass

hi ankush this is teja in boolean conditional expression evaluation the com[iler treats the above a()|b()&c() expression
aV(b^c)=(aVb)^(aVc)=(t)^(t)=t
the other expression a()|b()&&c() can be treated as (aVb)^c=(a^c)V(b^c)=(f)V(f)=f
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Amin wrote:Use a debugger and you will see that the functions are evaluated first. So it prints out a,b,c and then it evaluates the if condition which by the way has
(true|true&false). Referring to operator precedence true & false is false and true | false is true so it prints out pass.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

Thanks for the usefull suggestion, i dont have a debugger can you suggest one. M using eclipse
 
atif aslam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Treimin Clark wrote:Ankush,

Please use code tags and indent your code properly.

sure i will
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic