• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

null printing

 
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 didn't understand why this example works fine
<code>
public class Test009 extends T
{
public static void main(String args[])
{
Test009 t = null;
T tt = (T)t;
System.out.print(t);
System.out.print(" ");
System.out.println(tt);
}
}
class T{}
</code>
And this one giver nullPointerException when runs??
<code>
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);
System.out.println(t2);
}
}
</code>
Can anyone explain me that?!
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plucked from jiris.com:

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. Comments?


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.


In other words, it's a bug with the implicit toString() method called by the println() method.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods print and println are written to know how to deal with a parameter pointing to null. Say something like:
if(parameter == null) parameter = "null";
This explains why the first program runs ok.
In the second program this doesn't apply because the parameter isn't null, but toString is called returning null. The next calls in the print method simply weren't protected against receiving a null parameter as print was.
Thinking in the exam I think you should only be aware of the first point.
 
I like tacos! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic