0

What is the difference between

String name1 = "some name";

and

String name1 = new String("some name")

Which is better and good for use?

thanks

Hooman.AS
  • 190
  • 1
  • 3
  • 18
  • Initially, this feels like it's opinion based. Except there's only one opinion that makes sense here. – Dawood ibn Kareem Jun 27 '18 at 07:55
  • if one of those ways was not "good for use", do you think it would have been implemented? – Stultuske Jun 27 '18 at 07:55
  • 1
    Duplicate of https://stackoverflow.com/questions/390703/what-is-the-purpose-of-the-expression-new-string-in-java and https://stackoverflow.com/questions/465627/use-of-the-stringstring-constructor-in-java – DrHopfen Jun 27 '18 at 07:55
  • 2
    Use the first. The second one will create two strings (one for the literal, then an additional one with `new String`). – marstran Jun 27 '18 at 07:55
  • For programmer and program – Hooman.AS Jun 27 '18 at 07:56
  • @Stultuske that constructor was implemented in case of specific situations. The use proposed here is not one of these situations, and is bad. – kumesana Jun 27 '18 at 07:57
  • 1
    From JavaDoc of `String(String)` constructor: "Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable." There are very, very rare situations where it is useful, and you will likely not encounter them. The first form is shorter, faster, and more legible - use that one exclusively unless you know you want the other one. – Amadan Jun 27 '18 at 07:57
  • thanks i find the answer @DrHopfen – Hooman.AS Jun 27 '18 at 07:59
  • @kumesana since by the post we can't determine for what scenario the OP is asking, how do you know that? – Stultuske Jun 27 '18 at 08:00
  • 1
    @Stultuske because the scenarios where this is legitimate are extremely specific and rare, and if OP had been in one of them, OP would have said so... And not asked any question on the matter as OP would have known all about it. It is not possible to use it legitimately without knowing what is a legitimate usage. – kumesana Jun 27 '18 at 08:03
  • while creating string object like this String name1 = new String("some name"); this will not update in String pool memory. we have to explicitly put in string pool memory. like name1 = name1.intern(); but in another case, it's automatically maintain string pool memory. if it exists you get the same object. – Faiz Akram Jun 27 '18 at 08:05
  • that is saying: "it is impossible for a company to require junior developers to write software they don't truly understand (yet)". This happens on a daily basis, juniors are given guidelines to follow, with technical outlines they don't necessarily understand. – Stultuske Jun 27 '18 at 08:06
  • yes you are correct @Stultuske – Hooman.AS Jun 27 '18 at 09:34
  • @Stultuske the only reason why you'd use new String(String) is because you have written some code that compares Strings and this comparison really should be either identity or equality, and now you want to test that code you've made to check that it does use the right comparison. Using this constructor is the most straightforward way to test this, so it's a legitimate use. There is no other. Still standing to your theory? – kumesana Jun 27 '18 at 11:53
  • @kumesana that's the only use you can think of when you don't want to re-use a String from the pool? – Stultuske Jun 27 '18 at 12:26
  • @Stultuske when wanting that with good reason, yes. Though I suppose the notion of the String pool and the comparison to use between Strings, sometimes overlap. – kumesana Jun 27 '18 at 13:14

1 Answers1

1

Generally use String name1 = "some name"; But, if you want that strings to has different references, use new initialization.

Java has some optimization about strings. = "" initilizations checks string pool if same value initialized before.

String s1 = "test";
String s2 = "test";
String s3 = new String("test");


s1 == s2 // this is true because of string pool
s1 == s3 // this is false because of s3 is new instance

In the above example, s1 and s2 placed at string pool s3 is outside of java string pool.

Using new String causes creation new instances and more memory consuption.

Take a look for more information about string pool : https://www.journaldev.com/797/what-is-java-string-pool

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
  • Strictly speaking, `= "..."` doesn't do any checking. All string literals present at compilation time are preallocated at that time, so deduplication is never actually run at runtime. – Amadan Jun 27 '18 at 08:00