类和类型的区别 [英] Difference between class and type
问题描述
作为 Java 新手,我对 class 和 type 的概念感到困惑.例如,对象 "Hello World!"
应该属于 type String
还是 class String代码>?或者两者都有?
Being new to Java, I'm confused between the concepts of class and type.
For example, should the object "Hello World!"
belong to the type String
or class String
? 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 (including an enum constant), an interface, a primitive, or an array.
有两种截然不同的类型:原始类型和引用类型:
There are two distinct categories of types: primitive types and reference types:
- 原始类型的变量总是保存相同类型的原始值.这样的值只能通过对该变量的赋值操作来更改.
- 引用类型的变量始终保存对对象的引用的值.所有对象,包括数组,都支持
Object
类的方法.引用类型是类类型(包括枚举类型)、接口类型和数组类型.
- 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.
每条数据都有一个类型,它定义了它的结构,即它占用了多少内存,它是如何布局的,更重要的是,你可以如何与之交互.
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.
原始类型示例:
int
浮动
char
布尔值
类类型示例:
接口类型示例:
数组类型示例:
int[]
String[]
整数[][][]
基本上,任何可以称为变量的东西都有一个类型,而类是一种类型.
Basically, anything that you can refer to as a variable has a type, and classes are a kind of a type.
此处的更多信息:http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html
这篇关于类和类型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!