Java - Create String object using new keyword


Ankit Sharma

I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword

String str = new String("test");

In this case, we will pass a parameter of type String. My question is where is this string generated - heap or string constant pool or somewhere else?

As far as I can tell, this parameter is a string literal and therefore should be in the string constant pool. If so, what internis the purpose of the method - just link the variable strto the constant pool? Because it "test"will already be available.

Please clarify if I misunderstood the concept.

Manuti

This statement String str = new String("test");creates a string object that will be stored on the heap like any other object. String literals "test"passed as arguments are stored in the string constant pool.

String#intern()Check if the string constant is already available in the string pool. If it already exists, it will return it, otherwise it will create a new one and store it in the pool. See Javadocs :

Returns the canonical representation of a string object.

The initially empty string pool is maintained privately by the class String.

When the intern method is called, if the pool already contains a string equal to this String object determined by the equals(Object)method , the string in the pool is returned. Otherwise, Stringadd this object to the pool and Stringreturn a reference to the object .

So for any two string ssum t, true s.intern() == t.intern()if and only if true .s.equals(t)

Since JDK7, interned strings are stored on the heap. This is from the release notes for JDK7 :

In JDK 7, permanent strings are no longer allocated to the permanent generation of the Java heap, but to the main part of the Java heap (called the young and old generations) and other objects created by applications. . This change will result in more data residing in the main Java heap and less data in permanent generation, so heap resizing may be required. Because of this change, most applications will only see relatively small differences in heap usage, but large applications that load many classes or make heavy use of the String.intern()method will see larger differences.

Related


Create object using static keyword in Java

Shashank Agarwal: class abc { int a = 0; static int b; static abc h = new abc(); //line 4 public abc() { System.out.println("cons"); } { System.out.println("ini"); } static { System.out.println("stat")

Create new object from string in Java

W : Is there a way to create new class from String variable in Java? String className = "Class1"; //pseudocode follows Object xyz = new className(param1, param2); Also, if possible, does the resulting object have to be of type Object? There may be a better wa

How to create XML object in String using Java?

Lake: I am trying to write code to help me create XML objects. For example, I take a string as input to a function and it will return an XMLObject. XMLObject convertToXML(String s) {} When searching the web, it is common to see examples of creating XML docume

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create object using static keyword in Java

Shashank Agarwal: class abc { int a = 0; static int b; static abc h = new abc(); //line 4 public abc() { System.out.println("cons"); } { System.out.println("ini"); } static { System.out.println("stat")

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create new object from string in Java

W : Is there a way to create new class from String variable in Java? String className = "Class1"; //pseudocode follows Object xyz = new className(param1, param2); Also, if possible, does the resulting object have to be of type Object? There may be a better wa

How to create XML object in String using Java?

Lake: I am trying to write code to help me create XML objects. For example, I take a string as input to a function and it will return an XMLObject. XMLObject convertToXML(String s) {} When searching the web, it is common to see examples of creating XML docume

When we create a new DateTime (object) using the new keyword

Evgeni Velikov what's the difference DateTime thisTime = new DateTime(); and this DateTime thisTime; Who is better performance, who is better practice. The start time of creation is the same datetime. I use thisTime to add dateTime from a file with 8000 line

When we create a new DateTime (object) using the new keyword

Evgeni Velikov what's the difference DateTime thisTime = new DateTime(); and this DateTime thisTime; Who is better performance, who is better practice. The start time of creation is the same datetime. I use thisTime to add dateTime from a file with 8000 line

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Create an object in Java using the "Object" keyword

Cooley I'm just starting to learn Java and have two basic questions. My main() looks like this: public class Main { public static void main(String[] args) { Storage<BankAccount> aStorage = new Storage<BankAccount>(); Storage<String> sStorage = new Sto

Create new string-based object using Java Stream

User 11998955 I'm playing around with Java Streams and I'm wondering if there is any way to create a block of code like this -> if(givenString.equals("productA")) { return new productA(); } else if(givenString.equals("productB") { return new productB()

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Create new string-based object using Java Stream

User 11998955 I'm playing around with Java Streams and I'm wondering if there is any way to create a block of code like this -> if(givenString.equals("productA")) { return new productA(); } else if(givenString.equals("productB") { return new productB()

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Create object using static keyword in Java

Shashank Agarwal class abc { int a = 0; static int b; static abc h = new abc(); //line 4 public abc() { System.out.println("cons"); } { System.out.println("ini"); } static { System.out.println("stat");

Create object using static keyword in Java

Shashank Agarwal: class abc { int a = 0; static int b; static abc h = new abc(); //line 4 public abc() { System.out.println("cons"); } { System.out.println("ini"); } static { System.out.println("stat")

Create new object from string in Java

W : Is there a way to create new class from String variable in Java? String className = "Class1"; //pseudocode follows Object xyz = new className(param1, param2); Also, if possible, does the resulting object have to be of type Object? There may be a better wa

How to create XML object in String using Java?

Lake: I am trying to write code to help me create XML objects. For example, I take a string as input to a function and it will return an XMLObject. XMLObject convertToXML(String s) {} When searching the web, it is common to see examples of creating XML docume

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Java - Create String object using new keyword

Ankit Sharma I know the difference between a String literal and a new String object, and how it works internally. But my question is a little improvement on this. When we create String object using new keyword String str = new String("test"); In this case, we

Use the new keyword to create a single object

Randier The following questions were posed in the fourth round of interviews conducted by the program director. There are A grades. Any number of classes can be derived from A. Constraints are A or any subclass derived from A, I should only be able to create o

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }

Create C++ object with and without new keyword

Karan Shetty Create an object with the new keyword: #include <iostream> #include <string> using namespace std; class Person { private: string name; public: Person(string name) { setName(name); } string getName() { return this->name; }