[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
1) What if the there's no terminal methods - will the program do nothing, not compile or give an exception at runtime?
2) What if there are 2 terminal methods - will the program do nothing, not compile or give an exception at runtime?
3) What if you start a new stream with the same stream reference, will the compiler catch that one?
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:That is why Checked Exceptions are considered part of the signature of methods.
Junilu Lacar wrote:There's a section in the JLS that discusses unreachable statements. The end of that section explains why if-statements are treated differently by the compiler.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21
Mike Simmons wrote:
Jesse Silverman wrote:That is why Checked Exceptions are considered part of the signature of methods.
Well, not according to the JLS: 8.4.2. Method Signature. The signature consists of the method name, the type parameters, and the formal parameter types. No throws clause, and no return type. Paradoxically, both the throws clause and the return type are part of what the Java Virtual Machine Specification refers to as the "signature". But how the JVM defines things is an implementation detail, from the Java language perspective. If you're trying to decide whether you're looking at an override or an overload, for example, you need to follow the JLS definition of signature, and ignore the return type and throws clause. Then you immediately remember them again when you try to decide if it's a legal override.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
John Cunningham wrote:But, if you don't mind, I will give an example. What if a have a stream with a source and at possibly some intermediate methods. Does the compiler know that there needs to be one and only one terminal method?
There's nothing magical about terminal Stream methods. They're just regular methods like any other. The only reason we know they're terminal is because the documentation says they're terminal. If you hadn't read the Javadocs, calling two different terminal methods on the same stream would just look like valid Java code. You need to evaluate the two different method calls to find out that it will throw a runtime exception.
Jeanne Boyarsky wrote:The compiler will only catch if there is a syntax error in your example.
...
2) What if there are 2 terminal methods - will the program do nothing, not compile or give an exception at runtime?
Not compile. Because a terminal method doesn't return a stream so the compiler knows something is wrong when you try to add a second one.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:So basically, if something can be determined to be wrong at compile time,l (and not everything that superficially seems to be in that case is) it will be a compiler error.
If not, runtime exception.
Junilu Lacar wrote:
Jesse Silverman wrote:So basically, if something can be determined to be wrong at compile time,l (and not everything that superficially seems to be in that case is) it will be a compiler error.
If not, runtime exception.
More or less, yes. I might tweak that a little bit to be "if something can be determined to be wrong based on information that can be gleaned from type definitions, syntax rules, and static analysis at compile time, it will be a compiler error."
Determining if a line of code is unreachable is an example of what I mean by "static analysis."
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Junilu Lacar wrote:I might tweak that a little bit to be "if something can be determined to be wrong based on information that can be gleaned from type definitions, syntax rules, and static analysis at compile time, it will be a compiler error"
Jesse Silverman wrote:With my normal mix of trepidation and daring in contradicting anything you ever say
1. Runs on code bases that might take an hour to compile could take ten, twenty or forty hours as the algorithms go wild with "What if?" and
Mike Simmons wrote:
Junilu Lacar wrote:I might tweak that a little bit to be "if something can be determined to be wrong based on information that can be gleaned from type definitions, syntax rules, and static analysis at compile time, it will be a compiler error"
However, "static analysis" could include many things that are not compiler errors, because the designers of Java didn't want different compilers to behave too differently - they regulated exactly what types of problems would result in a compiler error, and which would not. So for example...
And Java's creators didn't want to create a situation where some smart compilers will refuse to compile code, and other less smart compilers would compile it - or vice versa. Instead, the idea is that any valid Java compiler should produce roughly the same result. So they basically decided that compilers would not take note of the actual values of variables. Instead the compiler only pays attention to the type of the variable. And even then, not the actual type of the object referenced by the variable, but the declared type of the variable.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|