kedar kamthe
kedar kamthe

Reputation: 8178

String literals vs String Object in Java

In java String can be created in 2 ways given below

  1. String foo="Test";
  2. String fooobj=new String("Test");

Everywhere it is mentioned about difference between these 2 ways of creating String. I want to know more about What are appropriate scenario's , where we should go for

  String foo="Test";

And when to go for

 String fooobj=new String("Test");  ?

Upvotes: 3

Views: 2717

Answers (6)

Ajay Pathak
Ajay Pathak

Reputation: 11

There is a subtle differences between String object and string literal.

String s = "abc"; // creates one String object and one reference variable In this simple case, "abc" will go in the pool and s will refer to it.

String s = new String("abc"); // creates two objects,and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.

Upvotes: 0

DonCallisto
DonCallisto

Reputation: 29912

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

     String str = "abc";

is equivalent to:

     String str = new String("abc");

You should avoid the second way to declare a string, for reasons explained into other answers

Upvotes: -1

Cole
Cole

Reputation: 179

When you use the second approach, you actually rely on the first approach to initialize the constructor arguments.

So the question is if you could create the String object via first approach, why you spend extra effort to use the second approach ?

I don't see any scenary to use the second constructor style, so always insist in using the first approach.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074138

The short answer: If you're in any doubt, you don't want new String("literal here"). If you need it, you'll know you need it, and why.

The long answer:

Essentially the only time you want to use new String("literal here") is if you want to ensure that the resulting string object is not == any other string object. Literals get interned automatically; strings created via new String("literal here") are not.

So why would you want that? The answer is you almost never would, because String instances are immutable, so you don't care if you're sharing a String instance with something else. Just about the only scenario I can imagine is if you had an API that accepted a String and you wanted to have a flag value other than null, and you wanted to check that flag/marker value via ==, like this:

public static final String MARKER = new String("Marker");
public void someFictionalMethod(String arg) {
    if (arg == MARKER) {
        // Take action on the marker
    }
    else {
        // Take action on the string
    }
}

...and even then I'd tend to find it a bit suspect and would explore other ways to do it.

Upvotes: 11

Piotr Kochański
Piotr Kochański

Reputation: 22672

You should never use new String(). Anytime you create string in this way, new object in memory is created. If you write String s = "aaa", then there is a chance such object was already created in a given JVM, it is stored in string pool and thanks to that your variable will be just reference to that existing object - you safe memory in this way.

Upvotes: 3

Jesper
Jesper

Reputation: 206786

It is never necessary to create a new String object with

String fooobj = new String("Test");

So, you should never do this. Just write String fooobj = "Test"; instead.

Class String is immutable. That means that the content of a String object cannot be changed after it has been constructed. It's never necessary to create an explicit copy of a string literal.

Upvotes: 7

Related Questions