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";
    }
}
slip stream
slipstream stream
stream slip stream
slipstream slip 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 2 of 3.

Anand H.R said:   1 decade ago
System.out.print()-it will print the message in the same line.

where as

System.out.println()-it will print the message in the next line.

Sriharshaa said:   1 decade ago
In the case of slip stream(string), why is that the change of s1 in stringfix() not reflected on to the s1 in start() ?

Krutibashrath said:   1 decade ago
Yaa fix() method is entered, strat()s s1 fix ok iT this oll things proopralis object but i want what can do you ?

Suman said:   1 decade ago
How is the slip coming between slipstream and stream? I don't understand. Please explain anyone in details?

Sridhar Salluri said:   1 decade ago
It will not change the value of s1, it will create a new object of that string. So string immutable.

Remya said:   1 decade ago
In the case of array, if fix method refers to same element, how it changes in the case of boolean?

Shashanka said:   1 decade ago
I am not getting how to calculated this type of questions please any one can explain me detailly.

Nandinisharma said:   9 years ago
s1 = s1 + "stream"; we override the rid stored in s1 then why it is still pointing slip?

Monish said:   9 years ago
Why is slip coming in between slipstream and stream?

Can anyone answer this question?

Neva said:   1 decade ago
@Remya : Remember array is an Object in Java whereas Boolean is just a variable.


Post your comments here:

Your comments will be displayed after verification.