Help coderanch get a
new server
by contributing to the fundraiser

Renaud Richardet

Greenhorn
+ Follow
since Dec 10, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Renaud Richardet

thanks all for the valuable posts! it helped me a lot.
unfortunately, i can't use the real enums from tiger on this project.
Ernest, is there a name behind the pattern you showed us? (or shall we call it the Ernest Friedman-Hill-pattern from now on )

thanks again, Renaud
19 years ago
hello
in the project i'm currently working on, we use a flag-mechanism to set and retrieve some properties. here's an example of the code:

is there a way to make this mechanism safer. just imagine that somebody calls setFlags(38): this blows the whole thing!
maybe there's a way with the new enums???

Thanks for any insights, renaud
19 years ago
Hi Mubeen

Look at my post on the other forum. (Boy, it looks like multithreading posting )
Hi Dave

I completely agree with your first post. However, i don't get it when you say:

Here e3 doe not inherit from e2.

on your second post. I believe the heritage diagram from Mubeen is right.

Cheers, Renaud
19 years ago

i got it that it is because of that Switch Run tim phenomenon, But how come compiler knows what is going to be called.....


He doesn't know which of the 3 Exeption will be thrown. But he knows that in example 1, whatever Exeption is thrown will be catched at your first catch ("catch (Level1Exception e) {b++;}") and there's no need for a second catch ("catch (Level3Exception e) { d++;}")


Still in both cases error should not come,level 3 is sub class to level1 and level2 . so once it is not reachanbel and once it is reachable....


in example 2, the compiler thinks that the catch-block could throw a Level1Exception. Therefore, he lets you write a second catch clause (EVEN IF IT WILL NEVER BE REACHED, i guess compilers are silly sometimes ).
Hi Mubeen
I have posted an answer to this forum, where you also posted this question. please don't post your question twice.
Renaud
19 years ago
very surprising to me too!
(for example 1, the compiler says "Unreachable catch block for Level3Exception. This exception is never thrown from the try statement body")

well, let's try to make things simpler: just remove the switch-block:

this time, both examples will fail compilation, as expected

so the switch-block is the answer to your problem:
a switch-block will be resolved only at runtime. that means that the compiler doesn't know what will happen with it at compilation time. all what the compiler knows, is that this switch clause can throw 3 different exceptions (Level1Exception, Level2Exception, Level3Exception).

in example 1, the compiler sees that all exceptions that could possibliy be thrown from the switch clause are catched at the line "catch (Level1Exception e) {b++;}". Therefore, he can be sure that the line "catch (Level3Exception e) { d++;}" will NEVER be reached. And as a brave compiler, he doesn't let the code compile.

in example 2, the compiler cannot be sure that all exceptions are catched at the line "catch (Level2Exception e) {b++;}". Indeed, the compiler thinks that the catch-block could throw a Level1Exception. Therefore, he lets the code compile.

If you still have trouble figuring out how the compile works, maybe it will help you if you replace x=3; by "x = new java.util.Random().nextInt(4);" which produces a random integer from 0 to 3. In this way, we don't know what the switch-block will do.

hth, renaud
Sulbha, maybe that quote will help:

for anyone whose primary interest is in simply taking the SCJP exam, rather than esoterica which aren't on the exam: the real exam will not have any questions which depend on knowing about the string pool and whether or not String literals are eligible for GC. The test authors have deliberately avoided this issue because it's way too nitpicky, even for them. Now mock exams may well ask questions which require this knowledge - but the real exam will not.


(quote by sheriff Jim Yingst on this tread
try that tread or this one or search the forum.

hth
renaud
hello ingo

"The wildcard parameterized type MyClass<? super String> is the family of all instantiations of the MyClass interface for type argument types that are supertypes of String"
found on the Generics FAQ from Angelika Langer

hth
renaud
Bilal, thanks for your notes, a very useful overview for me.
some mistake i found:
Iterator hasNext()
JavaBeans naming standards: setXXX(), getXXXX(), boolean isXXX()
void Arrays.binarySearch(Object[], key);
void Arrays.binarySearch(Prim[], key);

again, thanks for sharing
renaud
hello Vallabh

Can a labelled break statement be used to transfer control out of if {} Block. According to Khalid Mughal book it cannot..


first of all, your error on line 63 is not specific to an if-block or any loop: the java compiler will complain if a line of code can never be reached. you can try:

and watch the compiler complain about line 3.

so, now remove this silly line 63 and see what happens: it prints "1" on the console. that means that "break b1;" EXITS the if-statement (it is only executed once!).
could you tell me the meaning of this code? in my opinion, you should not try to exit an if-block this way. i would prefer to nest several if-statements, for example:


your second code example won't compile. an unlabelled break statement in an if-block without a loop or switch statement is illegal. it cannot be used to exit the if-loop.
for it to compile, you want to enclose it in a loop or switch statement:


here a quote from Bruce Eckels' "thinking in java", p 153.
"break and continue keywords will normally interrupt only the current loop, but when used with a label, they'll interrupt the loops up to where the labels exists."

HTH,
renaud

ps: when you paste code, just use the UBB button "CODE" on the Form.
This all started with an innocent question of a mock exam, but when i added some autoboxing features, here's what happend...
So young Tigers, can you spot which line will be printed, and which will (individually) cause a compile-errror or runtime-error?
Enjoy, Renaud

Originally posted by Jianfeng Qian:
What the different between compile and runtime ?
Sometime I was comfused by it,Which kinds of prolbems will happen when compile ,and what when runtime.


Janfeng, are you coding in an IDE? I also had this confusion in the past, because i started to code with java right away in an IDE. You just click on "run", and compilation+runtime happen (almost magically) all at once.
Well, you might want to understand that magic, because i believe it's very important to be able to distiguish btw compile and runtime.
cheers, renaud
ouups, sorry i missed it
thanks for the pointer
renaud