• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Strings  RSS feed

 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I here tried to manipulate the strings. Here the variable test2 assigned with null+test1. I am not clear how the value concatenate and prints as nulltest.

Code:
String test1 = "test";
String test2 = null + test1; //line
System.out.println(test2);

Output:
nulltest


Can any one please help me in clarifying my doubt..
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds standard practice for Strings; if you try to print a String which is null it produces the word "null." You are getting the same sort of phenomenon with concatenation.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String test2 = null + test1; //line

How does the + operator works...
I thought,when it is overloaded, it would be expecting two objects...
Wherein the leftside operand calls the overloaded method with the right side operand as argument.
Please correct me if i am worng...
[ November 27, 2008: Message edited by: James Tharakan ]
 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The way the strings handled null value is completely different. I was in a mess what had happened to the string.

String test2 = null+"test"

So you both mean that null in the statement will be treated as a string and in operator overloading the value is concatenated and printed as nulltest.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Specification
The string concatenation operator + (�15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Tharakan:
String test2 = null + test1; //line

How does the + operator works...
I thought,when it is overloaded, it would be expecting two objects...
Wherein the leftside operand calls the overloaded method with the right side operand as argument.
Please correct me if i am worng...



I don't know if this is useful -- as this is the beginners forum. (and certain people already complained that I give too much detail for the beginner forum... )

The compiler actually converts the string concats to method calls. Basically...

String test2 = null + test1;

Is syntactic sugar for...

String test2 = new StringBuilder().append(null).append(test1).toString();

Or close to it. I'm sure I am missing something (like a cast or something).

Henry
[ November 27, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need casts; toString is a java.lang.Object method, so no cast is required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!