Test 2 Flashcards
ConcurrentMap
It is a Map providing atomic versions of putIfAbsent, remove, and replace methods. dont provide any additional method
When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. The overriding declaration must have a throws clause that is compatible with ALL the overridden declarations. the most childish
When heredando d multiples interfacez, todas deben de tirar la misma excepcion o jerarquiaa y enla implementacion deben de tirarse la de jerarquia menor la mas nieta o ninguna
Cuando se implementa una interfaz, metodo que tire excepcion no es necesario tirarla tambien en la implementacion, o se puede tirar la excepcion o una hija de la exception
Putting a RuntimeException such as NullPointerException in the throws clause, is practically same as not putting it in the throws clause. In this case, as the base method has only a RuntimeException in its throws class, the overriding cannot throw any checked exception. It can throw any RuntimeException.
Java 5 onwards supports co-variant return types, which means that an overriding method can declare any sub-class of the return type declared in the overridden method as its return type.
If the parent throws a uncheck exception, the child can not throw a checked exception, only any unchecked exception
Sii una interfaz tiene metodo default pero una hija lo redeclara como abstracto, se vuelve abstracto para los que implementen o extiendan esa interfaz hija
You cannot use super keyword to call a method defined in the super interface.
interface A { default void hello() { } }
interface B extends A {
default void hello() {
6super.hello();//This is NOT valid. A.super.hello();//This is valid.
} }
public class TestClass implements B {
public void hello() {
super.hello();//This is NOT valid.
A.super.hello(); //This is NOT valid because TestClass does not implement A directly.
B.super.hello(); //This is valid.
} }
An enum cannot extend another enum or class. It may implement an interface though.
Enum constants cannot be instantiated/created using the new keyword.
Enum constructor is always private. You cannot make it public or protected. If an enum type has no constructor declarations, then a private constructor that takes no parameters is automatically provided.
An enum is implicitly final, which means you cannot extend it.
Compiler provides an enum with two public static methods automatically -values() and valueOf(String). The values method returns an array of its constants and valueOf method tries to match the String argument exactly (i.e. case sensitive) with an enum constant and returns that constant if successful otherwise it throws java.lang.IllegalArgumentException.
facts about java.lang.Enum
It implements java.lang.Comparable (thus, an enum can be added to sorted collections such as SortedSet, TreeSet, and TreeMap).
It has a method ordinal(), which returns the index (starting with 0) of that constant i.e. the position of that constant in its enum declaration.
- It has a method name(), which returns the name of this enum constant, exactly as declared in its enum declaration.
Enums can be compared using ==.
it should be: case OFF instead of case Switch.OFF.
- collect(Collectors.groupingBy(Item::getCategory)) returns a Map where the keys are the Category values and the values are Lists of the elements.
- collect(Collectors.averagingDouble(priceF)) returns the average of the values returned by the priceF function when it is applied to each element of the Stream. In this case, a stream is created from the List of elements belonging to each categtory by the call to forEach.
ToDoubleFunction can take any object and returns a double.
En clases genericas el diamante va luego del nombre de la clase, en los metodos va antes del tipo de retorno del metodo
Generic Class: public class Box { // T stands for "Type" private T t; } is called: Box integerBox;
Generic method public static boolean compare(Pair p1, Pair p2) { } is called: boolean same = Util.compare(p1, p2);
To customize the behavior of class serialization, the readObject and writeObject methods should be implemented.
Developers can change how a particular class is serialized by implementing two methods inside the class file.
These methods are: private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
Notice that both methods are (and must be) declared private, proving that neither method is inherited and overridden or overloaded. The trick here is that the virtual machine will automatically check to see if either method is declared during the corresponding method call. The virtual machine can call private methods of your class whenever it wants but no other objects can.
Thus, the integrity of the class is maintained and the serialization protocol can continue to work as normal.
Object of any class that implements Serializable interface can be serialized, even final or private or whatever
Constructor of the class for an object being deserialized is never invoked.
However, constructor for the super class may be invoked if the super class does not implement serializable interface.
Any field that is not marked transient but points to an object of a class that does not implement Serializable, will cause an exception to be thrown
Types of reduce
There are three versions of reduce : Optionalreduce(BinaryOperator accumulator) Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. or returns an empty Optional
Treduce(T identity, BinaryOperator accumulator) Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
<u> Ureduce(U identity, BiFunction<u> accumulator, BinaryOperator<u> combiner) Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.</u></u></u>
preparedStament vs Stament
PreparedStatement offers better performance when the same query is to be run multiple times with different parameter values.
PreparedStatement allows several additional SQL types such as BLOB and CLOB.
methods are available in java.io.Console?
readPassword reader writer readLine format
Overriding method can throw any exception as long as it is a subclass of any of the exceptions thrown by the overridden method.
It can also be a subclass of RuntimeException. eg. If the base class A has: void m1() throws IOException then overriding method in class B can be : void m1() throws FileNotFoundException because FileNotFoundException is a subclass of IOException.
Cuando se override, el hijo debe de tener el mismo o mayor acceso que el padre, es decir si el metodo es default, el hijo debe ser default, protected o public, mas no privado
c.setAutoCommit(false)
Tells to commit or not inmediately the changes to database,
To commit the changes you can call con.commit() or set autoCommit(true)