Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+1 vote

If an actual parameter is a reference type rather than a primitive type, then what difference does that make in its value after the method call?

asked in CSC202 by (122 points)

1 Answer

+1 vote

Since reference types store the reference to the object, then what is passed to the method is the reference to the object. It the object is changed in the method, then it will be changed in the method that did the calling.

For example, there is an ArrayList of Integers in the main method that is passed as a parameter to a method that changes the element at index 0 to 1000. If the ArrayList is printed in the main method after the method call, it will have a first element of 1000.

For primitive types, that is not the case. If the main method has an int variable x storing 5 and x is passed to a method. If x is changed to 7 in the method and then x is printed in the main method after the method call, the x in the main method will still be 5.

Objects "pass by reference" and primitive types "pass by value". This is the case for all objects and all primitive types.

answered by (4.3k points)
...