array1
isn't actually an array: it's a reference to an array. It's a bit like if you give someone a business card with your office address, that card isn't the actual office, but rather something that tells you how to reach the office.
When you do int[] unSorted = array1
, you're making a copy of the reference, not the array. Again, it's like someone copying down the info on your business card.
And when you perform an operation on unSorted
(like moving its elements to sort then), what you're actually doing is performing an operation on the object it points to. It's like saying, "go to the office specified on the business card called unSorted
, and move the chair at desk 1 to desk 2."
If you then went to the office specified on the business card called array1
, you would of course expect that the chair has moved there, since it's the same office.
If you want a copy, you have to create one. There are a few ways to do that, but the easiest is to use Arrays.copyOf
.