Is a String literal stored on the stack? Is a new

2019-01-21 07:47发布

问题:

Possible Duplicate:
difference between string object and string literal

Let's say I have two statements.

String one = "abc";
String two = new String("abc");

Which one is a stack memory and which is stored in heap?

What is the difference between these both?

How many objects are created and how is the reference in memory?

What is the best practice?

回答1:

All objects are stored on the heap (including their attributes).1

Local variables (including arguments) always contain primitive values or references and are stored on the stack.1

So, for your two lines:

String one = "abc";
String two = new String("abc");

You'll have two objects on the heap (two String objects containing "abc") and two references, one for each object, on the stack (provided one and two are local variables).

(Actually, to be precise, when it comes to interned strings such as string literals, they are stored in the so called string pool.)

How many objects are created and how is the reference in memory?

It is interesting that you ask, because Strings are special in the Java language.

One thing is guaranteed however: Whenever you use new you will indeed get a new reference. This means that two will not refer to the same object as one which means that you'll have two objects on the heap after those two lines of code.


1) Formally speaking the Java Language Specification does not specify how or where values are stored in memory. This (or variations of it) is however how it is usually done in practice.



回答2:

The first one is called as a String Literal and created at the time of compilation of the program and the 2nd one is string object and is created at the runtime.

As you used new keyword in 2nd case so it is allocated in heap.

In the first case the objects are created with the mechanism called interning. When you try to create another string literal representing the same sequence of characters, then instead of creating a new object compiler will refer to the previous string created and stored in the string pool



回答3:

Only instances of primitive types (int, long, ...) are saved on stack. All instances of reference types (String, Integer, Long, YourTypeHere, ...) are saved in heap.

UPDATE As pointed out in comments, references to instances of reference types (that is, non-primitive types -- Object and its' descendants) can be saved on stack. These are your local variables.

This is not "a best practice", it's the way JVM works and you can't change it.



回答4:

In your case 2 String objects are created. Generally, all objects are created on the heap. However since String one is a string literal, this will be stored in the string pool (in PermGen). You can also use the intern() method to add a string to the string pool and get a reference to it.

If the declarations you posted are in a method then the references will be stored on the stack, but not the objects themselves.

As for, best practice i think it would be: String one="abc"

This is for 2 reasons:

  1. The code is cleaner
  2. Interned strings can be compared with == which is faster than equals. But you'll need to intern() any non-literal string before you can compare it.

EDIT: You might be interested in checking out this link: Escape Analysis in Java SE 7. It presents some HotSpot related optimizations which affect object allocation.