原始值可以被视为java中的对象吗? [英] Can a primitive value be considered an object in java?

查看:77
本文介绍了原始值可以被视为java中的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始使用Java时,我暗示我们有两种不同的类型:

 对象(字符串,来自类,println等)
原始值(int,double,char)

I刚刚从教授那里得到了一个考试,这个差异意味着两个选择之间的差异作为答案。当我让我的教授澄清时,他告诉我原始价值观可以是对象。



谁在这里,谁能给我任何证据呢?
证明是正式的声音,我会选择这个作为答案,并给你一些众所周知的互联网。

解决方案

< blockquote>

可以将原始值视为对象吗?


答案是否定。



JLS 声明


Java编程语言中有两种类型:原始类型(§4.2)和引用类型(§4.3)。相应地,两种数据值可以存储在变量中,作为参数传递,由方法返回,并在以下操作:原始值(§4.2)和参考值(§4.3)。


这些是原始类型和值


预定义为Java编程语言并以其保留关键字命名


参考类型和值,可以是



之一


类类型(§8),接口类型(§9),类型变量(§4.4)和数组类型(§10)。




另请注意,有一种特殊类型 null类型及其对应值 null reference


还有一个特殊的null类型,表达式的类型为null(§3.10.7, §15.8.1),没有名字。



...



空引用是null类型表达式唯一可能的值。


对于基元类型,JLS定义了一种名为拳击转换,用于将它们转换为相应的引用类型。


拳击转换将基本类型的表达式转换为引用类型的相应表达式。


因此,由于需要转换从基本类型转换为相应的引用类型,因此不能说基元可以被视为对象,反之亦然。但可以说他们是可以兑换的。


When I started out using Java, it was implied to me that we have two different types:

Objects (strings, from classes, println, etc)
primitive values (int, double, char)

I just got back an exam from a Professor where this difference would mean the difference between two choices as answers. When I asked my professor to clarify, he told me that primitive values can be objects.

Who is right here and can anyone give me any proof? Proof would be something official sounding and I would choose that as the answer as well as awarding you some proverbial internets.

解决方案

Can a primitive value be considered an object?

The answer is No.

The JLS states

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

Those are Primitive Types and Values which are

predefined by the Java programming language and named by its reserved keyword

and Reference Types and Values which can be one of

class types (§8), interface types (§9), type variables (§4.4), and array types (§10).

Note also that there is a special type which is the null type and its corresponding value the null reference

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

...

The null reference is the only possible value of an expression of null type.

For the primitive types the JLS has defined a mechanism called Boxing Conversion for converting them into the corresponding reference types.

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type.

So since there is the need for a conversion to go from a primitive type to a corresponding reference type one cannot say that primitives can be considered as objects and vice versa. But one can say they are convertible.

这篇关于原始值可以被视为java中的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆