String s="abc" in java.what happened in the memory any object is created or not and what is "s" here variable or object,and same question with String s=new String("abc");.
There's a thing called String Memory Pool in java, when you declare:
String str1="abc";
It goes to that memory pool and not on the heap. But when you write:
String str2=new String("abc");
It creates a full fledged object on the heap, If you again write:
String str3 = "abc";
It won't create any more object on the pool, it will check the pool if this literal already exists it will assign that to it. But writing:
String str4 = new String("abc");
will again create a new object on the heap
Key point is that:
A new object will always be created on the heap as many times as you keep writing:
new String("abc");
But if you keep assigning the Strings directly without using the keyword new, it will just get referenced from the pool (if it exists in the pool)
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
String Java Doc
Related
String newString=new String("JAVA");
newString="JAVA8";
WHAT will happen here if we print newString we know output will be JAVA8 only but 2nd line created new object in heap or constant pool.
How its behave internally
In Java Strings are immutable.
What that means is that once a String object is created it cannot be updated.
Also Java has something called Java String Pool, a memory location where Java stores Strings.
To minimize the memory space used by this String Pool, Java performs Interning when a new String literal is create using the following statement.
String s = "apple";
What Interning means is that JVM looks in the String Pool to see if the string literal "apple" exists, if it does it just passes its reference to new variable, no new object is created, if it does not exist it creates a new object passes its reference and also can pass the same reference if another string is created with same value. For Example:
String t= "apple";
If you check if the both strings created above are reference to the same string literal in string pool, you can do that as follows:
System.out.println(s == t); // True
When a string is created using new keyword it always creates a new Object in the String pool, even if the string literal exists in the string pool.
String u = new String("apple");
You can check that the new object is created by following statement:
System.out.println(s == u); // False
System.out.println(t == u); // False
In yoru case the first statement created a new object in String pool and passed its reference. The second line created another object and passed its reference. The first object is still in String pool but not being referenced by any variable and eventually the memory occupied by it will be released by Garbage collection.
Hope this answers your question, you can read about Java String pool to know more.
First, newString is not a String!!!
newString is a reference to an instance of the class String! A C programmer would name it pointer and write String* newString.
With
String newString = new String( "JAVA" );
you declare the variable newString of type "reference to String" and assign the reference to (the address for) the new object, created by a call to the constructor of the class java.lang.String from the compile time constant "JAVA".
newString = "JAVA8";
now assigns the reference to the compile time constant "JAVA8" to that previously created variable. At the same time, the previously reference object gets abandoned (no more references to that object were hold) and therefore it is now ready to be garbage collected.
Most Java implementations do store the compile time constants not on the heap, and strings are usually stored in the string pool. Also it is possible that the call to new String() will never be executed; instead the compiler will directly use the reference to the compile time constant. Or it will be executed and the resulting String object is stored on the Heap, with or without referencing the string value in the string pool.
The details can be found in the Java Language Specification and the JVM Specification for the respective version, and if that says "implementation dependent", in the documentation for the respective JVM/Java implementation.
String Str1= new String("java");
String Str3 = new String("java");
System.out.println(Str1==Str3);
The 1st line of code creates object str1 with content "java" init both in String pool and heap. Now what will Str3 does? I know it create Str3 in heap what about Str3 in String pool? What does 3rd line do? does it checks for equality in String pool or in heap area? I know that it gives false but my question is if Str1 and Str3 are stored in String pool == should give true right as references are same.
The 1st line of code creates object str1 with content "java" init both in String pool and heap.
Actually:
The string pool is part of the heap. (This doesn't actually relate to your misconception, but it helps to get these things straight in your head.)
The string object corresponding to the "java" literal is created by the classloader, not by that statement. It is created when the class is loaded1 ... not when the code is run.
So, in fact that statement only creates a string object in the heap.
Now what will Str3 does?
The second statement creates a single string object ... in the heap.
As per the above explanation, the string object for the "java" literal used in this statement was created previously. (Indeed, if you look in the bytecode file, you will see that there is only one "java" string in the file's constant pool. So, the classloader won't even need to create two string instances and intern() both of them. Only one is created and interned.)
I know it create Str3 in heap what about Str3 in String pool?
Nope. All string literals are created in the string pool, which automatically dedups them.
What does 3rd line do? Does it checks for equality in String pool or in heap area?
Nope. It tests to see if the object references are the same; i.e. if they are the same object. It doesn't actually test where they are in.
(You can't actually directly test if a String is in the pool or not. And you can't even test if an object is in the heap or not ... if the JVM provides you a way to allocate objects outside of the (regular) heap.)
I know that it gives false but my question is if Str1 and Str3 are stored in String pool == should give true right as references are same.
They aren't in the string pool. They are ordinary heap objects.
Think of it this way, the fact that == returns false proves that they aren't (both) in the string pool. Because if they were both in the string pool they would have to be the same String object.
Here is what the JLS says on string literals and identity:
"Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern."
This interning happens when the class is loaded1.
1 - The specs don't say when the interning happens. It appears that recent JVMs resolve a method's reference to a String literal lazily; i.e. the interning happens the first time the method is called by the bytecode interpreter. However, this doesn't change the substance of the above explanation.
Well, I've got such a code
String s = "hello";
String s2 = s + "world";
I know that variable s is stored in Java Heap in String Literal Pool. Also I know that variable s2 is stored in Java Heap (because '+' in this example creates a new object). But my question is: will "world" be put into Pool or nothing will happen with it?
Yes it will. Wherever / Whenever , you have a String literal declared, it goes to pool obviously, regardless the usage of it.
Since the String "world" does not exist in the pool, a new String object instantiates, then it is placed in the pool.
When we use double quotes to create a String, it first looks for String with same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference.
Basically, 's' and 's2' are just references while real objects are "Hello" and "world". This follows the concept specifying Strings are immutable while String references are not. So, Yes, as "world" is String object,apart from being a String literal, it will be stored in heap similar to hello. Also remember neither 's' nor 's2' will goto heap as being mutable String references.
This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed last month.
If I write something like this:
String s1 = new String("ABC");
String s2 = "ABC";
In which scenarios does the string pool get updated? The first, second or both?
In the above programming code ,when string pool get updated?
The first statement will update/add to the String pool with "ABC" String literal, but create another object in the heap and the variable s1 will refer the heap object.
The second statement will refer the already created Pool String object.
String s = new String("ABC");
this always create a new String object on the heap and add "ABC" to the pool(if not present).
String s= "ABC";
Whereas this line is called string literal. It checks whether the string pool already has the same string "ABC" or not. If present then s will refer to that object otherwise a new object will be created.
Conclusion: new String() will always create a new object and string literal checks the string pool before creating one.
String s1=new String("ABC");//creates two objects and one reference variable
In your case, JVM will create a new String object in normal(nonpool) Heap memory and the literal "ABC" will be placed in the string constant pool.The variable s1 will refer to the object in Heap(nonpool).
String Constant Pool
String objects created with the new operator do not refer to objects
in the string pool but can be made to using String’s intern() method.
The java.lang.String.intern() returns an interned String, that is, one
that has an entry in the global String literal pool. If the String is
not already in the global String literal pool, then it will be added.
Whenever you use new keyword to create String Object,it will create in Heap and then it will check the String Constant Pool for the same String Literal.If SCP doesn't contain that String literal then Only it will create String literal in the SCP.
Nope. Strings created with the constructor never enter the string pool.
In the example, two separate objects are created. Therefore:
s1 != s2
This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 5 years ago.
When we use new operator to create a String object, I read that two Objects are created one Object is string constant pool and second one is in heap memory.
My question here is We are using new operator hence only one object should be created in Heap. Why then one more object has to be created in String Constant pool. I know Java stores String object whenever we not use new operator to create a String. For eg:
String s = "abc" .
In this case only it will create in String constant pool.
String s2 = new String("abc")
only one object hast to be created in heap and not in Constant pool.
Please explain why I am wrong here.
We are using new operator hence only one object should be created in Heap.
Sure - the new operation only creates one object. But its parameter is a String literal, which already represents an object. Any time you use a String literal, an object was created for that during class loading (unless the same literal was already used elsewhere). This isn't skipped just because you then use the object as a parameter for a new String() operation.
And because of that, the new String() operation is unnecessary most of the time and rarely used.
See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern
Thus when you write
String s2 = new String("abc")
The Compile-time constant expression "abc" will be interend.
It will be created two object, one object into pool area and other one in non pool area because you are using new and as well as string literal as a parameter.
String s = new String("abs");
String s2 = new String("abc")
It will create only one object in heap memory. We need to use intern() method of java.lang.String explicitly to make the entry in String pool.
String s = "def".
Two Objects will be created here. When you create using String literal notation of Java, it automatically call intern() method to put that object into String pool, provided it was not present in the pool already.
String in java is Immutable, once created its cannot be changed. also, any string literal will be stored in the string pool for later use whenever the same literal is used again, for example:
String name = "my name"; // one intern object is created in pool
String otherName = "my name"; // the old intern object is reused
System.out.println( name == otherName); // true , the same reference refer to same object
the two reference refer to the same location in the pool.
String name = new String("my name"); // one object is created, no string pool checking
String otherName = new String("my name"); // other object is created, no string pool checking
System.out.println( name == otherName); // false, we have 2 different object
here we have two different String object in memory each of them have its own value.
see this article for more:
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
form this below line of code will create 3 object in the JVM
String s2 = new String("abc")
for "abc" in String pool memory.
for new operator one object in heap.
one more for s2.
String s2 = new String("abc");
here 1 literal and 1 object will be created.
With new operator 1 String object will be created in heap as "abc" and s2 will refer it, moreover "abc" is string literal also that is passed in String Constructor so it will go in String Constant pool.
literal is consider as object so 2 object will be created here.