javaconstructorreferenceheap-memoryjava.util.date

How new Java.util.Date() works while other classes return just reference?


I am curious as to why new Java.util.Date() returns Date object but not address(reference) to itself on heap:

System.out.println(new Date()); //Should print address(reference)

of object on heap?

like every other class I've been learning about for example:

Cat cat = new Cat();  //new Cat() returns reference which is stored in cat;

how can I implement it in my classes?


Solution

  • java.util.Date, like a lot of other classes in the JDK, overrides toString(), which allows you to control the string representation of your objects.

    You can, of course, do this for your own classes too.