2

I really don't know when and why should I use the constructor for String and not the literal way. I have already looked, found some answers of differences which were not really clear... but nothing about the best practices or "why?" and "when?"

I understood that literal definition is treated by the JVM but also that they are not handled by the Garbage Collector (like static staff, annotations ...)... could this really be a problem for the memory of the JVM ?

I added here the shortest comparison that I found:

What is the difference between creating String as new() and literal? When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.

Thank you for your time and help.

ahmed_khan_89
  • 2,755
  • 26
  • 49
  • 3
    I never use `new String()`. (But using a constructor that takes a non-`String` argument is one legitimate reason; I've just never had to use that.) – ajb Apr 09 '14 at 22:38
  • Refer to http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java – s16h Apr 09 '14 at 22:41
  • 2
    I only used `new String()` when I create a String out of a byte[] or char[], otherwise I would always use a literal. – Gregor Koukkoullis Apr 09 '14 at 22:42
  • 4
    The primary reason to use `new String("literal")` is to confound new programmers who insist on using `==` to compare strings. – ajb Apr 09 '14 at 22:44
  • 1
    "it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap." This statement is no longer true on up-to-date versions of Java – Affe Apr 09 '14 at 22:54
  • ay ay ay !!!? now I am really confused . so can you give more details , like how is it now, or what changed ? or at least a new good link and I will read by myself ...? – ahmed_khan_89 Apr 09 '14 at 23:18

1 Answers1

5

Mainly, you'll find yourself using new String when you have some dynamically constructed char[] or byte[] that you want to use as a string. This could be for such reasons as having received data from the network into a byte[] buffer, but there are a multitude of possible reasons.

Note also that, if you want your new string interned into the string pool, you can use the intern() method.

The pool of interned strings hasn't been put into the permgen for quite a while, by the way, and in Java 8 the permgen was removed altogether.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Do you have a source for the permagen being removed? – Mark W Apr 09 '14 at 22:44
  • 1
    Heres a primary source: http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html – Mark W Apr 09 '14 at 22:50
  • thank you for your fast answer . can I please ask another question: "the string pool" and "The pool of interned strings" are terms for all the Strings' values in an application? "the permgen was removed altogether" , so where is the annotations and the static staff , if there is no permgen anymore in the JVM ? also, can I find java documentation for my "weird" questions? thx again – ahmed_khan_89 Apr 09 '14 at 23:14
  • 1
    @ahmed_khan_89: As for the significance of the string pool, refer to the documentation for [String.intern()](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#intern()). As for the permgen, it has been replaced by "metaspace" in Java 8. As I understand it, the primary difference is that metaspace is dynamically allocated and thus not intrinsically limited in size. Already in Java 7, however, interned strings were allocated on the heap rather than in permgen. – Dolda2000 Apr 09 '14 at 23:57