What happens when you change a reference inside a method?
What happens when you change a reference inside a method?
public void reverseX(int[] nums) {
int[] nums2 = new int[nums.length] ;
for( int i=0 ; i < nums.length ; i++ )
nums2[i] = nums[nums.length-(i+1)] ;
nums = nums2 ;
};
Objects, reference types are suppose to be immutable. What happens here
when nums is set to nums2.
This compiles fine.
Code is from here, and shown as an example of what not to do.
http://www.cs.nyu.edu/~cconway/teaching/cs1007/notes/arrays.pdf
No comments:
Post a Comment