November 15, 2026•7 min read

Java Methods: Why applyDiscount Never Changed the Receipt Total

A receipt prints subtotal: 25000 on both lines and exits 0. applyDiscount assigns to its parameter, which is a copy, so the caller's variable never moves and the total stays 25000 instead of 22500.

Read in Indonesian

Your receipt prints the subtotal twice, the discount never shows up, and the program exits 0 with no error. The method modified its local copy of the parameter, leaving the caller's variable unchanged.

Key takeaways

  • Java passes every argument by value, so a method that assigns to its parameter changes a copy and leaves the caller's variable alone.
  • A void method returns nothing, so a value computed inside it is gone unless the method prints or stores it before it ends.
  • The fixed version returns int and the caller assigns the result, which is what puts total: 22500 on the receipt.
  • An array argument is a copy of a reference, so prices[i] = ... changes the caller's elements while prices = new int[] {0, 0} moves only the copy.
  • A double return type does not rescue integer division: sum / count on two ints drops the fraction before the widening.
  • Overloads are chosen at compile time from the declared argument types, so char and short both widen to int and pick the int overload.

The method that subtracts nothing

Here is the broken program. applyDiscount takes a price, subtracts 2500 from it, and returns nothing. main calls it with a subtotal of 25000, then prints that subtotal under two labels. Every run in this article used Temurin JDK 21.0.13 on Linux.

JAVA
// Receipt.java
public class Receipt {
    static void applyDiscount(int total) {
        total = total - 2500;
    }

    public static void main(String[] args) {
        int subtotal = 25000;
        applyDiscount(subtotal);
        System.out.println("subtotal: " + subtotal);
        System.out.println("total: " + subtotal);
    }
}
Output
$ javac Receipt.java && java Receipt
subtotal: 25000
total: 25000

Both lines say 25000. The discount is 2500, so the total should be 22500. The compiler accepts the code and the program exits 0, so nothing flags the mistake.

Why the caller's price never changes

Read the call applyDiscount(subtotal) one clause at a time.

  1. Java reads the variable subtotal and gets the value 25000.
  2. It copies that value into the parameter total, a new variable that belongs to the method.
  3. The line total = total - 2500 stores 22500 in that new variable.
  4. The method is void, so it returns nothing. The 22500 is neither printed nor assigned inside the method, so it is gone when the method ends.
  5. Back in main, subtotal was never touched. It still holds 25000.

This is pass-by-value. Java passes every argument by value, so the parameter is a copy of the caller's variable. The Oracle tutorial on passing arguments covers it. The language specification defines method declarations in section 8.4.1.

A check you can run

Reading two lines by eye works. A checker works for any number of lines. CheckReceipt reads standard input and compares each line to the text it expects. It prints PASS or FAIL for each line and exits with status 1 if any check fails.

JAVA
// CheckReceipt.java
import java.util.List;
import java.util.Scanner;

public class CheckReceipt {
    public static void main(String[] args) {
        List<String> expected = List.of("subtotal: 25000", "total: 22500");
        Scanner in = new Scanner(System.in);
        int passed = 0;
        for (int i = 0; i < expected.size(); i++) {
            String got = in.hasNextLine() ? in.nextLine() : "<nothing>";
            if (got.equals(expected.get(i))) {
                passed++;
                System.out.println("PASS  line " + (i + 1) + ": " + got);
            } else {
                System.out.println("FAIL  line " + (i + 1) + ": expected " + expected.get(i) + ", got " + got);
            }
        }
        if (in.hasNextLine()) {
            System.out.println("FAIL  extra line: " + in.nextLine());
        }
        System.out.println();
        System.out.println(passed + " of " + expected.size() + " checks passed");
        if (passed != expected.size()) {
            System.exit(1);
        }
    }
}

The | sends the output of Receipt into the input of CheckReceipt.

Output
$ javac CheckReceipt.java && java Receipt | java CheckReceipt
PASS  line 1: subtotal: 25000
FAIL  line 2: expected total: 22500, got total: 25000

1 of 2 checks passed

Line 1 passes. Line 2 fails with the same wrong number you saw by eye, and the checker exits with status 1. The red run gives you a target: make line 2 say 22500.

Return the number and assign it

Hand the answer back. The return type becomes int, the body becomes return total - 2500;, and main assigns the result to a new variable named total.

JAVA
// Receipt.java
public class Receipt {
    static int applyDiscount(int total) {
        return total - 2500;
    }

    public static void main(String[] args) {
        int subtotal = 25000;
        int total = applyDiscount(subtotal);
        System.out.println("subtotal: " + subtotal);
        System.out.println("total: " + total);
    }
}
Output
$ javac Receipt.java && java Receipt
subtotal: 25000
total: 22500
Output
$ java Receipt | java CheckReceipt
PASS  line 1: subtotal: 25000
PASS  line 2: total: 22500

2 of 2 checks passed

The checker is green and exits with status 0. subtotal still holds 25000 because applyDiscount never modified it, and main now assigns the returned value to total.

A returned number can still be wrong in a quieter way. Average.java divides 47000 by 3 and prints 15666.0, not 15666.666666666666.

JAVA
// Average.java
public class Average {
    static double average(int sum, int count) {
        return sum / count;
    }

    public static void main(String[] args) {
        System.out.println("average: " + average(47000, 3));
    }
}
Output
$ javac Average.java && java Average
average: 15666.0

Both sum and count are int, so sum / count is integer division. The fraction is dropped first. The double return type then widens the already-truncated 15666.

The array that does change

Arrays look like a contradiction. applyDiscount below changes the caller's data, and reset does not.

JAVA
// Cart.java
public class Cart {
    static void applyDiscount(int[] prices) {
        for (int i = 0; i < prices.length; i++) {
            prices[i] = prices[i] - 2500;
        }
    }

    static void reset(int[] prices) {
        prices = new int[] {0, 0};
    }

    public static void main(String[] args) {
        int[] prices = {25000, 40000};
        applyDiscount(prices);
        System.out.println("after applyDiscount: " + prices[0] + " " + prices[1]);
        reset(prices);
        System.out.println("after reset: " + prices[0] + " " + prices[1]);
    }
}
Output
$ javac Cart.java && java Cart
after applyDiscount: 22500 37500
after reset: 22500 37500

The first line, 22500 37500, is not a counterexample. An array argument is also a copy, but the copy is a reference to the same array. Writing prices[i] follows that reference and changes the caller's elements.

The second line shows the copy is still a copy. In reset, the assignment gives the parameter a new array and moves only the copy. The caller's prices still points at the old array, so it prints 22500 37500 again.

Two methods with one name

A class can hold two methods with the same name if their parameter types differ. The compiler chooses between them from the declared types of the arguments, before the program runs.

JAVA
// Overload.java
public class Overload {
    static void label(int total) {
        System.out.println("int overload");
    }

    static void label(double total) {
        System.out.println("double overload");
    }

    public static void main(String[] args) {
        label(25000);
        label(25000.0);
        label('A');
        short s = 250;
        label(s);
    }
}
Output
$ javac Overload.java && java Overload
int overload
double overload
int overload
int overload

The literal 25000 is an int, and 25000.0 is a double. A char and a short both widen to int, so 'A' and the short holding 250 pick the int overload. The language specification describes the full rules in section 15.12.2.

Before the next article

In the fixed Receipt.java, change int subtotal = 25000; to int subtotal = 40000;. Recompile it, run it, and pipe it into the same checker. The receipt prints subtotal: 40000 and total: 37500. The checker reports 0 of 2 checks passed and exits with status 1, because its two expected lines are fixed at 25000 and 22500.

Both receipt lines are correct for a 40000 subtotal; the checker fails only because its expected output is hardcoded to 25000 and 22500.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
Struktur DataWhat a Linked List Actually Is

A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.

PascalPascal Capstone: Why Every Gradebook Score Came Back Zero

A gradebook reads three students from a file, prints three rows, and every score is 0. The procedure filled a copy of the array, because the parameter was missing var. Add the word and the report matches the file; the compiler had already warned about the call that filled nothing.

For developers who learn by struggling

© 2026 Inva.dev.