strings
the output of the below code says that str1 and str3 refer to different objects; why can't you have two strings with different values to refer to the same object?
class EqualsTest{
publicstaticvoid main(String[] args){
String str1, str2, str3;
str1 ="mars";
str2 = str1;
str3 ="neptune";
System.out.println("Str1: = "+str1);
System.out.println("Str2: = "+str2);
System.out.println("Str3: = "+str3);
System.out.println("str1 & str2. Same Object? "+(str1 == str2));
System.out.println("str1 & str3. Same Object? "+(str1 == str3));
}
}
[1043 byte] By [
mark_8206a] at [2007-11-27 11:05:04]

As an analogy...
Why can't you have an apple and a banana refer to the same fruit, hmm?
For that matter, why can't you have an apple and another distinct apple refer to the same fruit?
simple: They aren't the same fruit. They exist separately in space as distinct, concrete objects.
> why can't you have two strings with different values to refer to the same object?
Think about what you're asking.
~
> As an analogy...
>
> Why can't you have an apple and a banana refer to the
> same fruit, hmm?
> For that matter, why can't you have an apple and
> another distinct apple refer to the same fruit?
This is the essence of the Theory of Relativity. What Newton was basically saying was, when an apple falls on his head, somewhere else in the Universe there was a completely different apple not falling on his head, at exactly the same time. So when the Big Bang utterly destroyed the Universe, it also didn't
Or something
(please note subject line)
> > why can't you have two strings with different
> values to refer to the same object?
>
> Think about what you're asking.
>
> ~
Ooh! Pick me!
Is the answer the fundamental basics of logic?
> > > why can't you have two strings with different
> > values to refer to the same object?
> >
> > Think about what you're asking.
> >
> > ~
>
> Ooh! Pick me!
>
> Is the answer the fundamental basics of logic?
42.
Wrong!
The answer is a different string object which only happens to have the same value "42" ;)
> Wrong!
> The answer is a different string object which only
> happens to have the same value "42" ;)
minus 10 points for making the obvious joke :p
> Wrong!
> The answer is a different string object which only
> happens to have the same value "42" ;)
You mean like
String s1 = null;
String s2 = null;
System.out.println(s1 == s2);
?
when i join str1 and str2 together, str4 is no longer the same object as str3. Why are str4 and str3 different objects when they still have the same value" (redgreen) ?
thanks
class JoiningStrings {
public static void main(String[] args) {
String str1 = "red", str2 = "green", str3 = "redgreen", str4 = "redgreen";
System.out.println("str1 & str2. Same Object? "+(str1 == str2));
System.out.println("str3 & str4. Same Object? "+(str3 == str4));
str4 = str1+str2;
System.out.println("str3 & str4. Same Object? "+(str3 == str4));
}
}
> Why are str4 and str3 different objects when they still have the same value
Different String objects can contain the same character sequence. You may want to look at the intern() method...
String str1 = "red", str2 = "green", str3 = "redgreen", str4 = "redgreen";
System.out.println("str1 & str2. Same Object? "+(str1 == str2)); // false
System.out.println("str3 & str4. Same Object? "+(str3 == str4)); // true
str4 = str1+str2;
System.out.println("str3 & str4. Same Object? "+(str3 == str4));// false
System.out.println("str3 & str4. Same Object? "+(str3 == str4.intern())); // true
~
When you assign a value to a referece variable--whether String or any other kind of object--all you're doing is pointing that variable at an object. You're not copying an object, you're not changing the contents of an object, and you're not affecting any other reference variables.
> String str1 = "red",
Points reference variable str1 at the String object in the constant pool that holds "red".
> str2 = "green",
Points reference variable str2 at the String object in the constant pool that holds "green".
> str3 =
> = "redgreen",
Points reference variable str3 at the String object in the constant pool that holds "redgreen".
> str4 = "redgreen";
Points reference variable str4 at the same String object as above.
> str4 = str1+str2;
Creates a new String obect that is a concatention of "red" and "green" and points str4 at it.
> System.out.println("str3 & str4. Same Object?
> ? "+(str3 == str4));
False.
== compares reference values, not object contents. That is, compares whether the two references point to the same object (or both contain null). Here, str3 points to one String object and str4 points to a different String object. The fact that the two objects have the same contents--the same state--is irrelevant.
jverda at 2007-7-29 13:06:07 >

thanks for the explanations, i have one more question
String str1 = "the", str2 = " sun";
str1 += str2;
After i change the value of str1 to str1+=str2, is it correct that another object has been created and now str1 points to that different object?
str1 does not now refer to the object it refered to when str1 = "the"?
> After i change the value of str1 to str1+=str2, is it
> correct that another object has been created and now
> str1 points to that different object?
Yes.
> str1 does not now refer to the object it refered to
> when str1 = "the"?
Correct.
~
does anyone know of any web pages with good examples of how strings are immutable?, i've done a google search but can't find what i'm wanting.
thanks
> does anyone know of any web pages with good examples
> of how strings are immutable?,
What do you mean "examples of how Strings are immutable."
I don't know how you could make an example of the fact that a String's contents can't be changed.
All you can do is look at the docs and and note that it says, "Strings are constant; their values cannot be changed after they are created," and see that yes indeed, there are no methods that change the contents.
jverda at 2007-7-29 13:06:12 >

> does anyone know of any web pages with good examples
> of how strings are immutable?
Strings are designed for immutability in that there's not a good way to change their contents. What kind of an example are you looking for? What do you find confusing about this?
~
> > does anyone know of any web pages with good
> examples
> > of how strings are immutable?
>
> Strings are designed for immutability in that there's
> not a good way to change their contents. What kind of
> an example are you looking for? What do you find
> confusing about this?
>
> ~
I see you're attending those "Reflection Anonymous" meetings I told you about :-)
This might be an example of immutability.
public class Test {
public static void main(String[] args) {
String string = "This is the first string. ";
System.err.println(string);
string.concat("I'll try to concatenate this, but it won't. ");
System.err.println(string);
String other = string.concat("This will, though. See? ");
System.err.println(other);
}
}
After you invoke a method that you'd expect to change a mutable object, you see it hasn't worked. Very simple, and doesn't show much other than one method doesn't work as you might expect
> I see you're attending those "Reflection Anonymous" meetings I told you about...
;o)
~
String str = new String("the");
String w = new String("world");
str = str + " " + w;
str pointing to "the world" is formed by concatenating three or two String objects?
i'm pretty sure it's 3 but i want to make sure!
is 3 correct?
thanks
> str pointing to "the world" is formed by
> concatenating three or two String objects?
Don't worry about it. No, seriously. The compiler compiles it to append the strings to a StringBuilder (StringBuffer, prior to 1.5), so your question doesn't really apply. I guess if you're really wondering, the StringBuilder appends three strings...
Note that the following idiom is almost always wrong:
String s = new String("foo");
~
the webpage said 3 but sometimes those sites are wrong!
> > str = str + " " + w;
>
>
At runtime, three distinct String objects will be concatenated to form a new String object.
jverda at 2007-7-29 13:06:12 >

> > > > str = str + " " + w;
> >
> >
>
>
> At runtime, three distinct String objects will be
> concatenated to form a new String object.
But here: str = str + "a" + "b" + w;
there will be also be three objects concatenated at runtime, not four, because the compiler will turn the literals "a" + "b" into the single literal "ab" that goes into the constant pool.
Again, though, as yawmark said, this doesn't really matter.
jverda at 2007-7-29 13:06:12 >

> > > > str = str + " " + w;
> >
> >
>
>
> At runtime, three distinct String objects will be
> concatenated to form a new String object.
thanks, my confusion was with the " ". i wasn't sure if it was a string or not.
> thanks, my confusion was with the " ". i wasn't sure
> if it was a string or not.
Yep. It's a String literal, which refers to a String object (pooled) containing the character sequence [' '].
~