I got the right answer on this question, but now I'm wondering why :
I thought parenthesis had the highest precedence, so does it mean that "calling a method" has a highest precedence than using parenthesis ?int i = doIt() / (j = 2);
Thanks :]
Moderator: admin
I thought parenthesis had the highest precedence, so does it mean that "calling a method" has a highest precedence than using parenthesis ?int i = doIt() / (j = 2);
So in doIt() / (j = 2);, the method doIt() will be called first because it is the left operand of /.The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Code: Select all
public class Enthu_2_1260 {
public static void main(String[] args) {
try {
int i = 15 / (0 * doIt());
} catch (RuntimeException re) {
System.out.println(re);
} catch (Exception e) {
System.out.println(e);
}
}
public static int doIt() throws Exception {
throw new Exception("FORGET IT");
}
}
with this code:admin wrote:Throw statement is a valid replacement for a return statement because a method can either complete successfully (in which case it will return a value) or it can fail to do it job and end up with an exception (in which case, it will not be able to return a value). The method can either throw an exception explicitly (as is happening here) or the method may call some other method which throws an exception (for example, if this method tries to open a file).
Code: Select all
public class Sample {
public static void main(String[] args) {
}
public static String method() throws Exception {
methodHelper();
}
public static void methodHelper() throws Exception {
throw new Exception();
}
}
Code: Select all
public class TestClass {
public static void main(String[] args) {
int j = 1;
int i;
try {
i = doIt() / (j = 2);
System.out.println(i);
} catch (Exception e) {
System.out.println(" j = " + j);
}
}
public static int doIt() throws Exception {
throw new Exception("FORGET IT");
}
}
Code: Select all
System.out.println(i);
Users browsing this forum: No registered users and 105 guests