类和类型之间的差异 [英] Difference between class and type
问题描述
作为Java的新手,我对类和类型的概念感到困惑。
例如,如果对象Hello World!
属于类型 String
或 class String
?或者两者都有?
类是一种类型。接口是一种类型。原语是一种类型。数组是一个类型。
因此,每个类型也是一个类,一个接口,一个基本类型或一个数组。
有两种不同的类型:原始类型和引用类型:
- 相同类型的原始值。这样的值只能通过对该变量的赋值操作更改。
- 引用类型的变量始终保存对对象的引用的值。所有对象,包括数组,都支持
Object
类的方法。引用类型是类类型,接口类型和数组类型。
每个数据都有一个类型,它定义了它的结构,即它占用了多少内存,如何布局,
原始类型的示例:
-
int
-
float
-
boolean
/ ol>
类类型示例:
接口类型示例:
数组类型示例:
-
int []
-
String []
-
Integer [] [] []
基本上,您可以引用为变量的任何内容都具有类型,类是一种类型。
更多信息: http://docs.oracle.com/ javase / specs / jls / se8 / html / jls-4.html
Being new to Java, I'm confused between the concepts of class and type. For example, should the object
"Hello World!"
belong to the typeString
or classString
? Or maybe both?解决方案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, 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, interface types, and array types.
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
Examples of class types:
Examples of interface types:
Examples of array types:
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
这篇关于类和类型之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
char