OCP - 2
Functional Programming, Dates, Strings, Localization, Exceptions and Assertions
Functional Programming, Dates, Strings, Localization, Exceptions and Assertions
Kartei Details
Karten | 115 |
---|---|
Sprache | English |
Kategorie | Informatik |
Stufe | Andere |
Erstellt / Aktualisiert | 22.02.2019 / 08.01.2020 |
Weblink |
https://card2brain.ch/box/20190222_ocp_2
|
Einbinden |
<iframe src="https://card2brain.ch/box/20190222_ocp_2/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
Lernkarteien erstellen oder kopieren
Mit einem Upgrade kannst du unlimitiert Lernkarteien erstellen oder kopieren und viele Zusatzfunktionen mehr nutzen.
Melde dich an, um alle Karten zu sehen.
When no exception is declared on the close method of a specific implementation on AutoClosable, do you have to handle Exception when using the try-with-resources statement?
No, you only have to handle the specified checked exception of the concrete AutoCloseable implementation
What is the idea of suppressed exceptions?
When multiple exceptions occur, eg. in a try block and also when attempting to close a resource, the first exception is assumed th primary one and all other exceptions are attached as suppressed exceptions to the primary one
Is the exception from the finally block handled as a suppressed exception?
try(JammedTurkeyCage t = new JammedTurkeyCage()){
throw new IllegalStateException("turkey ran off");
} finally {
throw new RuntimeException("haven't found it");
}
no, Suppressed exceptions apply only to exceptions thrown in the try clause.
The RuntimeException from the finally block effecively swallows the IllegalStateException, which means that it is lost
What does this code do?
public void rerhrowing() throws SQLException, DateTimeParseException {
try {
parseData();
} catch (Exception e) {
System.err.println(e);
throw e;
}
}
It is a short for the following code to prevent duplicates. Java can infer the specific Exception from the method signature
public void rerhrowing() throws SQLException, DateTimeParseException {
try {
parseData();
} catch (SQLException | DateTimeParseException e) {
System.err.println(e);
throw e;
}
}
What are assertions for?
They can verify that sth. is always true. They are intended for development and NOT for production!
What must be considered when using assertions?
- Because they only run optional, assertions should not have sideeffects!
- Asserts are disabled by default and must explicitly be enabled
What happens when an assertion fails?
A java.lang.AssertionError is thrown. Since programs aren't supposed to catch an Error, this means that assertion failures are fatal and end the program.
How can you enable/disable assertions?
- Enable using the JVM -enableassertions (-ea) flag
- Disable using the JVM -disableassertions (-da) flag
What happens here?
java -ea:com.example.demos... my.programs.Main
Enable assertions in any class in the specified package or subpackage.
Should asserts be used to check for valid arguments passed into a method?
NO, use if-statements and IllegalArgumentExceptions instead!
The variable in a multi-catch expression is ___.
The variable in a multi-catch expression is effectively final
Can you use Closeable in a try-with-resources statement?
YES
How can you specify a message for an assert statement?
Using a colon after the boolean expression, eg:
assert false : "This always goes wrong"
Is IllegalFormatException a checked exception?
No
What happens when calling Optional.of(null)
A NullpointerException is Thrown
What is the return type of Predicate?
Primitive boolean (prmitive)
What common Built-In Functional Interfaces do exist?
- Supplier
- Consumer
- BiConsumer
- Predicate
- BiPredicate
- Function
- BiFunction
- UnaryOperator
- BinaryOperator
In which package can the built-in functional interfaces be found?
java.util.functional
Does the Predicate class define only exactly one method?
Only one abstract method, but some additional utility default implementations, such as and.
This makes it's use convenient: eg, given two predicates egg and brown: egg.and(brown)
What is the goal of the Function Interface?
Turning one parameter into a value of a potentially different type and returning it
What is the goal of the Unaryperator Interface?
A special case of Function, for which the Input and Output Parameter are the same. It is actually a subclass of the Function interface
What happens here?
Predicate ex = String::isEmpty;
It is missing the generic for Predicate. This makes the parameter that was passed an Object rather than a String. The lambda expects a String because it calls a method that eists on String rather than Object. Therfore, it does not compile
What static factory methods exist on Optional?
- Optional.empty()
- Optional.of(123)
- Optional.ofNullable(val)
What instance methods exist on Optional?
- opt.isPresent()
- opt.get()
- ifPresent(Consumer c)
- orElse(T other)
- orElseGet(Supplier s)
- orElseThrow(Supplier s)
What happens when get() is called on an Optional that is empty?
An exception is thrown
Of what does a stream consist?
- Source
- Intermediate operations
- Terminal operation
In wich package are stream related functions?
java.util.sream
What are common factory methods to create a stream?
- Stream.empty()
- Stream.of(1, 2, 3)
- aCollection.stream()
- aCollection.para;parallelStream()
- Stream.generate(Math::random)
- Stream.iterate(1, n -> n + 2)
What are Reductions?
A special type of terminal operation where all of the contents of the stream are cobined into a single primitive or Object
What are the terminal stream operations?
- allMatch() / anyMatch() / noneMatch()
- collect()
- count()
- findAny()/findFirst()
- forEach()
- min()/max()
- reduce()
-
- 1 / 115
-