Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Returning arrays

Thread view: 
Joan C - 29 Sep 2006 20:36 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.