Method Parameters and Arguments

Methods can take parameters (inputs) that allow you to pass data into them. The data passed to the method is called arguments.

  • Passing Parameters by Value (Primitives)
  • Passing Reference Types (Objects)
Passing Parameters by Value (Primitives)

In Java, primitive data types (like int, float, char, etc.) are passed to methods by value, meaning that a copy of the value is passed to the method. Changes made inside the method do not affect the original variable.

Output
Original value: 5
Passing Reference Types (Objects)

For reference types (like arrays, objects), the reference (address) is passed to the method. This means changes to the object inside the method affect the original object.

Output
First element: 100