How does Java handle ArrayList refrerences and assignments?
        Posted  
        
            by Jonathan
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jonathan
        
        
        
        Published on 2010-05-06T01:57:18Z
        Indexed on 
            2010/05/06
            2:08 UTC
        
        
        Read the original article
        Hit count: 481
        
Hey all-
I mostly write in C but am using Java for this project. I want to know what Java is doing here under the hood.
ArrayList<Integer> prevRow, currRow;
currRow = new ArrayList<Integer>();
for(i =0; i < numRows; i++){
    prevRow = currRow;
    currRow.clear();
    currRow.addAll(aBunchOfItems);
}
Is the prevRow = currRow line copying the list or does prevRow now point to the same list as currRow? If prevRow points to the same list as currRow, I should create a new ArrayList instead of clearing....
private ArrayList<Integer> someFunction(ArrayList<Integer> l){
    Collections.sort(l);
    return l;
}
main(){
    ArrayList<Integer> list = new ArrayList<Integer>(Integer(3), Integer(2), Integer(1));
    list = someFunction(list);  //Option 1
    someFunction(list);  //Option 2
}
In a similar question, do Option 1 and Option 2 do the same thing in the above code?
Thanks-
Jonathan
© Stack Overflow or respective owner