class Test001 { int i; public Test001(int i) { this.i = i; } public String toString() { if(i == 0) return null; else return "" + i; } public static void main(String[ ] args) { Test001 t1 = new Test001(0); Test001 t2 = new Test001(2); System.out.println(t1); //1 System.out.println(t2); //2 } } Why //1 will throws Exception as is while will print "null" if t1 is replaced with t1.toString()?? Isn't it true that the println will call toString method for t1 implicitly anyways??? can someone explain?
Usha Damarla
Greenhorn
Joined: Aug 29, 2001
Posts: 13
posted
0
Hi Ernst, You got this question from Guoqiao Sun's mock exam site, right. Infact the toString() method gets called here. Guoqiao actually forgot to concatenate "null" with an empty string. The original stmt if(i == 0) return null; was actually returning a null value instead of a string, causing it to fail at runtime. Change the code like this and try again. public String toString() { if(i == 0) return ""+null; else return "" + i; }
Guoqiao, its ur wish to modify either question or your explanation.
Regards Usha ps: Removed the < PRE > tags in the post since it was difficult to read. - satya
[This message has been edited by Madhav Lakkapragada (edited December 18, 2001).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Helo! Deeper explanation may be found on Sun sources?! But yet the solution presented by Usha works alright as well as printing object like this: System.out.println( "" + t1 ); ------------------ Antti Barck It Solutions Consultant -- NSD Oy Sun Certified Programmer for the Java� 2 Platform
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Ernest Check out this thread here It looks like the same question.
------------------ Dave Sun Certified Programmer for the Java� 2 Platform
Dave
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
This program shows the difference
This the output from javap for the line //1 15 invokevirtual #6 (Method void println(java.lang.String)) and this for the other 22 invokevirtual #7 (Method void println(java.lang.Object)) println(String) was written for returning "null" whenever the argument was null. But println(Object) was not written for providing a "null" String to the write method when the method toString() of the Object argument returned null.
SCJP2. Please Indent your code using UBB Code
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Excellent, Jose... I am in the clear now -Shyam [This message has been edited by Shyamsundar Gururaj (edited September 05, 2001).]
marilyn murphy
Ranch Hand
Joined: Aug 28, 2001
Posts: 83
posted
0
Thank you, Jose. That clarifies the answer for me.
Nguyen Trung
Greenhorn
Joined: Aug 16, 2001
Posts: 6
posted
0
Hi Jose, Please give more explanation to a novice.still don't understand what you said about println method. System.out.println((Object)null) ; Why does the above stmt give null as output ? thanx
Cameron Park
Ranch Hand
Joined: Apr 06, 2001
Posts: 371
posted
0
Basically Jose was saying the two overloading methods println(String) and println(Object) println(String) will send null to the writer if the argument is null. On the other hand, println(Object) will not send null to the writer when the Object's toString() returns null. HIH.
MONZY THARIAN
Ranch Hand
Joined: Feb 21, 2001
Posts: 63
posted
0
Thanks Jose.The explanation though simple was very tricky
Think Big . So shall you become big.
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
quote: -------------------------------------------------------------------------------- Originally posted by Jose Botella: But println(Object) was not written for providing a "null" String to the write method when the method toString() of the Object argument returned null. --------------------------------------------------------------------------------
Hi I am pasting source code for println(Object obj): from System.java : public final static PrintStream out = nullPrintStream(); -------------------------------------------------------------- from PrintStream.java : public void println(Object x) { synchronized (this) { print(x); newLine(); } public void print(Object obj) { write(String.valueOf(obj)); } ----------------------------------------------------------------- from String.java : public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
Now will any one please tell me why NullPointerException should be thrown? Thanks in adv
------------------ Regards Ravish
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
Now will any one please tell me why NullPointerException should be thrown? NullPointerException is a runtime exception and hence doesn't have to be part of the method declaration, if that's what you are trying to point in the code. regds. - satya
Originally posted by Madhav Lakkapragada: if that's what you are trying to point in the code. regds. - satya
No, I am trying to say that : if Object type refernce is null still String "null" should be returned.(in case of System.out.println(someObjRefWhichIsEqualToNull)) if you follow the method calls, you will find no where null.anyMethod() has invoked. Every time you are suppose to get "null" String(in case of null) but still we get NullPointerException. TIA
------------------ Regards Ravish
sun ram
Ranch Hand
Joined: Dec 18, 2001
Posts: 61
posted
0
stack trace of this program. java.lang.NullPointerException at java.io.Writer.write(Writer.java:129) at java.io.PrintStream.write(PrintStream.java:267) at java.io.PrintStream.print(PrintStream.java:426) at java.io.PrintStream.println(PrintStream.java:563) at Test001.main(Test001.java:15) See Control goes to java.io.Writer.write method. In write method str.length() call throws the Nullpointer Exception. /** * Write a string. * * @param str String to be written * * @exception IOException If an I/O error occurs */ public void write(String str) throws IOException { write(str, 0, str.length()); }
SCSecA,SCNA,SCSA,SCWCD,SCJP
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
but why should write(String str) throw an exception. as >> at java.io.PrintStream.print(PrintStream.java:426) public void print(Object obj) { write(String.valueOf(obj)); } will call String.valueOf(obj) which is in turn is: public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } So if obj is null then it will return "null". NOW when write(String str) is called: public void write(String str) throws IOException { write(str, 0, str.length()); } str.length() should return 4 not NullPointerException. I think I have cleared my doubt now ..
------------------ Regards Ravish
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
okay, I see your point..... Here is how it all goes. The principal difference is where the obj.toString() gets called?.
Case 1: Line 1 executes like this.........
System.out.println(t1.toString());// Line 1 Since the class over-rides the toString() method, the call becomes: System.out.println(null); //null - Object reference Following the code all the way to String.java
The String literal "null" is returned (because our pbject is null and successfully printed. Line 2 explanation goes like this......... On the other hand, System.out.println(t1);//NullPointerException Line 2 Here the obj reference is t1 and is NOT null. Hence when you trace the code all the to String.java, the if stmt decides to to call the toString() method. Since the class of the object (Test001) over-rides this method and return null as a String object reference, we get the NullPointerException. However, if you want the behavior you are expecting, try this code......
NOw observe that the toString() method of the Test001 class returns a String with the literal "null" and NOT a null object reference. Thus you will avoid the NullPointerException. HTH. - satya
Edited: ps: While posting this msg, I missed your earlier post and "sun ram" post above. If your doubt is cleared, feel free to ignore my msg. regds. - satya
[This message has been edited by Madhav Lakkapragada (edited December 18, 2001).]
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
Thansk a lot .. got my error too.. t1 is not null.... toString() returns null.. so to call to write() will throw NPExcep....