• 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

what is this??? I m confused

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ankush
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a similar kind of example from Devaka's Exam....


OUTPUT : Sub1 2
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankush,

Please use code tags and indent your code properly.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Treimin Clark wrote:Ankush,

Please use code tags and indent your code properly.


sure i will
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic