Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Doubt regrding toString()

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Everyone,
I got my answer.

Meghana
 
The City calls upon her steadfast protectors. Now for a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic