Code States BE 부트캠프 #12

John Jun·2023년 5월 2일
0

Index


1. Enum

2. Generic

3. Exception Handling

4. Collection Framework


1. Enum

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 }

2. Generic

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); 
  • We can also make restriced Generic class in what specific types it would take, with the keyword "extends."
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<>();
    }
}
  • Using Wildcard(?)
<? extends T> // T and SubClasses which inherits T only
<? super T> // T and its SuperClass only

3. Exception Handling

  • 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).
}

4. Collection Framework

1. List

  • ordered data
  • duplicated data: ok
  • implements: ArrayList, Vector, Stack, LinkedList

2. Set

  • Not ordered data
  • duplicated data: No
  • implements: HashSet, TreeSet

3. Map<K,V>

  • saves a pair of key and value
  • Not ordered data
  • key cannot be duplicated/ value can be duplicated
  • implements: HashMap, HashTable, TreeMap, Properties
profile
I'm a musician who wants to be a developer.

0개의 댓글