0

I'm looking for the best way to do in my application, and now I'm wondering:

I received a byte array (byte[]) by reading an InputStream. Then I have to do some treatment, so I pass this array as a parameter of a method. Coming from C++ language, and as I have to do this treatment as fast as possible, and many time, I am wondering if passing a byte array by value might not cause problems.

Is it worth creating a ByteBuffer object to pass a reference to my method instead of a value?

Thank you for your help and advices.

1
  • don't worry, you are not passing it by value. Commented Feb 1, 2013 at 13:39

3 Answers 3

1

In Java arrays are objects, so you are actually passing a copy of the reference variable to this object.

IIRC in C++ passing an array is the equivalent of passing a pointer to the first element.

Sign up to request clarification or add additional context in comments.

Comments

1

Passing a byte[] reference is just like passing an ordinal object reference. There will be no copying of the array. It is not worth to wrap around with a ByteBuffer.

Comments

0

Generaly, working with primitive types is faster as it requires less memory to store the data.

I would therefore suggest to keep the byte array solution. This, of course depends what "treatment" you intend to do with it :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.