• Post Reply Bookmark Topic Watch Topic
  • New Topic

String, StringBuilder  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: The output is: javajavac

I do not understand the method replaceStringBuilder(StringBuilder s) in details. StringBuilder is mutable, the following code I understand perfectly: In the method replaceStringbuilder(s2) you pass a value. In the method you can see: It is not s2, it is s; the variable s has its cope within this method only. I am a bit confused about the variable s and the variable s2. I would kindly ask for a clarification.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs,
The method parameter is s. So in the context of that method, s refers to the StringBuilder.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that is very important to understand is the difference between variables and objects. Variables and objects are not the same thing - often this is not clear for beginners, and sometimes people use sloppy language and say "object" when they actually mean "variable", which can be confusing if you don't understand exactly what the difference is.

A variable is a reference to an object. In other words, it's something that points to an object. You can have multiple variables that point to the same object.

What happens exactly in your code?

In line 4, a StringBuilder object is created, and a variable named s2 is initialized to point to that StringBuilder object.

In line 6, the method replaceStringBuilder is called, and the value of the variable s2 (which is the pointer to the StringBuilder object) is passed to the method.

The declaration of the method is in line 12. The method has an argument variable s. When the method is called, this variable s is initialized to whatever was passed to the method at the point it was called. In this case, it was called in line 6, where the value of the variable s2 was passed. So, for that call, the argument variable of the method, s, is initialized to point to the same StringBuilder object as the one that s2 in the main method points to. There are now two variables that point to the same StringBuilder object - s2 in the main method, and s in the replaceStringBuilder method.

Then execution proceeds with line 13, where s.append("c"); is performed. So, the append method is called on the StringBuilder object.

Then the method returns and execution proceeds at line 7. Note that s2 is still pointing to the StringBuilder object, and the method replaceStringBuilder that was just called has called append("c") on that StringBuilder object.

And how does the replaceString method work?

In line 10, an assignment is done to the argument variable s. This assignment makes s point to a different String object than the one that was passed to the method, in line 5. Making the argument variable s refer to a different object does not affect the variable s1. Note that when you call a method, passing it a variable as an argument, only the value of the variable is passed - not the variable itself. The value is copied into the argument variable of the method, which is a separate, different variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!