2
String s="hi";
String s1=new String("hi");

In memory perspective, where are s and s1 stored? whether it is in heap memory or stack.

And s points "hi" and s1 points to memory location of where hi exists?

Please help?

zEro
  • 1,255
  • 14
  • 24
user2345736
  • 49
  • 1
  • 6
  • 1
    actually it is a duplicate of [memory allocation in java, heap or stack](http://stackoverflow.com/questions/9991701/memory-allocation-in-java-heap-or-stack) put can only mark as duplicate of [Difference between string object and string literal](http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal) – UmNyobe Jul 01 '13 at 08:30

7 Answers7

5

Consider following

 String s = "hi";
 String s1 = new String("hi");

variable s will refer to the string literal hi that is referenced from String constant pool and if there are some more variables like s2 = "hi", then s and s2 will refer to same object.

String s1 = new String("hi"); 

This will create a new String at runtime.

In first case ,all the strnig literals are created when class is loaded in JVM In seconds case, string objects are created when new String() is executed. You can find a good tutorial about string constant pool at following link

http://www.thejavageek.com/2013/06/19/the-string-constant-pool/

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2
String s="hi";

This statements creates the String object containing "hi" on the String pool.

String s1=new String("hi");

This statement creates a String object containing "hi" on the heap memory and a copy of it on StringPool (if "Hi" is not contained in stringPool).But here, S will point to the object on heap.

Mac
  • 1,711
  • 3
  • 12
  • 26
1

At class loading time, all String literals will be placed in pool. When you use new String("hi") an object will be created on heap. s and s1 are reference variables , it should reside in the method call stack ! The string literal "hi" will be handled by the JVM, by creating a String object on the heap, and having a reference to it from the String pool. The new operator merely takes the string object, whose reference is passed in the constructor, and create a new object. It just so happens that the string being passed to the constructor is a literal.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

"hi" is a String literal. This gets created once in the String Constant Pool. In Java 7 that's in the Heap with other objects, but prior to that it was created in the Perm-Gen

 String s = "hi";

 String s1 = new String("hi");

new String("hi") creates a new String object on the heap, separate from the existing one.

s and s1 are references to the two separate objects. References themselves actually live on the stack, even though they point to objects in the heap.

matt freake
  • 4,877
  • 4
  • 27
  • 56
0

s and s1 are just references to the strings, therefore they will be stored on the stack, unlike the string instances that are referenced by s and s1. In this case the value that s refers to will be put in the string pool whereas the value refered by s1 won't. Why? because the constructor of a string has been used to make that string. Only literals are pooled, and even if you chose those literals to be concatenations of other literals ( for example, if you have made String s2 = "h" + "i" then s and s2 would point to the same instance of a string that was stored in the string pool).

This leads to a little trap though: because pooled strings point to the same object it is tempting to use == operator instead of equals method to compare strings, which is just dangerous, because there are scenarios where == is the same as equals() but there are a lot more where == will have a different result than the equals() method.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
user2511124
  • 158
  • 5
0

Above answers are correct but shouldn't you use String *s1 = new String("hi") in place ofString s1 = new String("hi") ,as call to new will be returning a pointer to the string object. I am fairly new to C++. Excuse me, if I am wrong.

PS: I am using the gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC).

0

According to me when we create String using literal like String s="hello" s object will reference the String "hello" that is stored in the String Constant pool.

If we will create the new String using New keyword like String s = new String("hello") then in this case two object is created. object s referenced the another object that is stored in the normal heap area and this object will referenced the String Hello that is stored in the String constant pool.

For more details please follow the link:-http://www.javatpoint.com/java-string