1
int[] a = new int[]{1,2,3};

int[] b = {1,2,3};

What difference between a and b? Am I right that first is object and 'a' is a link, and second is a primitive type and 'b' is a variable? But what advantages/disadvantages have first array?

1
  • 1
    I think that the first can be done anywhere, but the second can only be done when initializing a variable. That is: int[] b; b = {1, 2, 3}; is illegal, but int b; b = new int[]{1, 2, 3}; is not. Commented Mar 4, 2012 at 15:53

1 Answer 1

5

In an initialization like you have there, there is no difference between them at all. They result in the same bytecode. Note that you have to use the first form in an assignment, though:

int[] b;
b = {1,2,3}; // <== Syntax error

Am I right that first is object and 'a' is a link, and second is a primitive type and 'b' is a variable?

No, in both cases, you have a variable (a, b) which is a reference to the array.

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

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.