0

Can anyone tell me how many objects are created. Does s3 not reference the same hello from string pool? how many String objects are there

/**
 * 
 */
 package agnitio;

/**
 * @author admin
 *
 */
public class TestString {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String s1="hello";
    String s2="hello";
    String s3 = new String("hello");
    System.out.println(s1==s2); // true
    System.out.println(s1==s3); // false
    System.out.println(s2==s3); // false

}

}
Monis
  • 167
  • 2
  • 13
  • Do you _really_ care? You know that you should use `.equals()` to compare contents and `==` to compare references. That's it! Problem solved! – fge Jun 20 '13 at 06:03
  • can anyone tell me how many objects are being created there – Monis Jun 20 '13 at 06:04
  • not a duplicate - this is more about string caching – Oleg Mikheev Jun 20 '13 at 06:05
  • Again, _why_ do you want to know? – fge Jun 20 '13 at 06:05
  • why do you want to know ? – Monis Jun 20 '13 at 06:08
  • i mean why anybody wants to know...........for knowledge – Monis Jun 20 '13 at 06:09
  • You want to use intern() on s3 to get it to == s1 and s2. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#intern() – Rob Jun 20 '13 at 06:09
  • dude can anyone tell me how many string objects are there – Monis Jun 20 '13 at 06:11
  • Two strings are created by your code. – Rob Jun 20 '13 at 06:11
  • 2, s1 and s2 is "interned", in other words they point to the same String pool because the the string is the same. In s3, you explicitly use new operator and thus it created new instance of String therefore not referencing to the same instance of s1 and s2. – jchips12 Jun 20 '13 at 06:13
  • Depends on how many Arguments you pass to main :P. Two is the minium. One for the literal `"hello"` (which get's interned) and a new one where the reference is stored in `s3`. – Andreas Dolk Jun 20 '13 at 06:13
  • Knowledge of this will bring you nothing here; different JVMs may very well behave differently. You have tried this with _one_ JVM. Have you tried with Dalvik, BEA, etc etc? – fge Jun 20 '13 at 06:16
  • String s1 = new String("hello"); , how many references are created at JVM level? I know of two objects:1 at the time of class loading for "hello" and 1 at the actual time of execution of command. 1 reference is s1, my doubt is there is also a 2nd reference s2 which is the result of "hello" string at class loading which is passed to the "new String()" constructor. In java, there is no way we can pass an object itself in a function or constructor, we can only pass a reference. Also, there should be a 3rd reference s3, which should be the result of new String("hello") and then got assigned to s1? – Bhavesh Agarwal Jan 16 '14 at 04:40

4 Answers4

5

Only two objects are created. First object when you do :

String s1="hello";

No object is created in memory when you do :

String s2="hello";

This is because JVM String class is based on flyweight pattern so if a string already exist in memory as in your case "hello", so creating a new reference will not create a new object. Both s1 and s2 will point to the same memory location.

Second object is created when you do:

String s3 = new String("hello");

As new operator will always create a new object.

== compares whether both the references pointing to the same memory location or not. While equals compares the contents of strings. Having said that and as I mentioned both s1 and s2 are pointing to same memory location and hence both == and equals will return TRUE for their comparison. But s3 is a different object and hence comparison wiht s1 and s2 with == operation will return false. But if you do equals comparison of s1,s2 and s3, you will get TRUE.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 4
    that i know dude please the whole question – Monis Jun 20 '13 at 06:02
  • @Monis Hope you understand now what you wanted to :-) – Juned Ahsan Jun 20 '13 at 06:08
  • if all three reference to same object why only one is true and not all – Monis Jun 20 '13 at 06:12
  • @Monis I said s1,s2 are pointing to one memory object, while s3 is pointing to different memory object. If you read my complete answer you should be able to understand, why two objects are created and how == will behave when you compare them – Juned Ahsan Jun 20 '13 at 06:15
  • @Monis: this answers your question perfectly well. Two objects are created. Reference compare only checks if the objects are the same, not the contents, but dude you already knew that right. – Jonas Byström Jun 20 '13 at 06:17
  • @Jonas Bystrom if s3 refers to different object then there are 3 object two for hello and one for new – Monis Jun 20 '13 at 06:19
  • @Monis s3 is differernt object but s1 & s2 are two references to 1 memory object. When you use a string literal the string can be interned but when you use new String("...") you get a new string object. Learn more about string intern here: http://en.wikipedia.org/wiki/String_interning – Juned Ahsan Jun 20 '13 at 06:23
  • @JunedAhsan what you are saying i completely understand but that makes 3 objects which are created isn't that true – Monis Jun 20 '13 at 06:25
  • are we only talking about the string in the pool being created? otherwise this String s3 = new String("hello"); //creates two objects, and one reference variable. – mel3kings Jun 20 '13 at 06:27
  • 1
    @Monis I believe you are confused between objects and references. s1,s2,s3 are references to some memory objects. Which means they point to some memory location where some contents are stored. s1 & s2 are two references which point point to one memory location. But s3 is a reference which points to another memory location. The contents at both the memory locations are same. Hope it clears some confusion now – Juned Ahsan Jun 20 '13 at 06:27
  • @JunedAhsan ya i know about references i am just saying if s3 is not referencing pool's hello then another hello object is created – Monis Jun 20 '13 at 06:30
  • String s= new String("hello"); creates two object one with new and other with String.............Kathy Sierra – Monis Jun 20 '13 at 06:31
  • @JunedAhsan String s = "abc"; // creates one String object and one // reference variable In this simple case, "abc" will go in the pool and s will refer to it. String s = new String("abc"); // creates two objects, // and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool. – Monis Jun 20 '13 at 06:52
  • @JunedAhsan Now who is confused dude – Monis Jun 20 '13 at 07:30
  • 1
    @Monis: Me and JunedAhsan still belive it's you. – Jonas Byström Jun 20 '13 at 09:43
  • @JonasByström ok i will ask one ques how many objects are there at run time give a number – Monis Jun 20 '13 at 10:24
  • @JonasByström let me tell you what i understand. s1=s3 is false because they refer to different objects so they are different objects and String s3= new String("hello"); is creating two objects one with new and other hello. tell me where am i wrong – Monis Jun 20 '13 at 10:31
  • String s1 = new String("hello"); , how many references are created at JVM level? I know of two objects:1 at the time of class loading for "hello" and 1 at the actual time of execution of command. 1 reference is s1, my doubt is there is also a 2nd reference s2 which is the result of "hello" string at class loading which is passed to the "new String()" constructor. In java, there is no way we can pass an object itself in a function or constructor, we can only pass a reference. Also, there should be a 3rd reference s3, which should be the result of new String("hello") and then got assigned to s1? – Bhavesh Agarwal Jan 16 '14 at 04:40
1

No, imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "hello" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say

String A = "hello"
String B = "hello" 

Now String B called "hello".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable.

=====EDIT=====

If we are talking about how many string objects are there:

String s = "hello";    // one string object in the string pool "hello"
String s2 = "hello";   // two object variables (s, s2)

in this simple case, "abc" will go in the pool and s and s2 will refer to it.

String s3 = new String("hello"); // creates two objects, and one reference variable.

In this case, because we used the new keyword, Java will create a new String object in a normal (nonpool) memory and s will refer to it. In addition the literal "hello" will be placed in the pool as well (if it doesn't exist).

mel3kings
  • 8,857
  • 3
  • 60
  • 68
  • if uppercase is applied to it a new string object will be created – Monis Jun 20 '13 at 06:07
  • exactly, answering the question : "Does s3 not reference the same hello from string pool?" – mel3kings Jun 20 '13 at 06:08
  • @melt321 - `which change the same object` - this is totally *misleading* – Oleg Mikheev Jun 20 '13 at 06:11
  • @melt321 if all refer to same then why one true – Monis Jun 20 '13 at 06:17
  • @melt321 so there are 3 objects created or not – Monis Jun 20 '13 at 08:42
  • @Monis 2 String objects in the String pool. 1 for s and s2, one for s3 due to the `new`. 4 objects (3 String variables and 1 reference variable, due to the way s3 was instantiated) – mel3kings Jun 20 '13 at 08:49
  • @melt321 one object in the pool "hello" one object for new and one hello created separately . "In addition the literal "hello" will be placed in the pool as well (if it doesn't exist)." – Monis Jun 20 '13 at 08:53
  • @Monis you should really clarify your question, if you are just asking if s3 uses the same reference, then the answer is no due to `new` – mel3kings Jun 20 '13 at 08:55
0

.equals() method matches two strings based on values (contents present) in that String and == check whether two objects points to same reference or not.

check the link below you will get your answer,

difference between == and equals method

Mayank Tiwari
  • 2,974
  • 5
  • 30
  • 52
0

use String.contentEquals:

"stringA".contentEquals("StringB");

Strings are objects, and whenever you create a 'new string' it actually creates a 'new String(value)' which points to a location in memory For example a "float" is a primitive/value, whereas Float is an Object with a pointer to a location in memory. When you use '==' it simply checks if the pointers are the same. "String.contentEquals" checks to see if the contents of both objects are the same.

Psyfire
  • 1
  • 1