This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding Strings" Watch "Doubt regarding Strings" New topic
Author

Doubt regarding Strings

Amit Punjwani
Ranch Hand

Joined: Jul 10, 2000
Posts: 50
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

Joined: Sep 12, 2000
Posts: 63
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

Joined: Jul 10, 2000
Posts: 50
Thanx a lot srikrish,
I got it, i had a little doubt. Thanx for clearing it
Amit Punjwani
Ranch Hand

Joined: Jul 10, 2000
Posts: 50
Thanx a lot srikrish,
I got it, i had a little doubt. Thanx for clearing it
 
Consider Paul's rocket mass heater.
 
subject: Doubt regarding Strings
 
Similar Threads
wrapper class doubt
Doubt in Strings
StringBuffer and String returned from a method doubt
Constructor doubt
Threads Doubt