Reputation: 23
I have created two classes called clz1(main) & clz2.
clz1:
public class clz1 {
public static void main(String[]args){
clz2 clz2object=new clz2();
clz2object.display();
}
...
}
clz2:
import java.util.Scanner;
public class clz2 {
input=new Scanner(System.in);
public String print1(){
System.out.print("What is your name:");
String a=input.next();
return a;
}
public String print2(){
System.out.print("What is your girlfriend's name:");
String b=input.next();
return b;
}
public void display(){
System.out.println("Your name is "+print1()+" & your girlfriend's name is "+print2());
}
}
and the output is like this:
What is your name:aaa
What is your girlfriend's name:www
Your name is aaa & your girlfriend's name is www
how could this be possible? I didn't call print1()
and print2()
... first this should print "your name is" and print1() ....then "your girlfriend's name is" and print2() ...isn't it?
Upvotes: 1
Views: 149
Reputation: 40336
Your output makes sense. You did call print1()
and print2()
. You called them in this line:
System.out.println("Your name is "+print1()+" & your girlfriend's name is "+print2());
When that line executes, the effect is: first print1()
is called, then print2()
, then their return values (the user input) are used to build the longer string, then that string is passed to System.out.println()
which prints the whole thing.
The reason this happens is because the string parameter must be fully built before it is passed to a method like System.out.println()
-- this is the way Java works. So everything that needs to happen in order to fully build that string happens before the string is sent to System.out.println()
.
If the order of operations here confuses you, there are two ways to think about it. One is the above can be rewritten to look like this:
String s = "Your name is " + print1() + " & your girlfriend's name is " + print2();
System.out.println(s);
Which can be broken down even more into:
String s1 = print1();
String s2 = print2();
String s = "Your name is " + s1 + " & your girlfriend's name is " + s2;
System.out.println(s);
And if you look at that, the order is very clear. That is essentially what is happening, you just took a shortcut by specifying everything in one statement.
The second way to think about it is more conceptual: In order for you to be able to build the final output string and pass it to System.out.println()
, you have to know what the return values of print1()
and print2()
are, otherwise you can't build the string! Therefore, they have to be executed first.
If you want to get technical, this behavior is described in a few places in the Java Language Specification. From Section 8.4.1:
When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.
In other words, Java will fully build your string before System.out.println()
does its work -- and in your case, that requires calling your print methods.
Upvotes: 3
Reputation: 209004
When you call display, display calls print1 and print2
display();
public void display(){
System.out.println("Your name is "+print1()+" & your girlfriend's name is "+print2());
^^^ ^^^
}
Before the method returns anything, The actions of print1 will be performed
public String print1(){
System.out.print("What is your name:"); // perform before return
String a=input.next();
return a;
}
So before anything is returned from print1 and print2, their actions are performed.
Steps for System.out.println("Your name is "+print1()+" & your girlfriend's name is "+print2());
1) perform
print1
return value toprintln
; cannot print line untilprint2
is completed2) perform
print2
return value toprintln
3) print line with return values of
print1
andprint2
So ultimately, println
cannot perform because it's performance is based on the calls to other methods. Once those methods complete their actions, then println
can perform.
Upvotes: 0
Reputation: 728
System.out.println("Your name is "+print1()+" & your girlfriend's name is "+print2());
You did call print1() and print2(). Java process code in your bracket first, then print it. It pretty much same like:
System.out.println(1+1);
It process '1+1' first, then print it later on.
Upvotes: 3