About Question enthuware.ocajp.i.v7.2.1354 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1354 :

Post 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??

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post 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.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post by mrmuiz »

As the explanation says

Code: Select all

Object[] o = new int[10];
is not valid
but what about this?

Code: Select all

Object []t={1,2,3};
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)

Code: Select all

Object []t={1,2,3};
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

Code: Select all

class java.lang.Integer

mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post by mj.anjuthan »

mrmuiz wrote:As the explanation says

Code: Select all

Object[] o = new int[10];
is not valid
but what about this?

Code: Select all

Object []t={1,2,3};
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.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post 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 :)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post 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.
If you like our products and services, please help us by posting your review here.

Aayushma
Posts: 2
Joined: Sun Apr 30, 2017 8:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post by Aayushma »

How will int[ ] iA and int iA[] point to 10 ints?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1354 :

Post 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];
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests