Skip to main content
Engineering LibreTexts

2.5: Displaying Objects

  • Page ID
    15281
  • 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);
        System.out.println(time);
    }
    

    The output will look something like:

    Time@80cc7c0
    

    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) {
        System.out.print(t.hour);
        System.out.print(":");
        System.out.println(t.minute);
        System.out.print(":");
        System.out.println(t.second);
    }
    

    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) {
        System.out.printf("%02d:%02d:%04.1f\n",
            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”.