1

I don't understand what exactly the brackets [] do in this code Integer[] while parsing new Objects

 private Integer data[]=(Integer[]) new Object[10];//works fine

 private Integer data[]=(Integer) new Object[10];//error
1
  • Isn't that simple, you are trying to parse an Array to single object in latter one? Commented Jul 20, 2013 at 10:04

6 Answers 6

3

private Integer data[]=(Integer) new Object[10]; //I'm a C programmer :)!

Is identical to:

private Integer[] data=(Integer) new Object[10];

Now you can better see that data is an array of Integers.

data is of type Integer[], you can't cast new Object[10]; to Integer if the type of the variable in the left side is Integer[].

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

2 Comments

Thanks, I thought that (Integer) new Object[10]; ie (Integer) will corresponding on every new Object[10]
btw Upvote for //I'm a C programmer :)! even though I don't have enough reputation for that action :)
2

I'll read the code for you:

Line 1:

  • new Object[10] - create a new array for 10 references to Object instances. The references are all null when that has completed.
  • (Integer []) ... - tell the compiler that it should assume the array of Object references to actually be an array of Integer references, ie an array of references to instances of Integer.
  • private Integer data [] = ... - keep the array we just created in a private reference variable called data, and assume data references an array of Integer.

Line 2:

  • (Integer) - tell the compiler to make one Integer reference of the array of Object instances. That can't be done, because there is no way to turn an array into only one object.

To sum up:

The brackets in X [] indicate array of references to X.

And (Y) foo reads assume foo actually is an instance of type Y.

Comments

0

Array of objects has to be cast to Integer[] not to Integer

Comments

0

You are trying to create an instance of data array in the first case. But, in the 2nd case, you are trying to assign array to Integer whereas it is expecting it to be an array.

Comments

0

The type of the variable data is Integer[] (array of Integers). It would be clearer if you used the traditional style of declaring arrays:

private Integer[] data = ...

So, the first line casts the expression to the type of the variable (Integer[]), whereas the second line casts it to Integer, which is not the type of data, hence the compilation error.

Note that the second line, although it compiles, doesn't make much sense. You should simply use

private Integer[] data = new Integer[10];

Comments

0

It is casting the array of object to array of integer to allow the assignment.

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.