Java Programming I

Course Home

Java Programming II

Course Home


Start Here

JoC Help

Bonus Videos

Quiz Reviews

Java Tutorial Trail: Learning the Java Language

CodingBat Warmups




AP Computer Science

How do I...

Related Links

Non-void Return Types

This video explains how to use methods with return types.

Methods with void return type

Some methods have void return types. For example, the method header for the turn method is:

void turn(int)

The void keyword before the name of the method tells us the return type.

Methods with void return types are like commands. They tell an object to do something. The act method below calls the turn method.

        public void act()

Try out this method. You'll see that the turtle turns 5 degrees every time you press the Act button.

Methods with non-void return type

Methods with other return types (like int or boolean) are like questions. They ask an object for information, but they don't tell the object what to do with that information.

For example, the method header for the getX method is:

int getX()

The int before the method name tells us that the method returns an integer. For example, if the X coordinate of the object is 327, the method will return an integer of 327. This information may be useful, but just calling this method doesn't tell the object how to use the information.

Try this code:

        public void act()

Notice that nothing happens when you press the Act button. The turtle has retrieved the correct information (its X coordinate), but it doesn't know what to do with that information. Since getX returns an int, you can use that int any way you like. For example:

        public void act()

Notice that turn, move, setRotation, and setLocation all have void return types and accept int parameters. Notice also that we used getX twice for our call to setLocation. The method header for setLocation is void setLocation(int,int), which means it needs two ints for input.

When you understand how return values are used, take this quiz. Retake as needed to get a perfect score.


CodingBat is a special environment for writing and testing methods with non-void return types. For more on CodingBat, see the Introduction to CodingBat video on the Writing Methods page.