I am using the example from Java the complete reference by Herbert Schildt, 12th ed and Java is 14
Where he gives the 2 following examples (if blocks), the first one is ok and the 2nd one is wrong therefore commented
public class PatternMatch {
public static void main(String[] args){
Number myOb = Integer.valueOf(9);
int count = 10;
if((count < 100) && myOb instanceof Integer iObj) { // is OK!!!
iObj = count;
}
/*
if((count < 100) & myOb instanceof Integer iObj) { // Error!!!
iObj = count;
}
*/
}
}
His explanation on why the 2nd if block (commented out) is "In this case, the compiler cannot know whether or not iObj will be in scope in the if block because the right side of the & will not necessarily be evaluated". This leads to my confusion: & makes sure both its left and right sides are ALWAYS evaluated so why "because the right side of the & will not necessarily be evaluated"?
Thanks!
if you put the java14 code in your IDE, you do see the 2nd if block produces the compilation error saying iObj not defined... but I just don't understand his explanation:
His explanation on why the 2nd if block is "In this case, the compiler cannot know whether or not iObj will be in scope in the if block because the right side of the & will not necessarily be evaluated". This leads to my confusion: & makes sure both its left and right sides are ALWAYS evaluated so why "because the right side of the & will not necessarily be evaluated"?
public class PatternMatch {
public static void main(String[] args){
Number myOb = Integer.valueOf(9);
int count = 10;
if((count < 100) && myOb instanceof Integer iObj) { // is OK!!!
iObj = count;
}
if((count < 100) & myOb instanceof Integer iObj) { // Error!!!
iObj = count;
}
}
}