-2
int[] outSize = new int[]{bytes, packets};

new byte[] {0, 0, 0, 0}

What does the first line of code mean? What is it doing to size array? How is the second line of code initializing that byte array (if it is at all)?

8
  • 1
    what do you want to know exactly Commented Jun 17, 2018 at 5:05
  • int [ ] outsize = new int[10].. i know what that does, but what is { } doing here?? Commented Jun 17, 2018 at 5:05
  • the code is not correct. and will give compilation error Commented Jun 17, 2018 at 5:08
  • well i can compile it with maven ... Commented Jun 17, 2018 at 5:09
  • then bytes and packets are integers and they are initialized in outSize array Commented Jun 17, 2018 at 5:11

2 Answers 2

0

We can initialized an array at the time of declaration using the given syntex

int[] outSize = new int[]{5, 9}; //create a Integer array of 2 element named outSize

OR

 int bytes = 5, packets =9;
 int[] outSize = new int[]{bytes, packets}; //create a Integer array of 2 element named outSize.

there is no difference using both the above statement.

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

Comments

0

First line

int[] outSize = new int{ bytes, packets };

This creates an integer (primitive) array with two integer elements, namely bytes and packets.

Second line

new byte[] { 0, 0, 0, 0 }

By itself, this will give you an error (not compile) since it isn't a statement (there's no assignment to a variable anywhere).

If you write

byte[] bytes = new byte[] { 0, 0, 0, 0 };

you are declaring a byte array called bytes with four elements.

Use of {} for array initialization

Refer to chapter 10 (arrays) of the documentation, particularly sections 10.1 and 10.2.

Also worth reading, the primitive data types.

From the above:

int is a 32-bit signed two's complement integer (min value in base 10 is -2^31 and max is 2^31 - 1).

byte is an 8-bit signed two's complement integer (min value in base 10 is -2^7 and max is -2^7 - 1).

What it's doing

The first line uses 64 bits to store two integers.

The second line uses 32 bits to store four bytes.

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.