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.
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.
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"
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");
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)