Using Strings

Sir,I have the following doubt:class TestString{String a = "Hello";String b = new String("Hellohowareu");}Is there any difference between the strings a and b;RgdsBiswajith
[241 byte] By [Bjith_p_ma] at [2007-11-27 1:56:31]
# 1
I see your doubt and raise you a Captain Obvious.One is "Hello" and one is "Hellohowareu". Of course they are different.
floundera at 2007-7-12 1:30:59 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi, Yes, they are not the same reference and they have a different value.But both are initialize correctly.Hope that help, Jack
jack@square6a at 2007-7-12 1:30:59 > top of Java-index,Java Essentials,Java Programming...
# 3

Hallo,

Yes you have the difference between two.

String s ="Maruthi";

It creates a String containing "Maruthi", and a reference, s pointing to it.

String s1 = new String("Maruthi");

This creates a new String-object that points to a

String ("Maruthi"), and a reference s will be

pointing to the new String-object

So it will function the same, but the second option

s1 will get two references. ( don't worry).

Example:

There is another important issue..

String s1 = "Maruthi";

String s2 = "Maruthi";

String s3 = new String("Maruthi");

Now s1 == s2 ,its True

but s1 != s3, Its False

That is s1 and s2 references the same object, but s3 references another object.

Regards

Maruthi.

mars@sayampua at 2007-7-12 1:30:59 > top of Java-index,Java Essentials,Java Programming...