This is a real brainer.

Moderator: admin
Code: Select all
public class Test {
public static int[] getArray() { return new int[8]; }
public static void main(String[] args) {
int index = 1;
try {
getArray()[index=2]++;
}
catch (Exception e) { e.printStackTrace();} // empty catch
System.out.println("index = " + index);
}
}
As I said before, ++ is not applied on index. We are not doing index++, we are going getArray()[2]++, so whatever is the result of getArray[2], that is incremented. Not index.Javanaut wrote:Ah, Thank-you Paul for the explanation.![]()
Although, I see now that this code does throw a NullPointerException especially upon calling the printStackTrace method on the exception object, but if I pass a an array instead of null the index variable is still not post-incremented.![]()
Code: Select all
public class Test { public static int[] getArray() { return new int[8]; } public static void main(String[] args) { int index = 1; try { getArray()[index=2]++; } catch (Exception e) { e.printStackTrace();} // empty catch System.out.println("index = " + index); } }
Code: Select all
public class ArrayKoPo {
public static int[] getArray() {
return null;
}
public static void main(String args[]) {
int i = 0;
try {
int j = getArray()[i++];
} catch (Exception e) {
System.out.println(i); //prints 1 <---- This one I understand.
}
}
}
Code: Select all
public class ArrayKoPo {
public static int[][] getArray() {
return null;
}
public static void main(String args[]) {
int i = 0;
try {
int j = getArray()[i++][i++];
} catch (Exception e) {
System.out.println(i); //prints 1 <---- This one I don't understand. I thought 2 will be printed.
}
}
}
Code: Select all
int j = getArray()[i++][i++];
Code: Select all
[]
/ \
myArray 1
Code: Select all
[]
/ \
[] 2
/ \
myArray 1
Code: Select all
=
/ \
j []
/ \
[] i++
/ \
getArray() i++
Javanaut wrote:Ah, Thank-you Paul for the explanation.![]()
Although, I see now that this code does throw a NullPointerException especially upon calling the printStackTrace method on the exception object, but if I pass a an array instead of null the index variable is still not post-incremented.![]()
Code: Select all
public class Test { public static int[] getArray() { return new int[8]; } public static void main(String[] args) { int index = 1; try { getArray()[index=2]++; } catch (Exception e) { e.printStackTrace();} // empty catch System.out.println("index = " + index); } }
Code: Select all
getArray()[index=2]++
Code: Select all
public static int[] getArray()
{
return new int[]{0, 1, 4, 6, 8, 10};
}
public static void main(String[] args)
{
int index = 1;
int g = 0;
try
{
g = getArray()[index=2]++;
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("index = " + index);
System.out.println("index = " + g);
}}
Code: Select all
index = 2
index = 4
Code: Select all
g = getArray()[index=2]<<2;
No, because it is postfix.bbakla wrote:My intention is actually to print g.
if the ++ doesnt increment the variable index, shouldn't g be 5?
No, it doesn't. Not sure why you think so.I guess ++ operator increments g in this case
Is this the case ?In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Code: Select all
class X {
public static int [] apply(){
if (Y.i==Y.i)throw new NullPointerException();
return new int[]{10,8,4,6};
}
}
public class Y {
public static int i = 0;
public static void main(String[] args) {
try {
System.out.println(X.apply()[i = 3]);
} finally {
System.out.println(i);
}
}
}
Code: Select all
class Test {
public static int[ ] getArray() { return null; }
public static void main(String[] args) {
int index = 1;
try{
getArray()[index=2]++; //1
} catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Code: Select all
int temp= getArray()[index=2];
temp++;
Yes, i will remain 0. i = 3 will not be executed.qmwuzapz wrote:from the exam explanation:Is this the case ?In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Code: Select all
class X { public static int [] apply(){ if (Y.i==Y.i)throw new NullPointerException(); return new int[]{10,8,4,6}; } } public class Y { public static int i = 0; public static void main(String[] args) { try { System.out.println(X.apply()[i = 3]); } finally { System.out.println(i); } } }
Users browsing this forum: No registered users and 5 guests