Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Finding the output (Q.No. 21)
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);
}
}
Answer: Option
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"
Discussion:
9 comments Page 1 of 1.
Sachin said:
1 decade ago
@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.
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.
Uwe said:
8 years ago
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:
7 years ago
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.
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.
Jack said:
1 decade ago
"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.
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.
Deovrat Jalgaonkar said:
8 years ago
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.
text = text.replace ('j' , 'c'); /* Line 5 */
So, the output should be "cavajavac" if + operator is allowed for StringBuffer else Compile Error.
John said:
1 decade ago
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
String textString = new String ("java");
textString = textString.replace ('j' , 'c'); /* Line 5 */
System.out.println(textString);
This gives: cava
Fatalshears said:
1 decade ago
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:
9 years ago
Please explain clearly. I didn't understand the concept of this question.
Amit Singh said:
1 decade ago
Why javajavac why not javac. Please ellaborate.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers