<요약>
ㅇ 클래스, 함수, 인터페이스 등이 다양한 자료형들과 함께 동작할 수 있도록 정의하는 방법.
> 말이 어려운데, 더 쉽게 표현하자면 사용자가 원하는 자료 형식으로 정의한 클래스,인터페이스,함수 등을 쓸 수 있게 하는 것. 정도로 이해하면 됨.
<아래는 영어 원문 설명>
In Java, **Generics** allow you to write classes, interfaces, and methods that can operate on any data type while providing type safety at compile time. It enables code reuse and flexibility without compromising type safety, as it lets you define a placeholder for data types, which can be replaced with actual types when the class, method, or interface is used.
### Generic Type Definition in Java
A **generic type** in Java is a way of specifying that a class or method can work with different data types without sacrificing type safety. Generics are typically denoted using angle brackets (`<>`).
#### Example of a Generic Class
```java
// A generic class definition
public class Box<T> {
// T is a placeholder for the actual data type
private T content;
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
```
In this example, the class `Box` is defined with a generic type parameter `T`. When you create an instance of this class, you can specify the actual type to use:
```java
Box<String> stringBox = new Box<>();
stringBox.setContent("Hello");
System.out.println(stringBox.getContent()); // Outputs: Hello
Box<Integer> integerBox = new Box<>();
integerBox.setContent(100);
System.out.println(integerBox.getContent()); // Outputs: 100
```
Here:
- `Box<String>` creates a `Box` that can hold `String` values.
- `Box<Integer>` creates a `Box` that can hold `Integer` values.
### Using Generics in Methods
You can also define generic methods that work with different types, regardless of the type of the class.
#### Example of a Generic Method
```java
public class Utility {
// A generic method definition
public static <T> void printArray(T[] inputArray) {
for (T element : inputArray) {
System.out.println(element);
}
}
}
public class Main {
public static void main(String[] args) {
// Using the generic method with different types
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Hello", "World"};
Utility.printArray(intArray); // Prints integers
Utility.printArray(strArray); // Prints strings
}
}
```
In the example above, the method `printArray()` can accept arrays of any data type because it is defined as a generic method using `<T>`.
### Wildcards in Generics
You can also use wildcards with generics to define a range of acceptable types. The most common wildcard is the `?` (question mark), which represents an unknown type.
#### Example with Wildcards
```java
public void printBox(Box<?> box) {
System.out.println(box.getContent());
}
```
In this case, `Box<?>` means the method can accept a `Box` of any type (`Box<String>`, `Box<Integer>`, etc.).
### Bounded Type Parameters
Sometimes, you want to restrict the types that can be used with a generic type. You can achieve this using **bounded type parameters**.
#### Example of Bounded Type Parameters
```java
public class NumberBox<T extends Number> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public double getDoubleValue() {
return value.doubleValue();
}
}
```
Here, the `NumberBox<T>` class is restricted to types that are subclasses of `Number` (like `Integer`, `Double`, `Float`, etc.). This allows you to call methods available on the `Number` class, like `doubleValue()`.
### When to Use Generics
Generics are used when:
1. **Type Safety**: You want to ensure that the compiler can check for type correctness at compile time, reducing the chance of `ClassCastException` at runtime.
Without generics:
```java
List list = new ArrayList(); // Raw type
list.add("Hello");
String s = (String) list.get(0); // Needs casting, which is error-prone
```
With generics:
```java
List<String> list = new ArrayList<>();
list.add("Hello");
String s = list.get(0); // No need for casting, type safety ensured
```
2. **Code Reusability**: You can write generic classes and methods that can be reused for different types without rewriting the code.
Instead of writing separate classes or methods for different data types, you can use generics to write one version that works for all.
3. **Cleaner Code**: It makes the code more readable and maintainable, as the data type being used is explicit.
4. **Eliminating Casting**: With generics, you don’t have to manually cast objects when retrieving them from a collection or container class, reducing the risk of runtime errors.
### Common Use Cases of Generics
1. **Collections Framework**: Most classes in the Java Collections Framework (like `List`, `Map`, `Set`, etc.) are generic.
Example:
```java
List<String> strings = new ArrayList<>();
strings.add("Hello");
```
2. **Custom Data Structures**: When implementing custom data structures like stacks, queues, or linked lists that work with any data type.
3. **Utility Classes**: Classes that perform generic operations, like sorting, comparing, or filtering lists of objects, often use generics.
### Conclusion
- **Generics** allow you to create flexible and reusable classes, methods, and interfaces in Java.
- They provide **compile-time type safety**, ensuring that you can avoid many runtime errors, such as `ClassCastException`.
- Generics are used in a wide variety of situations, especially in the **Collections Framework**, where you frequently work with different types of data.
'Work' 카테고리의 다른 글
| [C#, CSharp] Web method : 자바스크립트단에서 서버단 코드 호출해서 쓰기. (0) | 2024.09.24 |
|---|---|
| [JAVA] REST API가 뭐야? (4) | 2024.09.23 |
| [C#, CSharp] IIS에서 TLS 1.2 버전 활성화 방법 (2) | 2024.09.20 |
| TLS 가 뭐야? (1) | 2024.09.20 |
| [JAVA] Interface (인터페이스) - 개념, 왜 쓰는지? (0) | 2024.09.20 |