• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock exam question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question in one of the mock exams.
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
}
}
The answer is : throws exception at runtime.
can some please explain this to me?
thanks
bani
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because it's wrong. Where did you find this question? The program prints
null
2
If the idea is that System.out.println(x) is
supposed to throw a NullPointerException
if x.toString() returns null.... well, it doesn't. It prints the string "null".
[ August 16, 2002: Message edited by: Ron Newman ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems PrintStream has only the following methods:
PrintStream.println(), PrintStream.println(boolean), PrintStream.println(char), PrintStream.println(char[]), PrintStream.println(double), PrintStream.println(float), PrintStream.println(int), PrintStream.println(long), PrintStream.println(java.lang.Object), PrintStream.println(java.lang.String)
There's no PrintStream.println(null).
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not calling println(null) ... although it's perfectly safe and OK to do so.
You're calling println(Object) , which in turn is calling your object's toString() method, which is returning null. At that point it's smart enough to print the string "null" rather than throwing a NullPointerException.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question was giving me trouble, too. I still am unsure. The question is from JIRIS.com
toString returns a String. I believe when you attempt to print a null reference that the PrintStream System.out calls toString and then toString will send the string "null" back to System.out. But, in this case toString is actually returning the value null, rather than the string "null". toString is supposed to return a String. So, I would expect that both
System.out.println(t1);
System.out.println(t1.toString());
give errors. But, a bug in the JDK allows the first one to work. Well, any insight on if I am right/wrong will be greatly appreciated.
Here is the explanation following that question at http://www.jiris.com/scjp2/mockexam1answer.html:
: It will throw the following exception at runtime at //1.

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:18)
But it is strange that
System.out.println(t1.toString());
will run properly with output null.
--------------
Words from Adrian Uribe about this question:

Question 1 on this mock exam seems to address a bug in the jdk. The explanation shows what is output from running the code but it does not seem logical that this is what should happen.
-------------
Words from Dallas Hockley about this question:

I agree that that seems to be an inconsistency or a bug on the JDK.
System.out.println(t1.toString()) should be the same as
System.out.println(t1) as I understand the automatic typecast/method
call in that situation. I've verified it on 1.3.1 on Windows, and on
1.4 Beta 2 on Windows. Very odd.
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question on jiris.com
I was thinking the pgm will print null and 2
but the explanation given by jiris is entirely different.
Its strange that
System.out.println(t1.toString());
will run properly with output null.
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i change line 1 to
System.out.println("" + t1)
then i getting the result as "null" .
In case of line 1 ie
System.out.println(t1)

Here System.out.println method will try and call toString method on null. Which results in a null pointer exception!
Correct me if i am wrong.
bani
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran this on Java 1.1.8 on MacOS 8.6. Are you saying that later versions of Java act differently?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out is a PrintStream.
From the J2SE APi documentation at
http://java.sun.com/j2se/1.4/docs/api/java/io/PrintStream.html:


public void println(Object�x)
Print an Object and then terminate the line. This method behaves as though it invokes print(Object) and then println().
public void print(Object�obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.


OK, let's check out the documentation of
String.valueOf(Object) at
http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html :


public static String valueOf(Object�obj)
Returns the string representation of the Object argument.
Parameters:
obj - an Object.
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.


This seems to leave open to question (and maybe to implementation?) exactly what happens when the null pointer "is translated into bytes according to the platform's default character encoding, and these bytes are written".
[ August 16, 2002: Message edited by: Ron Newman ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All:
I have jdk1.3.1_01. The following line:
System.out.println(t1)
gives run time error. I think the overriding
toString method is returning null reference instead
of a string which is probably a valid return
type for toString method. But when println()
tries to print a null reference, it causes a
run time error. However following statements:
System.out.println(""+t1)
will call toString method second time on t1. The
second time null reference will become "null". I
guess same is the case with :
System.out.println(t1.toString)
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone who has 1.4 installed please run the program and tell us what it does? Also tell us what sub-version of 1.4, and what OS it's running on.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, my system is running j2sdk1.4.0_01 and the following result I got when executing the commands:

1. System.out.println(t1) // 1
// compiler did not complain, but a NullPointerException was thrown during runtime
2. System.out.println(t1 + "")
OR
System.out.println(String.valueOf(t1))
//the above commands had the same effect and it print:
null
2
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With JDK 1.4 beta I get the exception
The jdk version info is
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
I am running it on Windows 2000 machine
The exception is
java.lang.NullPointerException
at java.io.Writer.write(Writer.java:129)
at java.io.PrintStream.write(PrintStream.java:306)
at java.io.PrintStream.print(PrintStream.java:465)
at java.io.PrintStream.println(PrintStream.java:602)
at com.rahul.certify.java.TestToString.main(TestToString.java:27)
Exception in thread "main"
The code I used to test is
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic