aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null String Concat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null String Concat" Watch "Null String Concat" New topic
Author

Null String Concat

Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14


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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Rakesh,

Please quote your sources. Thanks.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
Originally posted by Henry Wong:
Rakesh,

Please quote your sources. Thanks.

Henry


Source : JavaBeat
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null String Concat