0

I'm unsure of the significance of the number when initializing a byte array: e.g. byte[] t = new byte[2].

I've seen online that arrays are a fixed size so no matter what, the length of t should be 2 right?

Well then how come in this example:

byte[] t = new byte[0];
t = "hello".getBytes(StandardCharsets.ISO_8859_1);
System.out.println(t.length);

the output is 5? It seems like t is just getting filled with all the elements without caring about size.

I need something like this, where I append values to a byte array repeatedly, without knowing how big the array will end up, can I just keep adding to something like t here?

11
  • Are you aware of overwriting t? Commented Dec 1, 2020 at 15:36
  • 1
    Because after reassigning, t no longer refers to your array. Instead, it now refers to the array returned by getBytes. Commented Dec 1, 2020 at 15:36
  • LOL...he did and wonders about the odd outcome Commented Dec 1, 2020 at 15:36
  • Note that in Java, all variables of non-primitive types (and that includes arrays, even if the element type is primitive) are references (which are similar to pointers in C or C++, except that you cannot to anything except reassign them or pass them to methods). Commented Dec 1, 2020 at 15:39
  • @Hulk So if I were to use System.arraycopy to continuously append bytes to the end of t, how should I initialize t? Commented Dec 1, 2020 at 15:43

5 Answers 5

1

Arrays in Java have a fixed size, and cannot be appended.
For this typically you use an ArrayList, which is a wrapper around an array that handles dynamic resizing.

For bytes however, this is not ideal due to autoboxing, it costs extra memory and performance to store primitive bytes in a list.

In comes ByteArrayOutputStream, also a wrapper around an array that handles resizing, but this time specifically for bytes. Use it as follows:

ByteArrayOutputStream stream = new ByteArrayOutputStream();

byte[] hello = "hello ".getBytes(StandardCharsets.ISO_8859_1);
stream.write(hello, 0, hello.length);
byte[] world = "world".getBytes(StandardCharsets.ISO_8859_1);
stream.write(world, 0, world.length);

byte[] combined = stream.toByteArray();
System.out.println(combined.length); // prints 11

Note: This deals with appending bytes because the question did. In this example it might be easier to combine the Strings first and then get the bytes in one go.
Strings can be appended using a StringBuilder or with the + and += operators.

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

Comments

0

In that example the t is uselessly initialized with a zero-length array, which is thrown away when t is assigned the return value of "hello".getBytes(StandardCharsets.ISO_8859_1);.

It should really be written without the throw-away value as

byte[] t = "hello".getBytes(StandardCharsets.ISO_8859_1);
System.out.println(t.length);

5 Comments

Ok, and System.arraycopy doesn't care about how long t is when I continuously add bytes to t?
@jklo12334 I didn't say anything like that. You read the javadoc for System.arrayCopy and you follow those instructions. This isn't magic, everything is fully documented so you don't need to guess things or ask me how things work. It most certainly does care how large the arrays are, as they are fixed size and you can't write outside of an array.
@jklo12334 now instead of asking about arrayCopy, ask about your actual problem. That way you can get advice to solve it, instead of us having to tell you that you're wrong in your guesses, and your actual problem remains unsolved.
@jklo12334 it's a QA site, not discussion. Surely you know how to initialize an array, you did it in your question.
Oh lol ok let me rephrase, sorry for asking a question on a Q and A site..? What was the point of that correction..?? And I'm not asking how to initialize an array, can you quote the part of my question where I ask that?
0

if you try to print the length of t before changing it you get 0 and after overwriting the t variable data the new length is 5

byte[] t = new byte[0];
System.out.println(t.length); // result = 0
t = "hello".getBytes(StandardCharsets.ISO_8859_1);
System.out.println(t.length); // result = 5

Comments

0

In your second line, you didn't replace the contents of the array, you replaced the array itself, meaning that t now points to an array of length 5 made out of the "hello" String.

Comments

0

Main reason of this when you use

t = "hello".getBytes(StandardCharsets.ISO_8859_1);

you created absolutely new array with new length, so this code is similar

String t = "hi";
t = "hello";
System.out.println(t.length); // 5

Comments

Your Answer

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