Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java is always pass-by-value

MGSHOct 21 2010 — edited Oct 22 2010
i am passing an array b[] to editArray() which should return an edited array
    byte[] b = {1, 2, 3, 4};
    byte[] b2 = editArray(b);
    for (byte t: b)  System.out.print(t + ", "); System.out.println();		
    for (byte t: b2) System.out.print(t + ", ");
editArray();
public static byte[] editArray(byte[] bArray){
    for(int i=0; i<bArray.length; i++) bArray[i] = 0;
    return bArray;
    }
since java is passing by values, i was expecting b[] to have no changes.
here is the output
0, 0, 0, 0, 
0, 0, 0, 0,
* please can any one explain this for me, in some blogs the says : "Java passes objects as references passed by value". i cant get it.*
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 19 2010
Added on Oct 21 2010
9 comments
1,168 views