• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sting null value  RSS feed

 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the documentation for [System.out].println(Object), it is all explained there.
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you look in the documentation for [System.out].println(Object), it is all explained there.


Thanks again Sherrif.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume that answered your question . . . and “you’re welcome”
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations with your new job Nikhil!
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Congratulations with your new job Nikhil!

Thanks a lot Jesper.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations! Glad I was able to help.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Congratulations! Glad I was able to help.

Thanks a ton jeff.
And I love you.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Congratulations

Thank you so much Sherrif.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL! Congrats!
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen Kumar M K wrote:LOL! Congrats!

Thank you Paveen.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!