• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regrding toString()

 
Meghana Ghanekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call jng_dt.disp() and jng_dt is null than there is exception, you cannot do a call on non existing object.
 
Meghana Ghanekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meghana Ghanekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Everyone,
I got my answer.

Meghana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic