Reputation: 20891
It might sound strange at first, might seem simple, yet I'm stuck the well-expected point. I think in following code, text
is referenced to by s
and t
, as output,I would get hello world hello world
, but not. I get hello world
.
class Test2 {
private volatile static String text = "";
public static void main(String[] args) {
String s = text;
text = "hello world";
String t = text;
System.out.println(s + " " + t);
}
}
What point did I miss until now? I'm really baffled at that. Presumably a new object is created there implicitly. But why?
Following one is not related Java, but C-knowers. I try to interpret the above code in C. I get expected result there, hello world hello world
.
#include <stdio.h>
int main()
{
char const volatile * volatile x = "";
char const volatile * volatile const * xPtr = &x;
x = "hello world";
char const volatile * volatile const * xPtr2 = &x;
printf("%s %s\n", *xPtr, *xPtr2);
return 0;
}
Upvotes: 1
Views: 71
Reputation: 8598
Your two code snippets are not equivalent.
You have pointers to pointers in the C example, but not in your Java example. Actually equivalent code in C would be:
#include <stdio.h>
int main()
{
char const *text = ""; // text points to a memory location that contains the string ""
char const *s = text; // s now points to the same memory location
text = "hello world"; // text now points to another memory location that contains the string "hello world", s continues pointing to the memory location where the string "" is
char const *t = text; // t now points to the memory location that text points to, which is the one containing "hello world"
printf("%s %s\n", s, t);
return 0;
}
Which will give the same result as you got in your Java example.
Since Java doesn't have pointer semantic, you cannot achieve what you tried to achieve in Java, except if you use some kind of Holder
class to wrap a String object.
Upvotes: 2
Reputation: 2208
Your problem is here s = text
. You think that by doing this s
is a reference that will point to text
(which himself point to ""
). But what it does is that it evaluates the value of text and make s
point to it, so s
point to ""
and not text
.
Then when you do text = "hello";
you do not change the object ""
to "hello"
you just make text
point to a new object (It stop pointing ""
and now points "hello"
)
So when you print the whole things, it evaluates s
(=""
) and t (="hello"
)
Upvotes: 2
Reputation: 533492
I get hello world.
You should get this with two spaces at the start.
What point did I miss until now?
Using a debugger shows you why, but in short you only have references and primitives in Java. There are no references to references.
char const volatile * volatile const * xPtr = &x;
There is nothing like this available in Java.
Presumably a new object is created there implicitly.
A new StringBuilder
and a new char[]
are implicitly created however I don't think this is what you mean.
Stepping through the code
String s = text; // text = "", s = ""
text = "hello world"; // text = "hello world", s = ""
String t = text; // text & t = "hello world", s = ""
System.out.println(s + " " + t);
Upvotes: 4