6
String s = null;
s = s + "hai";
System.out.println(s);

Output:

nullhai

Thought this would throw me NPE.

What is the fundamental logic behind

  • not throwing NPE while using + (concatenation)
  • throwing NPE while using .
icza
  • 389,944
  • 63
  • 907
  • 827
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56

3 Answers3

4

For , s = s + "hai"; Internally, String.valueOf(null) will be called. That method will convert null to "null".

Byte code :

public static void main(java.lang.String[])   throws java.lang.Exception;
    0:   aconst_null
  //some code
   10:  invokestatic    #27; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;// check this line
// some other code
       27:  return
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
3

No it wont.Compiler replaces String concatenation with StringBuilder operations which doesn't give null pointer exception.Compiler does all this backend and saves our time of writing boiler-plate code.Actually there is no such thing as String concatenation as String is immutable

Decompiled code:-

    String s = null; 
    s = (new StringBuilder(String.valueOf(s))).append("hai").toString(); 
    System.out.println(s);

So,the answer to your question.

  1. Compiler internally changes + concatenation operations using StringBuilder and String.valueOf operations.So the compiler makes sure that null cases would be handled
  2. While using a . operator on a String instance,you invoke methods defined in a String instance such as String.concat(String str)which will give a NullPointerException if your String instance is null
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • 1
    I think you should emphasize more on `String.valueOf()` being called internally. Without valueOf(), you will get an NPE. :) – TheLostMind Sep 08 '14 at 09:35
  • @TheLostMind I think i answered how String concatenation works using + operator,but yes String.valueOf is important – Kumar Abhinav Sep 08 '14 at 09:37
  • Wow, so it was useless for me to convert s += "some text \n"; (10 of these lines) into a string builder? The compiler does that already to prevent so many strings from being initialized? – CausingUnderflowsEverywhere Oct 06 '17 at 17:03
1

NullPointerException is only thrown if you try to call a method on a reference which is null (or trying to access a field of it; or you manually throw a NullPointerException).

This does not happen in your case because your null reference will be converted to a "null" String, no method will be called on the s local variable.

So for example:

String s = null;
boolean isNull = s == null; // This is ok, just testing the value of s
s.toString();               // NPE, s is null

Also note that static fields can be accessed even if variable is null:

s.valueOf(12);             // THIS IS ALSO OK, valueOf(int) is static
// It is equivalent to:
String.valueOf(12);
icza
  • 389,944
  • 63
  • 907
  • 827
  • This is interesting !! For static methods, will the reference points to class ? I mean; `s.XXX` is equivalent `String.XX` ? – Vinay Veluri Sep 08 '14 at 09:42