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

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

Moderator: admin

Post Reply
nikolaj.mattrup
Posts: 2
Joined: Fri Sep 19, 2014 4:07 am
Contact:

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

Post by nikolaj.mattrup »

I simply don't understand this one: how come int[] array1, array2[] makes array2[] an array of arrays?

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

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

Post by admin »

Think of it this way, the square brackets attached to int apply on all the variables declared on that line. But the square brackets attached to the variable name apply only on that variable. So array1 has only one set of [] applied to it but array2 has two. Thus, array1 is an array of one dimension but array2 is of two dimensions i.e. array or arrays.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Brian B
Posts: 13
Joined: Mon Jun 16, 2014 1:07 pm
Contact:

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

Post by Brian B »

To make sure I understand this one,

this:

Code: Select all

int[] array1, array2[];
is essentially the same as:

Code: Select all

int[] array1;
int[] array2[];
or more along the lines of Java coding convention as:

Code: Select all

int[] array1;
int[][] array2;
So in order to keep them straight, you could just take lines that include multiple declarations, keep the declaration type (e.g. String[]), and re-write them (mentally or on scratch paper as: (declaration type) (reference variable); on separate lines?

So String[] array1[][], array2[], array3 translates to:

String[] array1[][]; // three dimensional array
String[] array2[]; // two dimensional array
String[] array3; // one dimensional array

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

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

Post by admin »

That is correct :)
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi Enthuware!!

I have a doubt with Arrays mixed with boxing/unboxing, I hope you can help me.

Why is not possible to convert:

Integer[] i= new int[2];
int[] ii= new Integer[2];

I have been searching in google but I couldn´t find the answer :(

Thank you for you help!!

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

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

Post by admin »

You need to read about arrays from a good book. Boxing/unboxing happenss between int and Integer.
Array of ints is neither an int nor an Integer!
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

Javier wrote:
Thu May 10, 2018 10:37 am
Hi Enthuware!!

I have a doubt with Arrays mixed with boxing/unboxing, I hope you can help me.

Why is not possible to convert:

Integer[] i= new int[2];
int[] ii= new Integer[2];

I have been searching in google but I couldn´t find the answer :(

Thank you for you help!!
Sorry, it's a bit late, but you just have to remember, int[] IS NOT A Integer[] and vice versa (of course). I think it hardly matters why.
You can though do

Code: Select all

        Object[] oa  = new Object[1];
        Integer[] ia = new Integer[4]; 
        oa=ia; //assign an Integer[] to an Object[] ie subclass array to superclass array
        oa[3]=99;  //note the index
        //oa[3]++; //Compilation error: oa is still a reference of type Object[] even though "we know" it points to an Integer[]
        ((Integer[])oa)[3]++; //OK - explicit cast of oa to Integer[] before indexing into it
        System.out.println(oa[3]); //100

sebaskoot
Posts: 3
Joined: Mon Aug 27, 2018 2:15 am
Contact:

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

Post by sebaskoot »

Hi admin,
In the answers I read array2 = array3; this confuses me since = is the assignment operator.
My suggestion: == would be better than =, or length would even be better.
Would you please consider changing the answers?
Thanks ,
Sebastiaan

NOTE:

Code: Select all

array2=new int[][]{};//initialisation needs to happen first
array3=new int[][]{};
		
System.out.println((array2.length == array3.length));//prints true

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

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

Post by admin »

Hello,
The problem statement asks you to identify the valid statements. All the options are potentially valid statement out of which 1, 2, and 5 are valid statements that assign array pointed to by one variable to another (that is why the assignment operator is used in these statements. There is no need to change them because you can expect such statements in the exam.
If you like our products and services, please help us by posting your review here.

sebaskoot
Posts: 3
Joined: Mon Aug 27, 2018 2:15 am
Contact:

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

Post by sebaskoot »

I understand your explanation and agree, thanks

ibrahimovic
Posts: 1
Joined: Tue Sep 18, 2018 6:42 am
Contact:

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

Post by ibrahimovic »

sebaskoot wrote:
Wed Aug 29, 2018 8:52 am
I understand your explanation V Tight Gel here and agree, thanks
same here :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest