✕ Close

Week 16 · 16.4 · Thu, Nov 19 · The Specific Skills + Try Its

Into a Method and Back: Calls, Parameters and Arguments

This section starts with one look back, at joining text and counting its characters. Then it shows the four kinds of steps on today's bench: following a method call in and back, handing values to a method, following a method that calls another method, and checking another student's method against a list of points.

Joining Text and Counting It

Big idea. The + sign joins two Strings exactly as they are, with nothing added between them. A space shows up only when a space is inside the quotation marks, and length() counts every character, spaces included.

When both sides of + are text, Java puts the second piece right after the first. It never adds a space by itself. To get a space, the code joins " ", or puts the space inside the quotation marks of a piece, as in "Go ". The method length() gives the number of characters a String holds. Letters and spaces both count.

Worked example. Trace every box and predict exactly what is printed.

1String first = "Zion";
2String last = "Hill";
3String name = first + last;
4System.out.println(name);
5System.out.println(name.length());
6name = first + " " + last;
7System.out.println(name);
8System.out.println(name.length());

Where this goes in a program

public class Main
{
    public static void main(String[] args)
    {
        ← the lines above go here
    }
}

The statements run inside main.

line    first    last    name         Output
1       Zion
2       Zion     Hill
3       Zion     Hill    ZionHill
4       Zion     Hill    ZionHill     ZionHill
5       Zion     Hill    ZionHill     8
6       Zion     Hill    Zion Hill
7       Zion     Hill    Zion Hill    Zion Hill
8       Zion     Hill    Zion Hill    9
ZionHill
8
Zion Hill
9

Many readers expect line 4 to print Zion Hill. But line 3 joins the two names with nothing between them, so name holds ZionHill, 8 characters. Line 6 joins " " between the names, so name holds Zion Hill. The space is a character too, so line 8 prints 9.

Calling a Method

A Java program starts in main. A method's lines run only when a line calls the method by its name. When a call runs, control jumps to the first line inside the method, the method's lines run in order, and then control comes back to main at the line right after the call. A void method sends no value back; it does its job and returns.

How to find the order the lines run:

  1. Start at the first line inside main.
  2. At a call, jump to the first line inside the method, and run its lines in order.
  3. At the method's closing brace, go back to the line right after the call.

Example 1. Follow one call. Number the lines the way the bench does: every line counts, blank lines and braces too.

1public class Main
2{
3    public static void cheer()
4    {
5        System.out.println("Who Dat");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("Kickoff");
11        cheer();
12        System.out.println("Final");
13    }
14}
Show the solution
Kickoff
Who Dat
Final

The lines run in the order 10, 11, 5, 12. Line 10 prints Kickoff. Line 11 calls cheer, so control jumps to line 5, which prints Who Dat. cheer has no more lines, so control comes back to line 12, the line after the call.

Example 2. Call the same method twice. Each call runs the method again.

1public class Main
2{
3    public static void bell()
4    {
5        System.out.println("ring");
6    }
7
8    public static void main(String[] args)
9    {
10        bell();
11        System.out.println("pause");
12        bell();
13    }
14}
Show the solution
ring
pause
ring

The order is 10, 5, 11, 12, 5. Line 5 runs twice, once for each call, because each call is its own trip into bell and back.

Parameters and Arguments

A method can have parameters, boxes named in its header that receive values when it is called. The values written in the call are the arguments. The first argument goes into the first parameter, the second into the second, so the arguments must match the parameters in type and order.

Example 3. One parameter, two calls.

1public class Main
2{
3    public static void greet(String name)
4    {
5        System.out.println("Hello, " + name);
6    }
7
8    public static void main(String[] args)
9    {
10        greet("Ana");
11        greet("Luis");
12    }
13}
Show the solution
Hello, Ana
Hello, Luis

name is the parameter. The first call hands it the argument "Ana", and the second call hands it "Luis". The same line 5 prints a different name each time.

Example 4. Two parameters, in order.

1public class Main
2{
3    public static void showScore(String team, int pts)
4    {
5        System.out.println(team + ": " + pts);
6    }
7
8    public static void main(String[] args)
9    {
10        showScore("Saints", 24);
11        showScore("Rams", 17);
12    }
13}
Show the solution
Saints: 24
Rams: 17

"Saints" goes into team and 24 goes into pts, because they are written in that order. A call written as showScore(24, "Saints") does not compile: 24 is a number, and the first parameter, team, is a String box.

Example 5. A box as the argument. The call hands over the value in the box, or the value of an expression.

1public class Main
2{
3    public static void square(int n)
4    {
5        System.out.println(n * n);
6    }
7
8    public static void main(String[] args)
9    {
10        int x = 3;
11        square(x);
12        square(x + 1);
13    }
14}
Show the solution
9
16

Line 11 hands square the value 3, so n holds 3 and line 5 prints 9. Line 12 works out x + 1 first, so n holds 4 and line 5 prints 16.

A Method That Calls a Method

A method can call another method. Each call still comes back to the line right after it, so the program returns one step at a time, the same way it went in.

Example 6. first calls second.

1public class Main
2{
3    public static void first()
4    {
5        System.out.println("one");
6        second();
7        System.out.println("three");
8    }
9
10    public static void second()
11    {
12        System.out.println("two");
13    }
14
15    public static void main(String[] args)
16    {
17        first();
18        System.out.println("four");
19    }
20}
Show the solution
one
two
three
four

The order is 17, 5, 6, 12, 7, 18. Line 17 calls first. Line 6 calls second, and line 12 prints two. second is done, so control comes back to line 7 inside first, and when first is done, control comes back to line 18 in main.

Example 7. A value passed along.

1public class Main
2{
3    public static void first(int n)
4    {
5        System.out.println(n);
6        second(n * 2);
7    }
8
9    public static void second(int m)
10    {
11        System.out.println(m);
12    }
13
14    public static void main(String[] args)
15    {
16        first(5);
17        System.out.println("end");
18    }
19}
Show the solution
5
10
end

first(5) puts 5 in n, and line 5 prints 5. Line 6 works out n * 2, which is 10, and hands it to second, so m holds 10 and line 11 prints 10. Control comes back through first to main, and line 17 prints end.

Checking a Student's Method

An AP reader scores a written method against a list of points, one point at a time. Checking someone else's method works the same way: read each point, then find the line that earns it or the line that misses it.

Example 8. The task asks for a void method named banner with one String parameter, word, that prints two dashes, a space and the word. A student wrote the method below and called it with banner("Saints").

1public class Main
2{
3    public static void banner(String word)
4    {
5        System.out.println("-- " + word);
6        System.out.println("word --");
7    }
8
9    public static void main(String[] args)
10    {
11        banner("Saints");
12    }
13}
Show the solution
-- Saints
word --

Point 1, a void method named banner with one String parameter: earned, on line 3. Point 2, prints two dashes, a space and the word: earned, on line 5, which prints -- Saints. Point 3, prints nothing else: not earned, because line 6 prints a second line, and "word" in quotation marks prints the letters w-o-r-d, not the value.

The bench catches three common mistakes, and each one changes the answer. The first is running a method's lines where the method is written instead of when it is called. The second is forgetting to come back to the line after the call. The third is mixing up the parameter, the box in the header, with the argument, the value written in the call.

Sources: Oracle, The Java Tutorials: Returning a Value from a Method and Passing Information to a Method or a Constructor; course repository, Bench Basics B19 job data (2026), run with the course JDK.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. What is printed? The first line printed is first.

1String city = "New";
2String s = city + " Orleans";
3s = s + " Pelicans";
4System.out.println(s);
5System.out.println(s.length());

Try It 2. What is printed?

1public class Main
2{
3    public static void hello()
4    {
5        System.out.println("Hi");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.println("A");
11        hello();
12        System.out.println("B");
13    }
14}

Try It 3. main has three lines, and each one is tap();. tap has one line, a println that prints tap. How many times does that println run?

Try It 4. What does shout("Geaux"); print?

1public static void shout(String word)
2{
3    System.out.println(word + " " + word);
4}

Try It 5. add has the header add(int a, int b) and prints a + b. What do the calls add(2, 5); and add(10, 1); print?

Try It 6. In the header add(int a, int b) and the call add(2, 5), which are the arguments?

Try It 7. What is printed?

1public class Main
2{
3    public static void inner()
4    {
5        System.out.println("in");
6    }
7
8    public static void outer()
9    {
10        System.out.println("out");
11        inner();
12    }
13
14    public static void main(String[] args)
15    {
16        outer();
17        System.out.println("done");
18    }
19}

Try It 8. In the program above, inner finishes. Where does control go next?

Try It 9. The task: a void method named showAge with a String parameter name and an int parameter age that prints the name, the word is and the age, with spaces. A student wrote the method below. showAge("Ana", 15) prints Ana is 15. Which point does it miss?

1public static void showAge(String name, int age)
2{
3    System.out.println(name + " is " + age);
4}

Try It 10. In Reading 2's Example 8, why is point 3 not earned?