aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String problem" Watch "String problem" New topic
Author

String problem

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
1 String s1 = "Amit";
2 String s2 = "Amit";
3 String s3 = new String("abcd");
4 String s4 = new String("abcd");
5 System.out.println(s1.equals(s2));
6 System.out.println((s1==s2));
7 System.out.println(s3.equals(s4));
8 System.out.println((s3==s4));

Output of above code is:
true
true
true
false

Output from 6th line is true but 8th line false why?? Please explain..


SCJP 1.4, SCBCD 1.3
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi Soni,
If u are not costrcting the string using new, it will refer it from the string pool. so the first 2 statements, refering to the same place, but if u create a string with new it will allocate memory for it, so the next 2 statements are refering to different locations.
== compares the references, and equals() compares the contents, so u got the result like that.
i think u got my point.
sri.
Mohan Mani
Greenhorn

Joined: Sep 02, 2004
Posts: 16
Hi Soni,

In String == checks, whether the object's are same.


1 String s1 = "Amit";
2 String s2 = "Amit";
3 String s3 = new String("abcd");
4 String s4 = new String("abcd");
5 System.out.println(s1.equals(s2));
6 System.out.println((s1==s2));
7 System.out.println(s3.equals(s4));
8 System.out.println((s3==s4));


Please note in String that there is a difference in creating a String with new String() and simply Initializing for the same value.

String str = "a";
String str1 = new String("a");

Here str and str1 will point to different address, means they are different objects thou having same value.


In above quote output of 8 is "False" since Object s3 and s4 are different.
The reason for 6 is true, is
If you initialize any number of String's with same value eg:- "Amit", all will point to same address where an Object is created for "Amit".
ie: s1 and s2 are pointing to the same address, were in s3 and s4 are pointing to different address.

Regards
Mohan
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Hi,
Soni Firstly
== Checks the Object reference equality,while
equals Checks the Object value equality,Secondly
When you code:
String S="JavaRanch";
String S1="JavaRanch";
these are just reference variables not Object
until we don't use new operator and when we
use new it refers to two different new objects
while if we are not using new then it refers to
String Pool.



"Do not be afraid of going slow, be afraid of standing still"
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
It means

String a1 = "soni";
String a2 = "soni";
String a3 = "soni";

a1,a2,a3 all are refering to the same string object in the pool but

String a4 = new String("soni");
String a5 = new String("soni");
String a6 = new String("soni");

a4,a5,a6 are refering to the three different string object?
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
So in the above case a1, a2, a3 are not objects they are just references but refering to what?? (some string in the pool but is that string not an object??).
SomeswaraRao Vudattula
Greenhorn

Joined: Mar 28, 2005
Posts: 12
soni,

at the time of declaring the variables the following process will be done..

str1 = "somu";
//Compiler create new reference for this String.
str2 = "somu";
//Compiler check the String Pool,if it found the same content, compiler will assign to that exist reference..other wise it will create new one.

"==" compares the references so it shown true
.equals() compares the content so it is also shown true


but in the following case, it will craete always new references because we r using the new operator

String str1 = new String("somu");
String str2 = new String("somu");
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
a.equals(b)//compares the content of the two reference variable
a==b//compares the address of the reference variable

can i say that the above conditions are true whether the equals() method is overwritten or not.
or

is there is any impact on the above condition when the equals method is overridden.

what the significance in overridding the equals method.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Soni,

All strings in Java are represented by objects. I think the confusion comes from the fact that there are really two times that Java is dealing with your code.

The first is when your code is compiled. At that point, any string literals (that is, any strings that are typed directly into your code) are set up as string objects. You can think of the literal itself as being a reference to this string object.

However, to save memory, Java is smart enough to keep these objects in the "string pool". If you have two literals with the same text, Java will make those two literals point to the same string object. Java can get away with this sort of reuse because the text of string objects cannot ever be changed. And it can afford to take the time to do all this matching because it's compiling, and time is not really a big factor.

The second time Java deals with your code is during run-time. Well okay, it's really dealing with a bytecode representation of your code, if you want to get picky. Anyway, at that point, when your program is running, it will also be creating string objects. Often, this creation will be because you've used "new", but it also happens behind the scene as a result of calling various string functions (like trim()). Java can't predict what will happen here ahead of time, when you compile, because it all depends on how your code executes. It has to run the code first to find out what strings objects to instantiate.

At this point--when strings are created at run time rather than at compile time--Java does not normally look at the string pool and try to "match" up your new string with an already existing one. It doesn't have time! Instead, it creates a brand-new object, even if it does duplicate a string object already in the string pool.

(You can force Java to use the string pool at run-time by using the intern() method on a string)

Hope that helps!

- Jeff
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String problem
 
Similar Threads
Doubt with String method "replace"
String Pool
one more on ==
String and StringBuffer
about string