This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sting null value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Sting null value" Watch "Sting null value" New topic
Author

Sting null value

Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 55
public class One
{
String s1, s2;
public static void main(String... a)
{
One o = new One();
o.test();
}
public void test()
{
System.out.println(s1+s2); //Line 1
System.out.println(s1.length()); // Line 2

}
}
Output:
nullnull
Runtime exception
Question is: how the concatination does not throw any runtime exception
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5888
    
    6

The concatenation by the + operator ultimately calls String.valueOf() (or something equivalent) which does, effectively


That is, the + operation invokes a method that checks for null and turns it into the String "null".
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Ah..
Sorry i replied something wrong thats why i am editing that to this.
Original post was-


Yesterday 23:55:45 Subject: Sting null value
public class One
{
String s1, s2;
public static void main(String... a)
{
One o = new One();
o.test();
}
public void test()
{
System.out.println(s1+s2); //Line 1
System.out.println(s1.length()); // Line 2

}
}
Output:
nullnull
Runtime exception
Question is: how the concatination does not throw any runtime exception


because when you concate through + operator it does not call to any method.
And i am Totally wrong.
Sorry jeff.


OCPJP 6 86%
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5888
    
    6

Nikhil Sagar wrote:
because when you concate through + operator then it does not calls to any method.


It does call a method. But it doesn't call it on the null reference.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Jeff Verdegan wrote:The concatenation by the + operator ultimately calls String.valueOf() (or something equivalent) which does, effectively


That is, the + operation invokes a method that checks for null and turns it into the String "null".


Hey jeff, can you please tell me what is that "something equivalent" is exactly ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5888
    
    6

Nikhil Sagar wrote:
Jeff Verdegan wrote:The concatenation by the + operator ultimately calls String.valueOf() (or something equivalent) which does, effectively


That is, the + operation invokes a method that checks for null and turns it into the String "null".


Hey jeff, can you please tell me what is that "something equivalent" is exactly ?


No, I can't. I would assume that it calls String.valueOf(), but that's not specified and it's not required. Since we're not getting NPE, though, we can deduce that it must be calling that method, or doing something equivalent to what that method does, but there's no way to know for sure without looking at the compiler source or maybe the javap output, and I'm not inclined to do either one.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

i found something...

The JLS, Section 15.18.1.1 requires this operation to succeed without failure:

...Now only reference values need to be considered. If the reference is null, it is converted to the string "null" . Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

How does it work?

Let's look at the bytecode! The compiler takes your code:


and compiles it into bytecode as if you had instead written this:


(You can do so yourself by using javap -c)

The append methods of StringBuilder all handle null just fine. In this case because null is the first argument, is invoked instead since StringBuilder does not have a constructor that takes any arbitrary reference type.

If you were to have done s = "hello" + s instead, the equivalent code would be:


where in this case the append method takes the null and then delegates it to

Note: String concatenation is actually one of the rare places where the compiler gets to decide which optimization(s) to perform. As such, the "exact equivalent" code may differ from compiler to compiler. This optimization is allowed by JLS, Section 15.18.1.2:

To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

The compiler I used to determine the "equivalent code" above was Eclipse's compiler, ecj.

Source- webpage
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Please tell me one thing that
Now only reference values need to be considered. If the reference is null, it is converted to the string "null"

Is it true for all reference variables or only for String ??
because..


Prints null too.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
If you look in the documentation for [System.out].println(Object), it is all explained there.
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 55
Jeff Verdegan wrote:The concatenation by the + operator ultimately calls String.valueOf() (or something equivalent) which does, effectively


That is, the + operation invokes a method that checks for null and turns it into the String "null".

Thanks for the reply. Than you very much
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5888
    
    6

Nikhil Sagar wrote:Please tell me one thing that
Now only reference values need to be considered. If the reference is null, it is converted to the string "null"

Is it true for all reference variables or only for String ??


Does it say, "If the reference is null, it is converted"? Or does it say, "If the reference is null, it is converted, but only if it was declared to be of type String"?
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Campbell Ritchie wrote:If you look in the documentation for [System.out].println(Object), it is all explained there.


Thanks again Sherrif.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
I presume that answered your question . . . and “you’re welcome”
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Campbell Ritchie wrote:I presume that answered your question . . . and “you’re welcome”

Because you already knew that i am smart enough to understand that by my own after having your link's great help. Isn't that it Sherrif ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4

It is a lot better when people find the information for themselves and read it.
Even if you don’t remember it this time, you will know where you can read it and look it up next time
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Campbell Ritchie wrote:
It is a lot better when people find the information for themselves and read it.
Even if you don’t remember it this time, you will know where you can read it and look it up next time

Yes, i will keep that in mind till the end of this world (or mine either).
Well, today is the best day of my life, i got my first job as a java developer in a reputed company.
I would really really like to say thank to all of the Rancher society and specially to you Sherrif and of-course to my favorites Jeff and Winson.
You all helped me to make my concepts clear and stronger that is why they selected me on the basis of my knowledge not on my grades.
God Bless You Guys.


Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

Congratulations with your new job Nikhil!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Jesper de Jong wrote:Congratulations with your new job Nikhil!

Thanks a lot Jesper.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5888
    
    6

Congratulations! Glad I was able to help.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Jeff Verdegan wrote:Congratulations! Glad I was able to help.

Thanks a ton jeff.
And I love you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Congratulations
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Campbell Ritchie wrote:Congratulations

Thank you so much Sherrif.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
LOL! Congrats!
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Praveen Kumar M K wrote:LOL! Congrats!

Thank you Paveen.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4756
    
    7

Nikhil Sagar wrote:Thanks a ton jeff.
And I love you....

Steady on. You don't even know if he snores.

However, hearty congratulations. Just remember: the first 10 years are the toughest.

Winston
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Winston Gutkowski wrote:
Nikhil Sagar wrote:Thanks a ton jeff.
And I love you....

Steady on. You don't even know if he snores.

However, hearty congratulations. Just remember: the first 10 years are the toughest.

Winston


Thank you Winson.
Just remember: the first 10 years are the toughest

And i would really like to face them.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 163

Dude, congrats for the job. Enjoy Java [companies can rarely be (oops! did I say "rarely"? )]

See the subject once sting a null
This is a null->Job thread now!


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Sting null value
 
Similar Threads
doubt in using String
String Objects
String
how many String objects are created
Strings