1

For the following line of code:

int x [ ] = new int [5]

Why is the second int necessary? Is the second type-declaration not always the same as the first?

3 Answers 3

3

On this specific case it may seem verbose and redundant, but since we're dealing with OO language, it's very likely (and useful) to run into declarations like:

Shape shape = new Triangle(); //where Triangle implements Shape interface

Same reason goes here, an Array could be of general type and contain items of different types, or, different type of arrays (that extend the same type). For example:

Number[] tmpNumbersArray = new Integer[10]; // 
Sign up to request clarification or add additional context in comments.

Comments

1

No, of cause.

Number [] numbers = new Float[5];

Comments

0

You can try this, if you have the values at compile time:

int[] x = {34, 45, 78};

Also look at this:

Declaration with data type

int[] a,b = new int[3]; //multiple arrays declared

Declaration with variable

int a[], b;//multiple declaration, but not all are arrays
a = new int[3];//later you can define the array size

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.