0

I just failed a code review without any reason given due to my writing, for a function where s (of type String) is passed as a parameter

if (s == null){
    s = new String();
}

Said I should use s = ""; instead. Why?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • https://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings – Jiri Kremser Apr 13 '16 at 09:48
  • Your question is unclear.. `s = new String();` doesn't compile. What are you trying to do exactly? – TheLostMind Apr 13 '16 at 09:49
  • U wot? It does in Java 7. – P45 Imminent Apr 13 '16 at 09:49
  • [There's definitely a default `String` constructor](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String()), and it creates an empty `String`. – Theodoros Chatzigiannakis Apr 13 '16 at 09:51
  • Adding to Bathsheba answer, Readability would improve if you make it `s = ""`.That might be one of the reason. – Keerthivasan Apr 13 '16 at 09:52
  • @Keerthivasan it's not readability. the first comments provided link and the given answer is correct. – rusted brain Apr 13 '16 at 09:55
  • @P45Imminent using `s = "" ` would save a little processing time as it will check in string pool, if the empty string exists it will refer there else it will make a new empty string. However that little processing time is not noticeable and there are chances that `""` empty string won't exist in string pool. so you can argue with reviewer. – rusted brain Apr 13 '16 at 09:59
  • Even better would be using a constant. Constants.EMPTY_STRING which = "", More importantly I would be checking the calling algorithm as to why on hell it would pass you a null string and fix it there. This type of null checking is inherently horrible. – VeenarM Apr 13 '16 at 10:15
  • I'm implementing an interface that has as a requirement that the parameter can be null. – P45 Imminent Apr 13 '16 at 14:56

4 Answers4

7

You could always clarify with the reviewer?

new String(); always creates a new string. (Isn't the Java syntax self-explantory?)

s = ""; will set the reference to an interned string and is therefore preferred.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

There is a preference in the String class when initialized with literals over initialization with the new keyword, this is because of the JVM optimizations, and because the JVM allocates strings initialized with literals directly in the StringPool.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
3

Both s = new String();s = ""; expressions give a string object but there is a clear difference between the two. s=new String() will create a new object in the heap memory whereas if we create object using string literal s = ""; it may return an existing object from String pool(a cache of string objects) if it already exists.

Example will make it more clear:

String a= "banana";
String b= "banana";

Here both a and b refer to the same object and hence a==b is true

String a =new String("banana")
String b= new String("banana")

Here both a and b refer to two different objects and hence a==b is false

annu
  • 75
  • 6
  • 4
    how will `a==b` in the first example return true? – rusted brain Apr 13 '16 at 10:21
  • 2
    You seem to have made a mistake in the example: `a == b` in the first example will not return true, since "banana" will _never_ equal "orange". If both were "banana", _then_ they'd be equal. – Jonas Czech Apr 13 '16 at 10:25
1

JVM handle it differently if you call:

  • s = new String(); //The constructor will create new object everytime it's called
  • s = ""; //The string literal will refer to the same object in the common pool

You can find the full answer in the link bebow: http://o7planning.org/web/fe/default/en/document/19529/string-stringbuffer-and-stringbuilder-tutorial

Example:

public static void main(String[] args) {
    String str1 = "apple";
    String str2 = "apple";

    if(str1 == str2) {
        System.out.println("str1, str2: same object");
    } else {
        System.out.println("str1, str2: different object");
    }

    String str3 = new String("apple");
    String str4 = new String("apple");

    if(str3 == str4) {
        System.out.println("str3, str4: same object");
    } else {
        System.out.println("str3, str4: different object");
    }       
}
Tri Nguyen
  • 56
  • 5