Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null String Concat

 
Rakesh Ne
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Attempting to compile and run the code
1. Will cause a compilation error.
2. Runtime Execption - NullPointerException in the 2nd line of the main method.
3. Will compile successfully and print nullnull on the screen.
4. Will compile successfully and print an empty line on the screen.
5. Will compile successfully and print nothing on the screen.


The above code compiles fine and when run, produces the output:
nullnull
on the console. (Option 3)

I tried Case 2, just out of my curiosity to check if the behaviour is any different.

Can somebody please explain why is it not lead to NullPointerException. (Option 2)

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20991
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

Please quote your sources. Thanks.

Henry
 
Rakesh Ne
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Rakesh,

Please quote your sources. Thanks.

Henry


Source : JavaBeat
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rakesh,

public class DemoNullStringConcat{/*//Case 1:static String s1;static String s2;*/public static void main(String args[]){//Case 2 :String s1= null;String s2= null; s2 = s1+s2;System.out.println(s2);}}

--------------------------------------------------------------------------------



Attempting to compile and run the code
1. Will cause a compilation error.
2. Runtime Execption - NullPointerException in the 2nd line of the main method.
3. Will compile successfully and print nullnull on the screen.
4. Will compile successfully and print an empty line on the screen.
5. Will compile successfully and print nothing on the screen.


The above code compiles fine and when run, produces the output:
nullnull
on the console. (Option 3)

I tried Case 2, just out of my curiosity to check if the behaviour is any different.

Can somebody please explain why is it not lead to NullPointerException. (Option 2)


"+ is a String concatenation operator apart from being addition operator"
If any of the operand of the bibary "+" concatenation operator is String simply String concatenation is done. Resultantly you got the output nullnull.

Class member variable get their default values unlike case of the local primitives or reference variables, where we have to initialize them before using. In your case 2 says, s1 and s2 get their default value that is null, so that is faultless to concatenate two String.

You try out the same operation by making the s1 and s2 a wrapper. You will get the NullPointerException. But if any of the operand is String, the String concatenation will be done.

Integer i1=null;
Integer i2 = null;
i1=i1+i2; // this causes NPE
System.out.println(i1);

Regards,
cmbhatt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic