Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why new keyword not needed for String

I am new in java.

In java, String is a class.But we do not have to use new keyword to create an object of class String where as new is used for creating objects for other classes.

I have heard about Wrapper classes like Integer,Double which are similar to this. But String is not Wrapper,isn't it?

Actually what is happening when i use

     String message = "Hai"; 

?? How it is different from

String message = new String("Hai"); 

Here is message a reference variable or something else?? Are there other classes which do not require new to create object ??

like image 372
sonu thomas Avatar asked Dec 30 '11 14:12

sonu thomas


People also ask

Why new keyword is not used in string?

String message = new String("Hai"); new String("Hai") is a new String object. In this case, even if the literal "Hai" was already in the string literal pool, a new object is created. This is not recommended because chances are that you might end with more than one String objects with the same value.

What happens when we create string using new keyword?

By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.

Can we create string object without new operator?

You can create an object without new through: Reflection/newInstance, clone() and (de)serialization.

Which is better string literal or new keyword?

In general, we should use the String literal notation when possible. It is easier to read and it gives the compiler a chance to optimize our code.


2 Answers

With the following line you are not creating a new String object in the heap but reusing a string literal (if already available):

String message = "Hai"; 

"Hai" is a string literal in the string literal pool. Since, strings are immutable, they are reusable so they are pooled in the string literal pool by the JVM. And this is the recommended way, because you are reusing it.

But, with the following you are actually creating a new object (in the heap):

String message = new String("Hai"); 

new String("Hai") is a new String object. In this case, even if the literal "Hai" was already in the string literal pool, a new object is created. This is not recommended because chances are that you might end with more than one String objects with the same value.

Also see this post: Questions about Java's String pool

Are there other classes which do not require new to create object ??

Actually, you can not create any object in Java without using the keyword new.

e.g.

Integer i = 1; 

Does, not mean that the Integer object is created without using new. It's just not required for us to use the new keyword explicitly. But under the hood, if the Integer object with value 1 does not already exist in cache (Integer objects are cached by JVM), new keyword will be used to create it.

like image 130
Bhesh Gurung Avatar answered Sep 18 '22 22:09

Bhesh Gurung


The Java language specification allows for representation of a string as a literal. You can consider it a shortcut initialization for a String that has one important side-effect that is different from regular initialization via new

String literals are all interned, which means that they are constant values stored by the Java runtime and can be shared across multiple classes. For example:

class MainClass (     public String test = "hello"; }  class OtherClass {    public String another = "hello";     public OtherClass() {        MainClass main = new MainClass();        System.out.println(main.test == another);    } } 

Would print out "true" since, both String instances actually point to the same object. This would not be the case if you initialize the strings via the new keyword.

like image 35
Perception Avatar answered Sep 22 '22 22:09

Perception