Index
Enum is a set of final values which are not changing; it makes it way easy to handle them without some errors occuring by overlaped names. It also made to use with Switch method; it is beacuse Switch method only takes "char, byte, short, int, Character, Byte, Short, Integer, String, enum," as its parameter.
How to make it:
enum EnumName { Element1, Element2, Element3, Element4 }
Generic is the way used for Class and method, which generalize the its type, so that it can decide its type when it is instantiated. In other words, it makes it possible to make an undecided Class or Method at the moment it is declared; it can be varied when it is newly instantiated.
There is basic grammar how to make Generic Class:
class ClassName<T> {
private T item;
public ClassName(T item) { // constructor
this.item = item;
}
public T getItem() { //getter
return item;
}
public void setItem(T item) { //setter
this.item = item;
}
}
Basket<String> basket1 = new Basket<String>("String");
Basket<Integer> basket2 = new Basket<Integer>(int);
Basket<Double> basket3 = new Basket<Double>(double);
class Basket {
...
public <T> void add(T element) {
...
}
}
basket.<Integer>add(int); // or
basket.add(int);
interface A { ... }
class B implements A { ... } // B implements the interface A
class C extends B implements A { ... }
// C inherits B which implements A
class Basket<T extends B(Super class) & A(interface)> {
// Basket class can only take the types which is extends B and implements A(inheritance(Super) must declared first than interface)
private T item;
...
}
class Main {
public static void main(String[] args) {
// Instantiation
Basket<B> flowerBasket = new Basket<>();
Basket<C> roseBasket = new Basket<>();
}
}
<? extends T> // T and SubClasses which inherits T only
<? super T> // T and its SuperClass only
Complie Error(Syntax Error):
It refers to the errors occurring in compiling; it mostly relates to grammaric mistakes(Syntax).
Runtime Error:
It refers to the errors occurring in runtime(When the program starts)
Exception can be handled by using Exception with try/catch block.
try {
// codes
}
catch (ExceptionType1 e1) {
//operation codes when the codes meet the exception of ExceptionType1 type.
}
catch (ExceptionType2 e2) {
// operation codes when the codes meet the exception of ExceptionType2 type.
}
finally {
// optional
// codes that must be operated(Lastly).
}