This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt regrding toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regrding toString()" Watch "Doubt regrding toString()" New topic
Author

Doubt regrding toString()

Meghana Ghanekar
Greenhorn

Joined: Oct 24, 2005
Posts: 6
Hi!
While developing the class for Account Holder. I am trying to contain an object of Date class inside the Account Holder object. the Code is as follows:
class Date
{
private int dd,mm,yy;

public Date()
{
dd = mm = yy=1;

}
public Date(int d,int m,int y)
{
dd = d;
mm = m;
yy = y;
}
public String toString()
{
return dd+"/"+mm+"/"+yy;
}

public void disp()
{
System.out.println(dd+"/"+mm+"/"+yy);

}
}


class AccHolder
{
int emp_id;
String name;
Date jng_dt;
float sal;

public AccHolder()
{
}

public AccHolder(int i,String n, Date j,float s)
{
emp_id = i;
name = n;
jng_dt = j;
sal = s;
}

public String toString()
{
return emp_id+"/n"+name+"/n"+jng_dt+"/n"+sal;
}

public void disp()
{
System.out.println(emp_id+"/n"+name+"/n"+jng_dt+"/n"+sal);
}

public void disp1()
{
System.out.println(emp_id+"/n"+name+"/n"+sal);
jng_dt.disp();
}
public static void main(String[] args)
{
AccHolder a1 = new AccHolder();
System.out.println(a1);
/* for a1.jng_dt is displayed as null */
AccHolder a2 = new AccHolder();
a2.disp();
/* for a2.jng_dt is displayed as null */
AccHolder a3 = new AccHolder();
a3.disp1();
/* Runtime exception thrown for jng_dt as it is null*/

}
}

If it is said that when we pass Object directly to the System.out.println(). Then internally toString for that Object is called which is a method call. Then why disp() of date generates Exception @ runtime. also if we explicitly call toString() of Date as (jng_dt.toString()) in disp() of Account Holder runtime exception is generated.
Can u help me out for this ? I cannot understand this problem.
thanx,
meghana
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
If you call jng_dt.disp() and jng_dt is null than there is exception, you cannot do a call on non existing object.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Meghana Ghanekar
Greenhorn

Joined: Oct 24, 2005
Posts: 6
Hi!
I agree with u. It will generate an exception. But it is said that when u pass an object to system.out.println() then implicitly tostring() on that object is called. In that case no Exception is generated. but when i try to call it explicitly call it for the object Exception is generated.
i can't understand why it is like this?


Warm Regards,
Meghana
jayram joshi
Greenhorn

Joined: Aug 05, 2000
Posts: 23
Meghana

The secret lies with the println() method code. println(String x) method from PrintStream calls



Thats why you dont get a NullPointerException but instead a "null" String gets appended
[ October 26, 2005: Message edited by: jayram joshi ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You can answer your question using the javadoc. Here is how to do it:

System.out is of type java.lang.PrintSteam.
The PrintStream method you are invoking is println(Object).
In its javadoc, println states that it calls print(Object) then println().
The javadoc for print(Object) states that it uses String.valueOf(Object).
And finally, the javadoc for valueOf(Object) states:

Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

Got that? println(obj) --> print(obj) --> valueOf(obj) --> (obj==null? "null" : obj.toString())
and that is why System.out.println doesn't throw a NullPointerException when you pass it a null pointer,
and along the way, you might have learned something about String.valueOf(Object),
too, which is a useful method in its own right.
[ October 26, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14279
    
  21

As a side note: The standard Java API already contains two Date classes (java.util.Date and java.sql.Date). Are you sure you want to write your own Date class?

To format Date objects for display, there's also java.text.SimpleDateFormat.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Meghana Ghanekar
Greenhorn

Joined: Oct 24, 2005
Posts: 6
Thanx Everyone,
I got my answer.

Meghana
 
wood burning stoves
 
subject: Doubt regrding toString()