OK, here's what I want to do (I realize the code below doesn't work, but it's what I would LIKE to do). I've converting things from C++

public void ChangePrimitiveArray(byte bytes[])
{
    bytes[0] = 1;
}


public void foo()
{
    byte bytes[] = new byte[2];
    bytes[0] = 4;
    ChangePrimitiveArray(bytes);

    System.out.printf("bytes[0]=0x%02x\n", bytes[0]);
}

This displays 0x04, not 0x01. It needs to display 0x01. Any ideas? The real function is more involved than this, but this shows the problem. Do I need to convert things from an array to an ArrayList or something or can I stick with plain old arrays? And is it because byte is a primitive type?

Recommended Answers

All 5 Replies

Really? I ran this and I got 0x01. Here's the code (Eclipse Helios on Windows 7):

public class ArrayRefTest 
{
	public ArrayRefTest()
	{
		//Default Constructor
	}
	
	public static void main(String[] args)
	{
		ArrayRefTest a = new ArrayRefTest();
		a.foo();
	}
	
	public void ChangePrimitiveArray(byte bytes[])
	{
	    bytes[0] = 1;
	}

	public void foo()
	{
	    byte bytes[] = new byte[2];
	    bytes[0] = 4;
	    ChangePrimitiveArray(bytes);

	    System.out.printf("bytes[0]=0x%02x\n", bytes[0]);
	}
}

Check this out:

http://www.javacoffeebreak.com/faq/faq0066.html

commented: Thanks. +13

By God you're right! OK, I guess I need to go back and figure out what I did. Obviously, when "simplifying" things in my real code, I changed something important and the example no longer reflects the actual code.

Thanks.

All right, this is more like it. I need to define the size of the array in the function being called, not the calling function.

public class ArrayRefTest 
{
    public ArrayRefTest()
    {
    	//Default Constructor
    }
	
    public static void main(String[] args)
    {
        ArrayRefTest a = new ArrayRefTest();
        a.foo();
    }
	
    public void ChangePrimitiveArray(byte bytes[])
    {
        // The size of the array is unknown by the calling function, so define
    	// the size here.
        bytes = new byte[2];
        bytes[0] = 1;
        bytes[1] = 2;
    }

    public void foo()
    {
        byte bytes[] = null;
        ChangePrimitiveArray(bytes);

        System.out.printf("bytes[0]=0x%02x%02x\n", bytes[0], bytes[1]);
    }
}

bytes[] is null in line 28, so I'm getting a null pointer error, so the array I'm creating in line 18 must be a local array. How can I make this work?

Yup local variable is getting created. You're not actually modifying anything - java everything is passed by value, a copy is made. So a copy of whatever reference value bytes[] holds is getting made, in this case null and assigned to the local variable bytes in your function.

You have to return return the newly created array ref. and assign it.

byte bytes[] = ChangePrimitiveArray( byte bytes[] );

Note: it's pointless in this case passing anything to the ChangePrimitiveArray method. Your method is working like a "Factory" method.

commented: Thanks. +13
byte bytes[] = ChangePrimitiveArray( byte bytes[] );

O.K. I changed things to this prototype and it works. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.