• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Output ????

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

class EBH202 {
static boolean m1(boolean b){
System.err.println(b+",");
return b;
}

static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = m1(true)) ||( (b = m1(true)) && (c = m1(false)));
System.out.print(a + "," + b + "," + c);

}
}

---------- intepreter ----------
true,
true,false,false
Output completed (0 sec consumed) - Normal Termination

i am geting this output .
I put the paranthesis to change the priority
I thot
( (b = m1(true)) && (c = m1(false)) this will excute first
Pls explain
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets see how it works :

static boolean a, b, c;

a = false, b = flase , c = false

boolean x = (a = m1(true)) ||( (b = m1(true)) && (c = m1(false)));

in this since this is a short circuit operator only a = m1(true)
once this comes as true rest of them are not executed.

so for (a = m1(true)) it prints true

System.out.print(a + "," + b + "," + c);

now this will give true, flase, flase - as b and c never got changed
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But
( (b = m1(true)) && (c = m1(false))

have higher priority bcz it is in paranthesis ()
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But
( (b = m1(true)) && (c = m1(false))

have higher priority bcz it is in paranthesis ()


Even if you put 10 more brackets it will not execute.because it is still right side operand for || in true ||(())
[ September 13, 2007: Message edited by: raj malhotra ]
 
Abhishek khare
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean x = (a = m1(true)) ||( (b = m1(true)) && (c = m1(false)));

see with a short circuit operator if the first one is true (a = m1(true)) then it wont evaluate anything thats written beyond "||". so they will never be executed.

See this from K&B
*****************
"The short-circuit feature of the && operator is so named because it doesn't waste its time on pointless evaluations. A short-circuit && evaluates the left side of the operation first (operand one), and if it resolves to false, the && operator doesn't bother looking at the right side of the expression (operand two) since the && operator already knows that the complete expression can't possibly be true."

"The | | operator is similar to the && operator, except that it evaluates to true if EITHER of the operands is true. If the first operand in an OR operation is true, the result will be true, so the short-circuit | | doesn't waste time looking at the right side of the equation. If the first operand is false, however, the short-circuit | | has to evaluate the second operand to see if the result of the OR operation will be true or false. "
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You mean both short circuit (&&,||) and (&,|) operate from left to right.
Don't consider the priority???
 
If you're gonna buy things, buy this thing and I get a fat kickback:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic