SCJP5 and SCWCD1.5
Think Twice Act Wise...
SCJP 6
can someone please explain why option d is true.
SCJP 6
Originally posted by Garima
can someone please explain why option d is true.
thanks
Harvinder
d. In this code example an assert statement could not be used in place of the "throw" statement.
default:{
assert (true): new AssertionError();
return "";
}
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:{
assert (true): new AssertionError();
return "";
}
SCJP 6
Originally posted by punit singh:
As no case has break statement, so everytime assert statement will get executed. Assert could be used with default, but in situation where it is not possible or error to control reach default case.
see here no case statement has break; means what? default case will be executed every time you call switch() with any value.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6
Originally posted by Punit:
see here no case statement has break; means what? default case will be executed every time you call switch() with any value. That is not the use of assertion.
thanks
Harvinder
Originally posted by Harvinder Thakur:
if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6
SCJP 6
The reason is because the method is returning int value
it must return something instead of returning void.
So it must either throw or return something.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6
Originally posted by punit singh:
James where is Preetha Arun' post ?
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Originally posted by geeta vemula:
method m1 is returning String. So it must return something.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;//error must return String here or
}
//or here
}
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false; return "D";
}
}
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;
}
return "D";
}
SCJP 6
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
SCJP 6
Originally posted by Geeta Vemula:
The above reason is not the correct explanation for the option d to be correct.
The reason is because the method is returning int value, it must return something instead of returning void. this is what i thought. Tell me whether I am correct or not.
thanks
Harvinder
All code in my posts, unless a source is explicitly mentioned, is my own.
Originally posted by Ruben:
throw new AssertionError();
is equivalent to:
assert false;
only when assertions are enabled when you launch the program. If assertions are disabled, the second statement is not even seen by the JVM. However, the first statement remains regardless of whether assertions are enabled or not.
Originally posted by Ruben:
But what would be the point of returning a String when an exception or error that is not caught is thrown?
thanks
Harvinder
can someone please explain why option d is true.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
in place of the "throw" statement.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
thanks
Harvinder
Originally posted by Harvinder Thakur:
I agree with Geeta and Ankit and all those who say that the reason why option d is correct is the return statement missing in default statement.
![]()
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
All code in my posts, unless a source is explicitly mentioned, is my own.