Concepts / Assignment and Type Conversion in Java

Assignment and Type Conversion in Java

A formal parameter is a named slot in a subroutine definition, with a simple identifier as its name and a declared type.

  • Programming

A Call That Seems to Mismatch

Suppose you write a subroutine that expects a decimal number, and later you call it with a whole number. Does the call work, or does Java reject it? The answer depends on a rule that governs every subroutine call. This article builds that rule from the ground up, so keep your first guess in mind, because the vocabulary you are about to learn is what makes the answer predictable.

What do you think happens?

A method is defined to accept a double. You call it with an int variable K that holds 7. Does Java accept the call?

  • Yes, the call is accepted

  • No, Java rejects the call

Reveal answer
Answer:

Yes, the call is accepted. An int can be passed where a double is expected.

The rule that decides this is assignability: a value is acceptable when it could legally be assigned to the formal parameter with an assignment statement. An int can be assigned to a double, so the call is legal.

Formal Parameters: The Slots in a Definition

The word parameter gets used for two related things that are easy to confuse. One is the list of names written inside a subroutine's header, which describes what information the subroutine needs. The other is the list of values written inside a call, which supplies that information. The first role belongs to the definition. The second belongs to the place where the subroutine is used. The two kinds of parameter always come in pairs, even though they live in different parts of a program.

Definition

A formal parameter is a named slot in a subroutine definition, with a simple identifier as its name and a declared type.

Think of a formal parameter as a labeled slot that the subroutine reserves for incoming information. Because the slot is a stand-in rather than a real piece of data, it has to be written as a name, specifically a simple identifier such as amount or startingValue. An expression such as total + 2 is not allowed in this position, because the definition is describing a slot and not computing a result.

Each formal parameter also carries a declared type, such as int, boolean, String, or double[]. This is what makes a formal parameter very much like an ordinary variable: it has a name, a type, and a place inside the subroutine where its value can be used. The difference is that its value is not chosen inside the subroutine. It arrives from whoever calls the subroutine.

voidmethod returns no valueprintAmountsubroutine namedoubledeclared type of the slotamountformal parameter name, asimple identifiertype of
Which name is in the definition, and what does each part of the header contribute to the slot?

Actual Parameters: The Values in a Call

An actual parameter is the value you hand to the subroutine at the moment you call it. Because it is a value, it can be written as any expression that produces a value of the right type. A plain variable, a literal, or a calculation all qualify. The source's own example is the variable K in the statement print3NSequence(K);, where K is the actual parameter for the formal parameter in that subroutine's definition.

java
Output
Amount: 7.0
Notes on the code
  • amount is the formal parameter: a named slot with declared type double
  • K is the actual parameter: a variable holding an int value
  • The call printAmount(K) supplies one actual parameter for one formal parameter

The one requirement is that the value's type must be one that could legally be assigned to the formal parameter with an assignment statement. The call does not need the types to match exactly. It needs the actual parameter to be assignable. A call must also supply exactly one actual parameter for each formal parameter in the definition, in the same order.

Tracing a Call from Start to Finish

Parameter passing is the process that connects the two roles, and it has a fixed sequence. That sequence is what makes the behavior predictable. The diagram below shows the flow for a single call.

Call statementprintAmount(K)Actual parametersexpressions reduced tovaluesFormal parametersnamed slots in thedefinitionSubroutine bodyruns after assignment1. evaluate2. assign values3. execute body
What happens first, second, and third when a subroutine is called, and where does each value go?
  1. 1
    Evaluate the actual parameters in the call statement, so any expressions are reduced to concrete values.
  2. 2
    Assign each of those values to the matching formal parameter in the definition, in order.
  3. 3
    Only after that assignment is complete, begin executing the body of the subroutine.
Worked example

Tracing printAmount(K) with K equal to 7

The definition is static void printAmount(double amount), and main calls printAmount(K) where int K = 7. Trace what happens during the call.

  1. 1
    Evaluate the actual parameter

    The actual parameter is the variable K. Its current value is 7, so the call has a concrete value ready to pass.

  2. 2
    Assign to the formal parameter

    The value 7 is assigned to the formal parameter amount, which is declared as double. Because an int can be assigned to a double, the assignment is legal and amount holds the value 7.0.

  3. 3
    Execute the body

    Only now does the body run. It prints the text Amount: followed by the value of amount, which is 7.0.

Result

The output is Amount: 7.0. The call was accepted because the actual parameter was assignable to the formal parameter.

The Assignability Test

Since the actual parameter must be assignable to the formal parameter, you can test compatibility by imagining the ordinary assignment statement. Ask whether the formal parameter could receive the actual value on its own line of code. If that assignment would be legal, the call is legal too.

java
Notes on the code
  • The first assignment mirrors a call that passes an int where a double is expected, so that call is legal
  • The second assignment mirrors a call that passes a String where an int is expected, so that call is rejected

The source's illustration runs in this direction: if the formal parameter is a double, an int may be passed, because an int can be assigned to a double. Notice that the test is about the assignment, not about whether the two types are spelled the same way. The call does not need an exact match. It needs the value to be assignable.

Mistakes Beginners Make with Parameters

  • Common mistake

    Writing an expression in the definition instead of a simple name

    What it looks like
    static void show(total + 2) { ... }
    Why it fails

    The definition describes a slot that will receive a value. An expression is not a slot, so it cannot appear where a formal parameter name belongs.

    Do this instead

    Write a simple identifier with a declared type, such as static void show(double total) { ... }.

  • Common mistake

    Confusing the formal parameter with the variable in the caller

    What it looks like

    Assuming that changing amount inside printAmount also changes K in main

    Why it fails

    The formal parameter is a slot that is filled by assignment during the call. The caller supplies a value; it does not hand over the variable itself.

    Do this instead

    Think of amount as a separate slot that receives the value of K at the moment of the call.

  • Common mistake

    Assuming the types must match exactly

    What it looks like

    Believing a call with an int argument fails when the formal parameter is a double

    Why it fails

    The actual parameter only needs to be assignable to the formal parameter. An int can be assigned to a double, so the call works.

    Do this instead

    Apply the assignment test: could the formal parameter legally receive this value on its own line of code?

  • Common mistake

    Forgetting that the order of parameters matters

    What it looks like

    Passing arguments in a different order than the definition lists the formal parameters

    Why it fails

    Each actual parameter is matched to a formal parameter by position, and the call must supply exactly one for each.

    Do this instead

    Line up the actual parameters with the formal parameters in the same order, one for one.

Practice Deciding Whether a Call Fits

EASY

A method is declared as static void showTotal(double total). For each call below, decide whether the actual parameter is acceptable for the formal parameter total, using the assignment rule. Call one: showTotal(5). Call two: showTotal(K) where K is an int variable holding 12. Call three: showTotal("twelve"). Explain your answer for each call.

Hints
  • Imagine the statement total = 5; and ask whether it would be legal.
  • An int value can be assigned to a double, so the assignment test is passed.
  • A String value cannot be assigned to a double, so that call is not acceptable.

Key Takeaways

  • A formal parameter is a named slot in a subroutine definition, written as a simple identifier with a declared type, while an actual parameter is the value supplied in a call and can be any expression that produces a suitable value.
  • A call first evaluates the actual parameters, then assigns each value to its matching formal parameter, and only then executes the body of the subroutine.
  • An actual parameter is acceptable when it could legally be assigned to the formal parameter with an assignment statement, so an int may be passed where a double is expected.
  • To test a call, imagine the ordinary assignment formal = actual; and check whether Java would allow it.