| Author |
when toString() is implicitly called
|
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
Sir , i want to know when toString() method of String class is implicitly called.....
this gives output nullnullnullnull
|
SCJP1.6,SCWCD1.5
|
 |
Afzal Rehman
Ranch Hand
Joined: Dec 16, 2009
Posts: 35
|
|
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.
|
SCJP 6.0
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
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),...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
but how like this null.toString();
|
 |
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
narendra wrote:how it is possible to call null.toString() method.......
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
narendra wrote: thank you sir i am very greatful to you...
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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!
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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
Joined: Oct 13, 2009
Posts: 771
|
|
Great insight Henry. Didn't know it before.
Best Regards,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
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
Joined: Feb 17, 2010
Posts: 66
|
|
|
thanks to Henry Wong sir he has certainly great knowledge in java ...
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Thanks Henry Wong, for your information!
|
 |
 |
|
|
subject: when toString() is implicitly called
|
|
|