Skip to main content
Engineering LibreTexts

2.5: Displaying Objects

  • Page ID
  • If you create a Time object and display it with println:

    public static void main(String[] args) {
        Time time = new Time(11, 59, 59.9);

    The output will look something like:


    When Java displays the value of an object type, it displays the name of the type and the address of the object (in hexadecimal). This address can be useful for debugging, if you want to keep track of individual objects.

    To display Time objects in a way that is more meaningful to users, you could write a method to display the hour, minute, and second. Using printTime in Section 4.6 as a starting point, we could write:

    public static void printTime(Time t) {

    The output of this method, given the time object from the previous section, would be 11:59:59.9. We can use printf to write it more concisely:

    public static void printTime(Time t) {
            t.hour, t.minute, t.second);

    As a reminder, you need to use \%d with integers and \%f with floating-point numbers. The 02 option means “total width 2, with leading zeros if necessary”, and the 04.1 option means “total width 4, one digit after the decimal point, leading zeros if necessary”.