Java Programming - Java.lang Class - Discussion

21. 

What will be the output of the program?

public class Test138 
{ 
    public static void stringReplace (String text) 
    {
        text = text.replace ('j' , 'c'); /* Line 5 */
    } 
    public static void bufferReplace (StringBuffer text) 
    { 
        text = text.append ("c");  /* Line 9 */
    } 
    public static void main (String args[]) 
    { 
        String textString = new String ("java"); 
        StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
        stringReplace(textString); 
        bufferReplace(textBuffer); 
        System.out.println (textString + textBuffer); 
    } 
}

[A]. java
[B]. javac
[C]. javajavac
[D]. Compile error

Answer: Option C

Explanation:

A string is immutable, it cannot be changed, that's the reason for the StringBuffer class. The stringReplace method does not change the string declared on line 14, so this remains set to "java".

Method parameters are always passed by value - a copy is passed into the method - if the copy changes, the original remains intact, line 5 changes the reference i.e. text points to a new String object, however this is lost when the method completes. The textBuffer is a StringBuffer so it can be changed.

This change is carried out on line 9, so "java" becomes "javac", the text reference on line 9 remains unchanged. This gives us the output of "javajavac"


Amit Singh said: (May 6, 2011)  
Why javajavac why not javac. Please ellaborate.

John said: (Jul 21, 2011)  
I just muted your immutable string:

String textString = new String ("java");
textString = textString.replace ('j' , 'c'); /* Line 5 */
System.out.println(textString);

This gives: cava

Sachin said: (Mar 18, 2013)  
@John u r right !

But this the reason why answer C is true:

Method parameters are always passed by value - a copy is passed into the method - if the copy changes, the original remains intact, line 5 changes the reference i.e. text points to a new String object, however this is lost when the method completes. The textBuffer is a StringBuffer so it can be changed.

so if you try whole above program yourself,then it will give you output "javajavac"..So try it and enjoy.

Jack said: (Apr 30, 2014)  
"Method parameters are always passed by value - a copy is passed into the method - ".

The should be Method parameters are always passed by reference for the reference type like string and other objects. For the primitives, they are passed by value. The reason for the String not changed here is it is immutable.

Fatalshears said: (Oct 29, 2014)  
replace() method will return a derived string from the original string while append() method return a reference of the original string. Therefore the answer is C.

Rishi said: (Dec 21, 2016)  
Please explain clearly. I didn't understand the concept of this question.

Deovrat Jalgaonkar said: (Jun 2, 2017)  
Following line will change the value of String as we are not just calling the method but also assigning it to the variable:

text = text.replace ('j' , 'c'); /* Line 5 */

So, the output should be "cavajavac" if + operator is allowed for StringBuffer else Compile Error.

Uwe said: (Feb 13, 2018)  
An object of the class "String" is immutable, but it has the method. Exchange (old character, new character). The result will be stored in a NEW STRING. In our case, "cava" is a new string, but though the method StringReplace returns nothing and text is an immutable String Object, it just remains. I thought, that a compile Error would be produced and I don't understand why not. So please anyone explain this.

Akhlaq Mirza said: (Aug 1, 2018)  
Why at the time of initialization of stringBuffer variable "textBuffer" is showing javac value during debugging in eclipse editior before calling bufferReplace(textBuffer);?

Even though the methods are not returning any result so in SOP statement we r printing just local variables which are holding java, java values respectively.

Please elaborate I am confused.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.