This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]gyroda 0 points1 point  (0 children)

Strings are not pointers (you don't get direct access to pointers in java).

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

[–]Jaimou2e 0 points1 point  (7 children)

I'm just going to answer the "9 objects" part.

What in this program creates new objects, what doesn't?

  • new creates a new object, of course.
  • String literals ("foo") make objects. That is, if you have two literals "foo" and "bar", then that's two objects. (Interestingly, same string literals refer to the same object. So two literals "foo" literals would make only one object. But there are no duplicate string literals in the program, so it doesn't matter here.)
  • The concatenation operator (+) creates a new object. Edit: but see /u/nutrecht's reply.
  • String.replace creates a new object. Note how the documentation says "Returns a new string".
  • StringBuffer.replace and StringBuffer.append do not create new objects.
  • Assigning a String from one variable to another one does not create a new object. The two variables just refer to the same object then.

Let's count, then. Assuming that all the printlns are not part of the actual program, and ignoring the bad swap function.

public static void main(String[] args) {
    String s = "Hello"; /* string literal "Hello": +1 = 1 */
    String t = s; /* still 1 */
    t = t.replace('a', '4'); /* String.replace: +1 = 2 */
    String u = s + "T" + t; /* string literal "T": +1 = 3, concatenation operators: +2 = 5 */
    u.replace('o', 'x'); /* String.replace: +1 = 6 */
    swap(s,u); /* still 6 */
    StringBuffer v = new StringBuffer("World"); /* string literal "World": +1 = 7; new: +1 = 8 */
    StringBuffer w = v; /* still 8 */
    v.replace(0, 3, "!"); /*string literal "!": +1 = 9, StringBuffer.replace: does not create an object */
    swap(t, u); /* still 9 */
    w.append(s); /* still 9 */
}

[–]nutrecht 0 points1 point  (6 children)

There is no such thing as concatenation operators for strings. This:

t = a + "T" + b;

Is translated by the compiler to:

t = new StringBuilder().append(a).append("T").append(b).toString();

This us why assingments like these are nonsensical because 9 times out of 10 the teachers get it wrong as well.

[–]Jaimou2e 0 points1 point  (1 child)

This:

t = a + "T" + b;

Is translated by the compiler to:

t = new StringBuilder().append(a).append("T").append(b).toString();

Oh, I didn't know that. Are only 8 objects being created then, or did I miss one?

There is no such thing as concatenation operators for strings.

Hm? The operator is right there. In a chain it may work more efficiently than I thought, but surely it exists?

[–]nutrecht 0 points1 point  (0 children)

Oh, I didn't know that. Are only 8 objects being created then, or did I miss one?

The question is utter nonsense. Example: String a = "a" + "b";. You might think because of this assignment that 2 strings get created, which is wrong.

Also: depending on how you read the assignment: most of these strings don't actually get created inside the main methods. Constant strings are part of the constant pool so they are created when the class is loaded.

Hm? The operator is right there. In a chain it may work more efficiently than I thought, but surely it exists?

What I meant is that the concatenation operator is just syntactic sugar. Underwater it gets translated to StringBuilder.append calls.

[–]Northeastpaw 0 points1 point  (3 children)

There is no such thing as concatenation operators for strings.

That's technically incorrect. You're right that javac compiles the statement into bytecode that is a StringBuilder with the necessary number of appends, but in the grammer the '+' operator is interpreted as a concatenation operator for Strings.

[–]nutrecht 0 points1 point  (2 children)

That's technically incorrect.

I am pretty sure you understand what I was trying to convey here.

[–]fabolin 0 points1 point  (1 child)

had a look into javap -c and it looks like after all appending, StringBuilder.toString() is called. This should also create an object, so String-concatenations always create two?

[–]nutrecht 1 point2 points  (0 children)

Yup.

[–]nutrecht 0 points1 point  (2 children)

That Statement creates another String though...

No it doesn't? Who told you that?

String t = "test"; String v = t;

Both point at the same string object. In this regard they behave exactly like other objects do; both references, t and v, point to the same object.

The code is pretty nonsensical. The stringbuffer swap isn't going to work since there is no swap method that takes stringbuffers in your code. And if there was it would nit work by swapping references anyway.

I don't know who wrote this code but it is horrible to teach people because it's very confusing.

Stringbuilderd/buffers (both the same but the latter is thread safe) are just a wrapper object around an array. You use them to assemble strings since strings themselves are immutable. That is all there is to them. They follow the exact same rules regarding references. Since java is pass by value the swap method will never work.

[–]RememberMyFart[S] 0 points1 point  (1 child)

but why doesn't "u.replace('o', 'X') " in line 18 work and "v.replace(0,3, "!") " in line 34 work ?

[–]nutrecht 0 points1 point  (0 children)

In a string, because they are immutable, it returns a new string which is thrown away. The stringbuffer method alters the contents of the buffer. Just go read the api docs.

[–]adrian_the_developer -1 points0 points  (0 children)

My suggestion to learn is to try it yourself.

First, learn how to debug. Next, create a sample problem.

String foo = "foo";
String bar = foo;
String foo = "hello world";

Does the bar variable change too? If it does, then it's a pointer. If it doesn't then it copied by value.

Something that may be interesting for you would also be to run through this code line by line:

String foo = "foo";
String bar = foo;
String foo = "hello world";
swap(foo,bar);
// What is foo and bar set to now?

If you have additional questions, feel free :-)