Zeta Oph's Study

[Data Structure (Java)] Generic 본문

프로그래밍/Java

[Data Structure (Java)] Generic

Zeta Oph 2024. 5. 7. 18:58

https://crane206265.tistory.com/74?category=1076637

 

[Data Structure (Java)] Array

https://crane206265.tistory.com/72 [Data Structure (Java)] OOP (객체 지향 프로그래밍) & Data Typehttps://crane206265.tistory.com/71 [Data Structure (Java)] Function (함수) https://crane206265.tistory.com/61 [Java - 기초 문법] 06. 반복문

crane206265.tistory.com

원래는 SLL을 다룰 차례이지만, SLL implementation을 위해 generic부터 공부하고 가도록 하겠습니다.


Data type - Primitive type / Wrapper type

primitive type wrapper type
boolean Boolean
char Character
double Double
int Integer

 

Primitive Type

- 8 primitive types : int, long, double, float, boolean, byte, short, char

- declared and provided essentially by Java

- allocated on stack memory

 

Wrapper Class

- to handle primitive types as a class

- can be used for generic type

 

 

and, these are base to understanding generic.


Generic

data type specified by (external) user (not be specified with declaration)

--> "specifiable data type"

 

Operator : <type parameter>

- how to pass a generic type

- should receive wrapper class

- pass the received wrapper classes in order

- if empty set of <> ("diamond operator") : rely on type inference (specified by the passed parameter)

 

ex)

public class ClassName<A, B> {
    A var1;
    B var2;
    public ClassName(A a, B b) {
    	var1 = a;
        var2 = b;
    }
    ...
}

 

 

* array creation with generic

//Example : array

private E[] data = new E[capacity]; //(X)
//Java does not allow the creation of arrays of generics

private E[] data = (E[]) new Object[capacity]; //(O)