Loading...

Java Return Values

Write a method named twice that takes an int named n and sends back twice that number. The header starts with int, not void: static int twice(int n). Inside the braces write return n * 2;. In main, print the result of the call: System.out.println(twice(7));. The screen should show one line: 14. twice itself does not call println. It only hands 14 back. main prints that number. That hand-back is what this page teaches.

Hand a number back to main

On the parameters page, greet and show printed inside the method body. The call sent a value in, and the method wrote to the screen. Here the method still receives a value, but it also sends a value out. The word return marks that outgoing value. main can store it or print it.
Create a small program file named Box.java. The public class on the first line must match the file name: public class Box. Do not leave an older Tip or Chime class name in this file; the compiler expects the class name and the file name to agree.
Outside main, still inside the class braces, write:
static int twice(int n) {
return n * 2;
}
The header no longer says void. It says int because the value that comes back is an int. Between the parentheses, int n is still a parameter — same idea as the last page. The body has one statement: return n * 2;. That multiplies n by 2 and sends the product back to whoever called twice. There is no System.out.println inside twice on this page. Save the file before you compile.

Box.java with twice

This is the full small program. twice computes. main prints.

twice returns n times 2

java
public class Box {
    public static void main(String[] args) {
        System.out.println(twice(7));
    }

    static int twice(int n) {
        return n * 2;
    }
}

From the folder that holds Box.java, run javac Box.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Box — not java Box.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. The program should print one line and stop: 14. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep System.out.println(twice(7)); plus the twice method above. A wrong screen line does not mean you need a new JDK install.

Walk the run once in order. Java starts at main. The call twice(7) sends 7 into n. Inside twice, return n * 2; computes 14 and hands that 14 back to main. Then println writes 14. twice never wrote to the screen by itself. If you change the call to twice(3), the screen should show 6. The method body stays the same; only the argument changes, and the returned product changes with it. If the screen shows 7 instead of 14, you printed n inside twice or printed the argument in main without calling twice — put the doubling on the return line and print the call’s result.
You can also keep the returned value in a name inside main before you print:
int result = twice(7);
System.out.println(result);
That still shows one line: 14. The call finishes first and fills result. Then println writes result. Both shapes — printing the call directly, or storing then printing — use the same return from twice.

Print the value main received

Change only the number in the call if you want a different screen line. Leave return n * 2; alone. Compile and run again after each change: javac Box.java, then java Box. twice(0) should print 0. twice(10) should print 20. Each time main prints whatever twice handed back.
If twice still says void in the header but the body has return n * 2;, javac writes an error — void means nothing comes back, so a return with a value does not fit. If the header says int but the body has no return, javac writes an error too — an int method must send an int back on every path this beginner shape uses. If the body prints and never returns, you are back on the Tip page’s idea; this page wants the number in main’s hands.

void versus a typed return

Read the two shapes side by side. A void method runs its body and ends. A method with a typed return must hand a value back with return. main treats the call as that value when the return type is not void.

📊 void body versus return

Shape What the method does What main often does
void show(int n) prints inside the body calls show(9); and may print nothing else
int twice(int n) return n * 2; prints or stores twice(7)

A common mix-up is putting System.out.println(n * 2); inside twice and leaving the header as int without a return. Then javac complains, or you change the header back to void and you no longer have a value for main to print from the call. Another mix-up is writing return; with no value while the header says int. Keep return n * 2; so the type of the returned expression matches int. Writing System.out.println("twice"); in main prints the word twice; it does not call the method. Keep println(twice(7)) when you want 14 on the screen.

A leftover Tip greet demo that only prints hi lena inside the method can sit in the same file and confuse the screen. Clear main down to the println that uses twice, and keep one method that returns. Do not nest twice inside main’s braces. Methods sit inside the class, next to main. Keep twice’s braces closed before the class ends.

Before overloading

You can write static int twice(int n), return a computed value, and print that value from main. The screen shows the number main received, not a line the method printed on its own. void is the wrong header when you need a value back. The next page is overloading, where more than one method can share a name when their parameter lists differ.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top