java - Nested "System.out.print" output -


why program:

import java.io.*;  public class testpage {     public static void main(string [] args)     {         pri();     }      public static int p2 (int x)     {         system.out.print("p");         return x * x + 1;     }      public static void pri ( )     {         int y = 3;         system.out.print( p2(y) + "-" + p2(y));     } } 

output this:

pp10-10 

specifically, why output on each side of - different when method calls same?

java evaluate operands of binary operator such + before performing operation. means p2(y) called twice before concatenations happen. 2 method calls each print p before concatenations, system.out.print prints 10-10.

the jls, section 15.17.2, covers this:

the java programming language guarantees every operand of operator (except conditional operators &&, ||, , ? :) appears evaluated before part of operation performed.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -