About Question enthuware.ocajp.i.v7.2.1115 :
Moderator: admin
-
- Posts: 27
- Joined: Thu Feb 07, 2013 9:46 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1115 :
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 };
int ia2[][] = { {1, 2}, null };
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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.
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.
-
- Posts: 1
- Joined: Sun Sep 20, 2015 8:02 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
How come
will give you a NullPointerException but
will simply print out "null."
Code: Select all
int [][] a = {{1},null}
System.out.print(a[1][0]);
Code: Select all
int [][] a = {{1},null}
System.out.print(a[1]);
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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.
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.
-
- Posts: 7
- Joined: Sun May 08, 2016 8:09 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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...
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...
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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)
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)
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
{ } 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.
-
- Posts: 3
- Joined: Sat Oct 20, 2018 7:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1115 :
oumayma, your question is already answered in admin's reply above.
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.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.
Who is online
Users browsing this forum: No registered users and 94 guests