File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when toString() is implicitly called

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir , i want to know when toString() method of String class is implicitly called.....

this gives output nullnullnullnull



 
Afzal Rehman
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You override the toString method when you are more conern to read about your object of your class.
When you pass the Object reference in System.out.println then toString method is called and gives you appropriate result.
checkout toString method.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra were you expecting a NullPointerException?? Using + operator on null references produce "null" as output. From JLS
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),...
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how like this null.toString();
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra wrote:how it is possible to call null.toString() method.......
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra bhattacharya wrote:how it is possible to call null.toString() method.......


It not... what happens is that when you do object concatenation, it uses a string builder (or prior to java 5, a string buffer). The append() methods of the string builder will check to see if the object is null. And if it is, it will be replaced with a string with the value "null".

Henry
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narendra wrote: thank you sir i am very greatful to you...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
narendra bhattacharya wrote:how it is possible to call null.toString() method.......


It not... what happens is that when you do object concatenation, it uses a string builder (or prior to java 5, a string buffer). The append() methods of the string builder will check to see if the object is null. And if it is, it will be replaced with a string with the value "null".

Henry


In this case we use String, not StringBuilder or StringBuffer! Then how the method append() will be invoked on 'null'?

Thanks in Advanced!
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
In this case we use String, not StringBuilder or StringBuffer! Then how the method append() will be invoked on 'null'?


That is how it works. The compiler uses StringBuilder (or StringBuffer) to do string concats. Meaning this...

String result = string1 + string2 + string3;

will be replaced with...

String result = new StringBuilder().append(string1).append(string2).append(string3).toString();


Henry
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great insight Henry. Didn't know it before.

Best Regards,
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narendra wrote: thank you sir you have cleared my doubt you are absolutely right ......sir java has many things to learn how you are able to memorise it....[ /quote]
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to Henry Wong sir he has certainly great knowledge in java ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong, for your information!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic