Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding Strings

 
Amit Punjwani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I just have a very small doubt regd Strings and StringBuffer. Please offer your comments
<pre>
class TestString1
{
public static void main(String[] args)
{
String hello = new String("Hello");
TestString1 t = new TestString1();
t.sayHello(hello); // line 1
System.out.println(hello); //line 2 "Prints out "Hello"
}
public void sayHello(String text) {
text += "World"; // line 3
System.out.println(text); // prints out "HelloWorld"
}
}
</pre>
Output of the program is
HelloWrold
Hello
Please correct me if I am wrong .
I just wanted to be sure regarding parameter passing in case of Objects including Strings and that it is the copy of the reference that is passed to the methods not the value.
1)If the a copy of the reference is passed to the method sayHello(hello) at line 1, then why doesn't the change be affected to above at line 2.
2)I guess the only reason could be that String objects are immutable therefore they cannot be changed once created. If changed it would create an entire new String object which is
local to the method at line 3
3)If the same program were to be changed such that String objects be replaced by StringBuffer then the changes would be available outside the method sayHello(). code given below.
<pre>
class TestString
{
public static void main(String[] args)
{
StringBuffer hello = new StringBuffer("Hello");
TestString t = new TestString();
t.sayHello(hello); // line 1
System.out.println(hello); //line 2 "Prints out "HelloWorld"
}
public void sayHello(StringBuffer text) {
text.append("World"); // line 3
System.out.println(text); // prints out "HelloWorld"
}
}
</pre>
The output of this program is
HelloWorld
HelloWrold
 
srikrish
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
The variable hello initally points a string object "Hello". When you pass the reference to this object to sayhello method, the net result is that the variable text is also pointing to the same "hello" object. Now, when you do string concatenation of "Hello" and "World", you get a brand new object "HelloWorld" and the variable text will be pointing to this new object after executing the concatenation statement. So, println(text) gives "HelloWorld". However, during all this, the variable hello keeps poiting to the original "Hello" object. So, println(hello) gives "Hello".
If hello and text are StringBuffer objects, then concatenating "World" to text variable (i.e., "Hello" object) only modifies the "Hello" object to "HelloWorld". The point to note here is that the resultant string "HelloWorld" is not a new object (as in the case of String concateneation). Only the contents of the "Hello" object (pointed to by both text and hello) are modified. Hence the two println statements produce the same result ("HelloWorld").
Hope this helps.
 
Amit Punjwani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot srikrish,
I got it, i had a little doubt. Thanx for clearing it
 
Amit Punjwani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot srikrish,
I got it, i had a little doubt. Thanx for clearing it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic