Search⌘ K

Passing Arguments

Explore how Java handles passing arguments to methods, including the differences between primitive types and class types. Understand call by value and how object references affect method behavior and data mutation.

Parameters of a primitive type

When a formal parameter has a primitive type such as int, it is initialized to the value of the corresponding argument in the call to the method. This argument can be a literal constant—such as 51—or it can be a variable or any expression. This way of passing an argument to a method is known as a call by value. A method cannot change the value of an argument that has a primitive data type. Such an argument serves as an input value only.

📝 Note

The arguments in a call to a method must match the formal parameters of the method’s definition with respect ...