3

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");  ?
kedar kamthe
  • 8,048
  • 10
  • 34
  • 46

6 Answers6

11

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    This is true. But such a requirement is *so* rare, that it makes the other answer ("it is never necessary") *almost* correct. – Joachim Sauer Apr 19 '12 at 07:56
7

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
3

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.

Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77
0

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.

Cole
  • 179
  • 6
0

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.

-1

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

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • @ Don: You need only look at the other answers here. You've said `String str = "abc";` is equivalent to `String str = new String("abc");`. That's simply incorrect. – T.J. Crowder Apr 19 '12 at 08:15
  • @T.J.Crowder : if we talk about "string comparing" and "object comparing" i totally agree. But here is asked about difference beetween those two different declaration. And, since All string literals in Java programs, such as "abc", are implemented as instances of this class the only differences are those that all answer showed. But take a look at this: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html – DonCallisto Apr 19 '12 at 08:20
  • @ Don: The question is about creating, not declaring, strings, and in what scenarios it would be appropriate to use which form. Your answer (with respect, don't get the idea I'm trying to be hard on you here) gives incorrect information (they're *not* equivalent, because of auto-[interning](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern%28%29)) and doesn't address the substance of the question (scenarios where each would be appropriate), so I expect that's why it's attracting downvotes. – T.J. Crowder Apr 19 '12 at 08:26
  • 1
    @T.J.Crowder Ok, don't have "fear" to explain your ideas:I do not offend :) So, if I look at the question from another slant i suppose that you got a point. But i'll no delete my answer that can be "usefull" as well for understand where is the "error" :) – DonCallisto Apr 19 '12 at 08:30