简体   繁体   English

为什么Java原始数据类型不称为Java数据类型?

[英]Why aren't Java Primitive Data Types called java data types?

我有一个问题,为什么Java原语数据类型不仅仅称为“ Java数据类型”或类似的东西?

Because Java has more data types than just primitives . 因为Java不仅具有原始类型,还具有更多的数据类型。 The primitive data types are: 基本数据类型为:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

A data type that is a non-primitive is a reference data type, which are references to objects . 非基本数据类型是引用数据类型,是对object的引用。

Some examples are: 一些例子是:

  • String
  • Integer
  • ArrayList
  • Random
  • JFrame

Here is a simple example of the difference between the two types: 这是两种类型之间差异的简单示例:

int i1 = 10;
Integer i2 = Integer.valueOf(10);

int i1 is a variable of the primitive data type int , with the primitive int value of 10. int i1是原始数据类型int的变量,原始int值为10。

Integer i2 is a variable with a reference data type of Integer , referencing an Integer object which contains the value 10 . Integer i2是一个变量,其引用数据类型为Integer ,引用一个包含值10Integer对象。

替代文字

区分它们和对象

Because there are two categories of types in Java. 因为Java中有两类类型。

From the Java Language Specification, CHAPTER 4: Types, Values, and Variables : 根据Java语言规范的第4章:类型,值和变量

The types of the Java programming language are divided into two categories: primitive types and reference types. Java编程语言的类型分为两类:基本类型和引用类型。 The primitive types (§4.2) are the boolean type and the numeric types. 基本类型(第4.2节)boolean类型和数字类型。 The numeric types are the integral types byte , short , int , long , and char , and the floating-point types float and double . 数值类型是整数类型byteshortintlongchar ,浮点类型floatdouble The reference types (§4.3) are class types, interface types, and array types. 引用类型(第4.3节)是类类型,接口类型和数组类型。 There is also a special null type. 还有一个特殊的空类型。 An object (§4.3.1) is a dynamically created instance of a class type or a dynamically created array. 对象(第4.3.1节)是动态创建的类类型的实例或动态创建的数组。 The values of a reference type are references to objects. 引用类型的值是对对象的引用。 All objects, including arrays, support the methods of class Object (§4.3.2) . 所有对象(包括数组)都支持Object(第4.3.2节)的方法 String literals are represented by String objects (§4.3.3) . 字符串文字由String对象(第4.3.3节)表示

To understand why, I think you need to look at programming languages other than Java. 要理解原因,我认为您需要查看Java以外的其他编程语言。 For example: 例如:

  • In C++ there are, primitive data types ( int , double , etc), constructed data types ( struct , etc) and object / reference types. 在C ++中,有原始数据类型( intdouble等),构造数据类型( struct等)和对象/引用类型。

  • In Ada there are primitive data types, and other data types that are derived from the primitive types; 在Ada中,存在原始数据类型,以及从原始类型派生的其他数据类型。 eg range types. 例如范围类型。

So, my understanding is that Java data types are described as "primitive data types" to put them into the context of other languages. 因此,我的理解是将Java数据类型描述为“原始数据类型”,以将其置于其他语言的上下文中。 They are "data types" in the sense that they have no object identity, and they are "primitive" in the sense that the specific types are defined by (and fundamental to) the Java language. 从没有对象身份的意义上来说,它们是“数据类型”,从特定的类型由Java语言定义(并且是Java语言的基础)的意义上来说,它们是“原始的”。

对象也是变量,因此术语“原始”用来区分那些类型。

区分对象数据类型。

Because reference types can also be considered data types. 因为引用类型也可以视为数据类型。 Primitives are considered value types. 基元被视为值类型。 Both can be considered a data type. 两者都可以视为数据类型。

Non primitive types are called java reference types and they have name starting with capital letter. 非原始类型称为Java引用类型,它们的名称以大写字母开头。 Eg: Integer, Float etc. For non primitives we can create the instances. 例如:Integer,Float等。对于非基本类型,我们可以创建实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM