This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Doubt in Strings  RSS feed

 
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..
 
Marshal
Posts: 60073
188
  • 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.
 
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.
 
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

 
author
Sheriff
Posts: 23586
138
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: 60073
188
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!