Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Sat Jan 11, 2014 12:55 pm
by javaman
If I do this:
Object[] iA = new Object[10];
iA[0] = new Integer(1);
System.out.println(iA[0]);
I create a variable that points to an array that can hold 10 objects, assign an Integer to the 1st position and print that value:
run:
1
BUILD SUCCESSFUL (total time: 0 seconds)
Isn't this what the question asks - Which of the following correctly declare a variable which can hold an array of 10 integers??
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Sat Jan 11, 2014 9:03 pm
by admin
It is "integers" with small i (not capital I), which means you are dealing with primitives not wrapper objects. You cannot store primitive integers in an array of Objects.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Wed Jul 29, 2015 3:57 pm
by mrmuiz
As the explanation says
but what about this?
it's valid and compiles without any error. This is actually an array of integers stored in an array of Objects...
It's a bit confusing to me, but probably it's accepted because (maybe)
is the same as
Code: Select all
Object []t=new Object[10];
t[0]=1;
t[1]=2;
t[2]=3;
where integers literals are atomatically wrapped into Integers objects before being added to the array. This would explain why
Code: Select all
Object []t={1,2,3};
System.out.println(t[0].getClass());
prints
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Fri Nov 18, 2016 2:43 am
by mj.anjuthan
mrmuiz wrote:As the explanation says
is not valid
but what about this?
it's valid and compiles without any error. This is actually an array of integers stored in an array of Objects...
In the case Object[] o = {1,2,3} the integer primitive values are autoboxed to Integer object and stored in the Object array o[];
Primitives values cannot be stored in Object array.
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Fri Feb 10, 2017 12:42 pm
by jamesmccreary
I find it interesting that the compiler treats
Object[] o = new int[3]
as invalid, whereas
Object[] o = new int{1,2,3}
as valid
Perhaps once we explicitly populate the array with actual integers (which become autoboxed into Integer wrapper classes), then everything is okay. Perhaps the compiler doesn't know in advance that you will populate the array with integer primitives (but what else would you do, you declared it as an int array!). I guess the compiler can only check one step at a time

Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Fri Feb 10, 2017 9:43 pm
by admin
Object[] o = new int{1,2,3} is invalid.
Object[] o = new int[]{1,2,3} is invalid as well.
Neither will compile.
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Mon May 01, 2017 2:00 am
by Aayushma
How will int[ ] iA and int iA[] point to 10 ints?
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Posted: Mon May 01, 2017 4:37 am
by admin
Aayushma wrote:How will int[ ] iA and int iA[] point to 10 ints?
iA will point to an array of 10 int elements. Like this:
iA = new int[10];