In most of the laboratories and projects, you are asked to output
objects "using the toString method." For example, step
3 in the CoinCounter lab says to: "Output the new status
of counter using the toString method." This
can be done in three different ways.
Assign the result of calling the toString method to
a String variable, followed by printing the variable with
System.out.println.
String counterString = counter.toString();
System.out.println("counter is " + counterString);
Call the toString method in the parameter
of System.out.println.
System.out.println("counter is " + counter.toString());
Java has a special shortcut for calling the toString
method. When you place an object variable in
a System.out.println without any method, Java automatically
calls the toString method.
System.out.println("counter is " + counter);
Any of three ways are correct, but programs will generally use the
last one because it requires less typing.