About Question enthuware.ocajp.i.v7.2.1354 :
Moderator: admin
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1354 :
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??
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??
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 49
- Joined: Mon Jul 27, 2015 4:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
As the explanation says
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
where integers literals are atomatically wrapped into Integers objects before being added to the array. This would explain why
prints
but what about this?is not validCode: Select all
Object[] o = new int[10];
Code: Select all
Object []t={1,2,3};
It's a bit confusing to me, but probably it's accepted because (maybe)
Code: Select all
Object []t={1,2,3};
Code: Select all
Object []t=new Object[10];
t[0]=1;
t[1]=2;
t[2]=3;
Code: Select all
Object []t={1,2,3};
System.out.println(t[0].getClass());
Code: Select all
class java.lang.Integer
-
- Posts: 10
- Joined: Thu Nov 10, 2016 3:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
In the case Object[] o = {1,2,3} the integer primitive values are autoboxed to Integer object and stored in the Object array o[];mrmuiz wrote:As the explanation saysis not validCode: Select all
Object[] o = new int[10];
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...Code: Select all
Object []t={1,2,3};
Primitives values cannot be stored in Object array.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
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
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
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
Object[] o = new int{1,2,3} is invalid.
Object[] o = new int[]{1,2,3} is invalid as well.
Neither will compile.
Object[] o = new int[]{1,2,3} is invalid as well.
Neither will compile.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Apr 30, 2017 8:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
How will int[ ] iA and int iA[] point to 10 ints?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1354 :
iA will point to an array of 10 int elements. Like this:Aayushma wrote:How will int[ ] iA and int iA[] point to 10 ints?
iA = new int[10];
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Google [Bot] and 23 guests