클래스와 유형의 차이점
Java를 처음 접하기 때문에 class 와 type 의 개념이 혼동 됩니다. 예를 들어, 객체 "Hello World!"
가 유형 String
또는 클래스에 속해야 String
합니까? 아니면 둘 다?
클래스는 유형입니다. 인터페이스는 유형입니다. 프리미티브는 유형입니다. 배열은 유형입니다.
따라서 모든 유형은 클래스 (열거 형 상수 포함), 인터페이스, 프리미티브 또는 배열이기도합니다.
유형에는 기본 유형과 참조 유형의 두 가지 유형이 있습니다.
- 기본 유형의 변수는 항상 동일한 유형의 기본 값을 보유합니다. 이러한 값은 해당 변수에 대한 할당 작업을 통해서만 변경할 수 있습니다.
- 참조 유형의 변수는 항상 객체에 대한 참조 값을 보유합니다. 배열을 포함한 모든 객체는 class의 메소드를 지원합니다
Object
. 참조 유형은 클래스 유형 (열거 형 유형 포함), 인터페이스 유형 및 배열 유형입니다.
모든 데이터에는 구조를 정의하는 유형이 있습니다. 즉, 차지하는 메모리 양, 배치 방법, 더 중요한 것은 데이터와 상호 작용할 수있는 방법입니다.
기본 유형의 예 :
int
float
char
boolean
클래스 유형의 예 :
인터페이스 유형의 예 :
배열 유형의 예 :
int[]
String[]
Integer[][][]
기본적으로 변수로 참조 할 수있는 모든 것에는 유형이 있으며 클래스는 일종의 유형입니다.
자세한 정보 : http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html
TLDR- 클래스는 Java의 유형 중 하나입니다.
주-답을 완전히 이해하려면 Java의 제네릭에 대한 약간의 아이디어가 있어야합니다.
차이점을 이해하기 위해 먼저 Java에서 유형 이 무엇인지 이해합시다 .
JLS SE 10 에 따르면 ,
Java 프로그래밍 언어에는 기본 유형 ( §4.2 )과 참조 유형 ( §4.3 ) 의 두 가지 유형이 있습니다 .
원시 유형이란 무엇입니까?
a) 정수 유형은 byte, short, int 및 long 이며 값은 각각 8 비트, 16 비트, 32 비트 및 64 비트 부호있는 2의 보수 정수 이고 값이 16 비트 인 char UTF-16 코드 단위를 나타내는 부호없는 정수 (§3.1).
b) 부동 소수점 유형은 값이 32 비트 IEEE 754 부동 소수점 숫자를 포함하는 float 및 값이 64 비트 IEEE 754 부동 소수점 숫자를 포함하는 double 입니다.
c) 부울 유형에는 정확히 두 가지 값, 즉 true와 false가 있습니다.
이제 참조 유형이란 무엇입니까?
참조 유형에는 클래스 유형 ( §8.1 ), 인터페이스 유형 ( §9.1 ), 유형 변수 ( §4.4 ) 및 배열 유형 ( §10.1 )의 네 가지 종류가 있습니다 .
하나씩 논의합시다.
JLS에서 어떻게 보면 Class 는 다음과 같이 정의됩니다.
A class declaration specifies a new named reference type.
There are two kinds of class declarations: normal class declarations and enum declarations.
ClassDeclaration:
NormalClassDeclaration
EnumDeclaration
NormalClassDeclaration:
{ClassModifier} class TypeIdentifier [TypeParameters] [Superclass] [Superinterfaces] ClassBody
You see that [TypeParameters]
, this shows that class type includes those generic classes too.
class Example<T>{
}
The class type will be called Example
In short , a class type covers our enums , our regular (non generic) classes like String
etc and our generic classes too.
Similarly , I hope interface and array types is also clear. By array Type we mean like int[]
, String[]
etc.
Let us come to the last part - Type variables. What are they ?
A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.
Let us understand by the example in the JLS below it.
class Test {
<T extends C & I> void test(T t) {
t.mI(); // OK
t.mCPublic(); // OK
t.mCProtected(); // OK
t.mCPackage(); // OK
}
}
You see that your object in the method parameter is of type T
. Yes , this T
is Type variable and is/can be used as a reference . Yes it is. (Could not understand this strange example - Learn what is generic method in Java)
This completes the answer.
"Type" is the more inclusive category. Variables in Java can have three kinds of types: the 8 "primitive" types like int and float, interfaces, and classes. Values (as opposed to variables) can be primitive or class instances.
"Type" defines 'what type of data it is'
Ex: "hello world" is a String --> "hello world" is String type (String is not a premetive data unlike int .. so we can say "hello world" is a string class type)
10 is a int --> 10 is a integer data type.
참고URL : https://stackoverflow.com/questions/16600750/difference-between-class-and-type
'code' 카테고리의 다른 글
rsync 제외 .gitignore & .hgignore & svn : ignore like --filter = : C (0) | 2020.08.22 |
---|---|
Hash Rocket은 더 이상 사용되지 않습니까? (0) | 2020.08.22 |
JVM이 테일 호출 최적화를 방지합니까? (0) | 2020.08.22 |
회전에 쿼터니언이 사용되는 이유는 무엇입니까? (0) | 2020.08.22 |
Visual Studio 2013을 사용할 때 TFVC (TFS 버전 제어)와 소스 제어용 Git의 큰 차이점은 무엇인가요? (0) | 2020.08.22 |