0

If we do int[] b = {2, 4}; then we have an array called b, with length 2. From what I understand, the java compiler is implicity doing int[] b = new int[] {2, 4}; for me, fine.

Similarly, if we do int[] c = new int[2]; , then we get an array called c initialzed to {0, 0} My confusion comes from why the following doesn't work:

Why can't I do int[] d = new int[2] {5, 6};

1 Answer 1

3

It's just a compiler thing. Besides, why would you want to manually input the size anyway? This will just be a source of error.

The int[] b = new int[] {2, 4} notation is for convenience, if you already know the contents of the array that you want to declare.

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

1 Comment

+1 - Maybe to partially initialize an array while still giving it a larger size would be my only thought on why it might be helpful if it were actually possible.

Your Answer

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