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

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

Moderator: admin

Post Reply
Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

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

Post by Sweetpin2 »

Why int ia1[] = {null}; throws compile error (Type mismatch: cannot convert from null to int). But below does not throw error

int ia2[][] = { {1, 2}, null };

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

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

Post by admin »

Because in case of ia[], each element of the array must be an int. A null cannot be converted into an int.
But int case of ia2[][], each element of ia2[] is an array (of ints) and since an array is an object, a null is valid value for an object.
If you like our products and services, please help us by posting your review here.

ReedClowen
Posts: 1
Joined: Sun Sep 20, 2015 8:02 pm
Contact:

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

Post by ReedClowen »

How come

Code: Select all

int [][] a = {{1},null}
System.out.print(a[1][0]);
will give you a NullPointerException but

Code: Select all

int [][] a = {{1},null}
System.out.print(a[1]);
will simply print out "null."

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

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

Post by admin »

Because in the first case, a[1] is null. So when you try to access a[1][0], you are trying to access an element in null. This will cause a NPE.
In the second case, you are not calling anything on null. You are just passing a[1] (which is null) to the print method, which checks if the argument is null, and if it is, prints null.
If you like our products and services, please help us by posting your review here.

Ingvarr
Posts: 7
Joined: Sun May 08, 2016 8:09 am
Contact:

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

Post by Ingvarr »

I wonder why the explanation talks about accessing the 3rd element of the main, top-level array (quoting: "If you try to access ia[2][0], it would have thrown ArrayIndexOutOfBoundsException because the length of ia is only 2 and so ia[2] tries to access an element out of that range. ia[2] is not null, it simply does not exist.")
The explanation per se is absolutely correct; I just don't think it's much relevant: after all, neither i nor j in the code will ever become 2. On the other hand, if ia[1][0] were some int instead of a null, an attempt to access this slot would have also thrown an AIOOBE...

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

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

Post by admin »

It is just giving the reader additional insight by contrasting ia[1] and ia[2]. One is null and one does not exist.
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.1115 :

Post by Javier »

Hi Admin!!
One person asked one question before about this piece of code:

public static void main (String[] args) {
FunWithArgs fwa = new FunWithArgs();
String[][] newargs = {args};

"Is this not trying to assign a 1d array to a 2d array?"

and the Admin answer was:

"No, it is not, notice the {}. You would be right if it were:
String[][] newargs= args; // wich of course wouldn´t compile"

My question is: What is exactly doing "String [][] newargs = {args};" in that snippet code? Thank you so much.

( I post here this question because I can not find where was posted this question, so sorry about that)

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

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

Post by admin »

{ } is a syntax for defining an array. For example, { 1 } is an array of ints with only one int element. Now, args is an array of Strings. So { args } is an array of string arrays with only one element.
If you like our products and services, please help us by posting your review here.

oumayma
Posts: 3
Joined: Sat Oct 20, 2018 7:14 am
Contact:

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

Post by oumayma »

Please why when using

String s = null;
System.out.println(s);

prints NULL

but with array

int ia[][] = { {1, 2}, null };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
System.out.println(ia[j]); It will throw a NullPointerException for ia[1][0] because ia[1] is null.

We try to access the item and print it !!!

thank you

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

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

Post by admin »

oumayma, your question is already answered in admin's reply above.
You are just passing a[1] (which is null) to the print method, which checks if the argument is null, and if it is, prints null.
The print/println methods don't invoke toString method on the argument if the argument is null. They just print the string null in such a case.
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 62 guests