When you call a function, all arguments are first evaluated before the function is called.
So, "The square of the number "+7+" is "+square(7) is evaluated before System.out.print , which prints it.
Thus, it calls the square(7) call, which then calls System.out.print(5) first, before calling System.out.print .
After square(7) returns 49, the string evaluates to "The square of the number 7 is 49" , which is then printed.
To make it even more explicit, as if you did this:
String toPrint = "The square of the number "+7+" is "+square(7); System.out.print(toPrint);
source share