• 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

why new date() prints date ?????

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In java new Date() prints the current date with time. but when i try to print any of class object it prints the memory location i.e(new Test() prints test@2e3ee...). So why is so, because both are objects why only date object prints date not the memory location.....

thanks
mano
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the Date() class overrides Object.toString(). If a class doesn't override this method, the implementation defined on Object is called, which just prints an object's class name and memory location.
[ March 16, 2008: Message edited by: Dan Murphy ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you print an object, println() calls String.valueOf(theObject), which calls theObject.toString() if "theObject" is not null. Therefore, if a class overrides the toString() method to return something interesting, that's what you see when you print the object. The "memory address" (actually not, but that's another story) is what java.lang.Object.toString() returns, so it's what you get by default for a class that doesn't override this method.
 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for your reply... one more question in java..
what is the difference between tostring() method which is present in object class and toString() method in String class... i.e(Object.tostring() and String.toString()).. why java has two tostring() methods implemented in java..
why cant any one be used???

thanks
mano
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String toString() is overriding Object toString() method.
 
mano ranjan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for your reply, So what is the main difference between these two methods . in what situation they are used for???

thanks
mano.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString is used to display information on an object. Now for Object there is no data to show, so Sun has decided the method should show the class name and the hexidecimal hash code. For String however, there is something to show - the contents of the String. Therefore, toString returns the String itself. Similarly, for most Collection classes, toString returns a String starting with [ followed by all elements separated by a , and ending with a ].

If you create a new class, it inherits toString from its super class. If you have extra data to show you can choose to override toString to display the data you feel is more appropriate. For a Person class, that could be the name.
 
reply
    Bookmark Topic Watch Topic
  • New Topic