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
voidmethod returns nothing, so a value computed inside it is gone unless the method prints or stores it before it ends. - The fixed version returns
intand the caller assigns the result, which is what putstotal: 22500on the receipt. - An array argument is a copy of a reference, so
prices[i] = ...changes the caller's elements whileprices = new int[] {0, 0}moves only the copy. - A
doublereturn type does not rescue integer division:sum / counton twoints drops the fraction before the widening. - Overloads are chosen at compile time from the declared argument types, so
charandshortboth widen tointand pick theintoverload.
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.
// 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);
}
}
$ 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.
- Java reads the variable
subtotaland gets the value 25000. - It copies that value into the parameter
total, a new variable that belongs to the method. - The line
total = total - 2500stores 22500 in that new variable. - 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. - Back in
main,subtotalwas 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.
// 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.
$ 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.
// 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);
}
}
$ javac Receipt.java && java Receipt
subtotal: 25000
total: 22500
$ 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.
// 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));
}
}
$ 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.
// 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]);
}
}
$ 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.
// 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);
}
}
$ 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.