aspose file tools*
The moose likes Java in General and the fly likes Why does String null work this way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why does String null work this way?" Watch "Why does String null work this way?" New topic
Author

Why does String null work this way?

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Hi All,

Here below is the code snippet...

String a = null;
String b = null;
String c = a+b;
System.out.println(c);

The above snippet will print "nullnull" as the output.
Can somebody explain me the scenario and why this is happening this way???

Thanks in advance
Regards,
Sriram
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

From the API docs for String#valueOf(Object obj)
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Aditya Kanitkar
Ranch Hand

Joined: Aug 08, 2009
Posts: 72
When you write like this

String some= Some_String + Some_Other_String;

The (+) symbol will CONCATENATE any value or null in the given strings.

As your both strings have NULL assigned to it. It'll just return null and append
it with previous string.
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Manish, Thanks for your reply... Still I am not able to get a clear understanding as why this is happening so.
Aditya, When there is no string existing for the two given variables (a and b), how come null and null are getting concatenated???

ok... though I did not get the complete meaning of the replies... May I know whats the difference between the code snippet that I pasted earlier and the one here below?

String a = "null";
String b = "null";
String c = a+b;
System.out.println(c);

This also prints "nullnull" only as the output.

I strongly believe that there is something to do with the way the string variable/object are allocated the memory.

char data[] = null;
String a = new String(data);

The above two lines will throw null pointer. That being the case, how is "nullnull" a resulting value???!!!
I am totally lost/confused!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The Java Language Specification explains it : 15.18.1 String Concatenation Operator +

If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (§15.28))that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string. If an operand of type String is null, then the string "null" is used instead of that operand.


[My Blog]
All roads lead to JavaRanch
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Sriram Sharma wrote:Manish, Thanks for your reply... Still I am not able to get a clear understanding as why this is happening so.


What does System.out.println actually do?
Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value,...

What does String.valueOf(x) actually do?
Returns the string representation of the Object argument.if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Great...
Thanks a lot Christophe and Manish.
The link was very helpful infact!

Regards,
Sriraam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does String null work this way?
 
Similar Threads
jq+ code
array...
alternative to I/O
Garbage collector
What does the keyword "final" have to do with compiling this code?