Hi,
I have written a method which takes a byte array in as an argument, does
some manipulation on that byte array, putting them into a new array and then
returning the byte array.
e.g.
public byte[] changeArray(byte[] mydata)
{
byte[] returnBytes = new byte[mydata.length];
// do some stuff etc.
return returnBytes;
}
That works fine !! My question is: the java.io.DataInputStream has the
method: int read(byte[] b) and yet the method puts the bytes into the array
b without actually returning that array. I've tried implementing it like
this for my above example but when I call my method the array byte b[] is
actually only a copy of the original byte array so is therefore only local
for that method - therefore the original byte array is not changed. So, how
on earth does the java.io.DataInputstream read(byte b[]) method manage to
change the byte array like this when I have to actually return the byte
array?
Knute Johnson - 29 Sep 2006 23:34 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> change the byte array like this when I have to actually return the byte
> array?
What gets passed in the method call is the reference to the byte[] not a
byte[].
public class test8 {
public static void method1(byte[] b) {
for (int i=0; i<b.length; i++)
b[i] += 1;
}
public static void main(String[] args) {
byte[] c = new byte[10];
for (int i=0; i<c.length; i++)
System.out.print(c[i]+" ");
System.out.println();
method1(c);
for (int i=0; i<c.length; i++)
System.out.print(c[i]+" ");
}
}

Signature
Knute Johnson
email s/nospam/knute/