Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 3)
3.
What will be the output of the program?
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
Answer: Option
Explanation:
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".
Discussion:
30 comments Page 1 of 3.
Clinton g said:
10 months ago
I got it now, clearly.
Mohanapriya said:
6 years ago
Thank you all.
(1)
Subhashini said:
6 years ago
Thank you @Siddu.
(1)
Likki said:
9 years ago
Thanks you all for giving the explanation.
Siddu said:
9 years ago
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
(1)
Siddu said:
9 years ago
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
Siddu said:
9 years ago
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.
Nandinisharma said:
9 years ago
s1 = s1 + "stream"; we override the rid stored in s1 then why it is still pointing slip?
Poorani joe said:
9 years ago
In Java, the string is immutable that is we can't change the value.
Explantion:
s1 = s1 + "stream";
So, the local object is created s1 = slipstream, The first s1 value is slip.
return "stream";
The value is return to s2.
Explantion:
s1 = s1 + "stream";
So, the local object is created s1 = slipstream, The first s1 value is slip.
return "stream";
The value is return to s2.
Monish said:
9 years ago
Why is slip coming in between slipstream and stream?
Can anyone answer this question?
Can anyone answer this question?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers