• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt in Strings

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74606
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need casts; toString is a java.lang.Object method, so no cast is required.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic