Java 类和类型的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16600750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between class and type
提问by user2236096
Being new to Java, I'm confused between the concepts of classand type.
For example, should the object "Hello World!"
belong to the typeString
or classString
? Or maybe both?
作为 Java 的新手,我对class和type的概念感到困惑。例如,对象应该"Hello World!"
属于类型String
还是类String
?或者两者都有?
采纳答案by Brandon
A class is a type. An interface is a type. A primitive is a type. An array is a type.
一个类是一种类型。接口是一种类型。原语是一种类型。数组是一种类型。
Therefore, every type is also either a class (including an enum constant), an interface, a primitive, or an array.
因此,每个类型也是一个类(包括一个枚举常量)、一个接口、一个基元或一个数组。
There are two distinct categories of types: primitive types and reference types:
有两种不同的类型:基本类型和引用类型:
- A variable of primitive type always holds a primitive value of that same type. Such a value can only be changed by assignment operations on that variable.
- A variable of reference type always holds the value of a reference to an object. All objects, including arrays, support the methods of class
Object
. The reference types are class types (including enum types), interface types, and array types.
- 原始类型的变量始终保存相同类型的原始值。这样的值只能通过对该变量的赋值操作来更改。
- 引用类型的变量始终保存对对象的引用的值。所有对象,包括数组,都支持 class 的方法
Object
。引用类型是类类型(包括枚举类型)、接口类型和数组类型。
Every piece of data has a type which defines its structure, namely how much memory it takes up, how it is laid out, and more importantly, how you can interact with it.
每条数据都有一个类型,它定义了它的结构,即它占用了多少内存,它是如何布局的,更重要的是,你可以如何与它交互。
Examples of primitive types:
原始类型的例子:
int
float
char
boolean
int
float
char
boolean
Examples of class types:
类类型示例:
Examples of interface types:
接口类型示例:
Examples of array types:
数组类型示例:
int[]
String[]
Integer[][][]
int[]
String[]
Integer[][][]
Basically, anything that you can refer to as a variable has a type, and classes are a kind of a type.
基本上,任何可以称为变量的东西都有一个类型,而类是一种类型。
More info here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html
更多信息:http: //docs.oracle.com/javase/specs/jls/se8/html/jls-4.html
回答by Lee Daniel Crocker
"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.
“类型”是更具包容性的类别。Java 中的变量可以有三种类型:8 种“原始”类型,如 int 和 float、接口和类。值(相对于变量)可以是原始的或类的实例。
回答by Satya
"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)
例如:"hello world" 是 String --> "hello world" 是 String 类型(与 int .. 不同,String 不是前置数据,所以我们可以说“hello world”是字符串类类型)
10 is a int --> 10 is a integer data type.
10 是一个 int --> 10 是一个整数数据类型。
回答by Number945
TLDR- Class is one of the Type in Java.
TLDR- 类是 Java 中的类型之一。
Note - To fully understand the answer, you must have a little idea about generics in Java.
注意 - 要完全理解答案,您必须对 Java 中的泛型有所了解。
To understand the difference let us first understand what a Typeis in Java.
要了解差异,让我们首先了解Java 中的Type是什么。
According to JLS SE 10,
根据JLS SE 10,
There are two kinds of typesin the Java programming language: primitive types(§4.2) and reference types(§4.3).
What is primitive Type ?
什么是原始类型?
a) The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char,whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).
b) The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.
c) The booleantype has exactly two values: true and false.
a) 整数类型为byte、short、int 和 long,其值分别为 8 位、16 位、32 位和 64 位有符号补码整数,以及char,其值为 16 位表示 UTF-16 代码单元的无符号整数(第 3.1 节)。
b) 浮点类型是float,其值包括 32 位 IEEE 754 浮点数,和double,其值包括 64 位 IEEE 754 浮点数。
c) boolean类型正好有两个值:true 和 false。
Now , let us come to what is reference type ?
现在,让我们来看看什么是引用类型?
There are four kinds of reference types: class types(§8.1), interface types(§9.1), type variables(§4.4), and array types(§10.1).
有四种引用类型:类类型(第8.1 节)、接口类型(第9.1 节)、类型变量(第4.4 节)和数组类型(第10.1 节)。
Let us discuss one by one.
让我们一一讨论。
If you see how in JLS , Classis defined like this :
如果您在 JLS 中看到如何,类的定义如下:
A class declarationspecifies 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.
你看[TypeParameters]
,这表明类类型也包括那些泛型类。
class Example<T>{
}
The class type will be called Example
类类型将被称为 Example
In short , a class type covers our enums , our regular (non generic) classes like String
etc and our generic classes too.
简而言之,类类型涵盖了我们的枚举、我们的常规(非泛型)类(如String
etc)以及我们的泛型类。
Similarly , I hope interface and array types is also clear. By array Type we mean like int[]
, String[]
etc.
同样,我希望接口和数组类型也清楚。通过数组类型,我们的意思是喜欢int[]
,String[]
等
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.
让我们通过下面 JLS 中的例子来理解吧。
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)
您会看到方法参数中的对象的类型为T
。是的,这T
是类型变量,是/可以用作参考。是的。(无法理解这个奇怪的例子 - 了解什么是 Java 中的泛型方法)
This completes the answer.
这就完成了答案。