Parameters are values that are being sent as input to a method. The variable that represent the parameter can be used to alter the value of it if it’s not declared as final.
A parameter can be sent by value or by reference. Any primitive datatype (like int, long, double) is sent by value, whereas any object (like Array, List, Set) is sent by reference.

A primitive type parameter value that is altered in the method only has it’s new value in that particular method, but a reference type parameter that is altered in the method will also have the new value outside of that method.

This first example shows a primitive type being sent as parameter and altered in the method:

The output from the above code is…

…since a primitive parameter is sent by value and is only used in that method.
A parameter (primitive or not) declared as final can’t be altered and the IDE or compiler will show an error:

The last example shows what happens when we send in a parameter by reference, in this case an array of int values (the array is only one position in size so it’s only one int value).
Since the parameter is an object, it’s altered value will also be known outside of the method ‘someMethod’.

The output from the code above is:

Search for more Java info on this site here:
Custom Search

Please type any questions here.