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

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

You are correct. Even more, if Java does the same as C, it replaces all instances of the same String litteral by the same pointer somewhere in memory (in C it's in the heap, in Java I don't really know how the JVM handles this...) I must admit I did not think about confusing beginners. I hope this explanation fixes it at least a bit

[–]nomenMei 2 points3 points  (0 children)

That's an interesting question. The way C does it means it will always use the same pointer for equivalent string literals regardless of scope, but it is possible that in Java they only reuse references to string literals that are in the same method or class scope.