他们在 Java 中说“一切都是对象”。真的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11844012/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:36:52  来源:igfitidea点击:

They say in java "every thing is an object". Is that true?

javaoop

提问by zizoujab

When I type

当我打字

int a = 5;

Is aan object ?

a对象吗?

Can anyone explain to me how in java every thing is an object?

任何人都可以向我解释在 Java 中每件事都是一个对象吗?

回答by Peter Lawrey

Every object is a java.lang.Object(NOTE:java.lang.Objecthas no super class. ;) )

每个对象都是一个java.lang.Object(注意:java.lang.Object没有超类。;))

However, there are many things which are not Objects.

然而,有很多东西不是对象。

  • primitives and references.
  • fields (the fields themselves not the contents)
  • local variables and parameters.
  • generic classes (that may change in Java 8)
  • methods (that will change in Java 8)
  • blocks of code (that will change in Java 8)
  • 原语和引用。
  • 字段(字段本身不是内容)
  • 局部变量和参数。
  • 泛型类(在 Java 8 中可能会改变)
  • 方法(这将在 Java 8 中改变)
  • 代码块(将在 Java 8 中更改)


Having a block of code as an object is one of the most exciting features in Java 8. The following examples will all be Closures and therefore objects.

将代码块作为对象是 Java 8 中最令人兴奋的特性之一。以下示例都将是闭包,因此都是对象。

x => x + 1
(x) => x + 1
(int x) => x + 1
(int x, int y) => x + y
(x, y) => x + y
(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
() => { System.out.println("I am a Runnable"); }

e.g. the block of code here will be passed as a RunnableObject

例如,这里的代码块将作为对象传递Runnable

new Thread(() => { System.out.println("I am a Runnable"); }).start();

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

回答by Adam Arold

This is nottrue in java. For example the intis actually a primitive. In java everything is an Objectwhat extends Object. Everything else is not.

这在java中不是真的。例如,int实际上是一个原语。在java中,一切都是Object扩展的Object。一切是不是

So, for example you can't manipulate namespaces (packages in java terms) like objects but in Erlang or Clojure you can.

因此,例如,您不能像对象一样操作名称空间(Java 术语中的包),但在 Erlang 或 Clojure 中您可以。

Although java provides an autoboxing feature which can translate primitives to objects. In your case if you say

尽管 java 提供了自动装箱功能,可以将原语转换为对象。在你的情况下,如果你说

Integer a = 5;

java boxes the 5 into the Integerreference. If you want to read about autoboxing click here: autoboxing docs. Objects in java: Objects

java 将 5 装入Integer引用。如果您想阅读有关自动装箱的信息,请单击此处:自动装箱文档。Java 中的对象对象

If you are looking for a language where everythingis an object technically, you can try out Common Lisp for example. In Lisp even the T(stands for boolean true) is an object.

如果您正在寻找一种从技术上讲一切都是对象的语言,您可以尝试使用 Common Lisp。在 Lisp 中,甚至T(代表 boolean true) 也是一个对象。

回答by Reimeus

ahere is a primitive. Java has both primitives and objects.

a这是一个原语。Java 有原语和对象。

回答by amicngh

No, this is not an Object.Java is not purely Object Oriented Language because of primitivesandstatic. To make primitive variable as Object java has introduced wrapper classed like Integer, Booleanetc.

,这不是 Object.Java 不是纯粹的面向对象语言,因为primitivesstatic。为了将原始变量作为 Object java 引入了IntegerBoolean等分类的包装器。

回答by dasblinkenlight

Although ais not an object but a value of a primitive type, Java makes every attempt to hide this fact from you. Starting with Java 5, primitives are automatically converted to their corresponding object wrappers when necessary, so that you could store them in containers, pass them to methods requiring objects, and so on.

尽管a它不是对象而是原始类型的值,但 Java 会尽一切努力向您隐藏这一事实。从 Java 5 开始,必要时基元会自动转换为其相应的对象包装器,以便您可以将它们存储在容器中,将它们传递给需要对象的方法,等等。

回答by Romain

Not everything in Java is an object. Some values are of primitive types, to name a few: int, float, double, byte, char, ... There are ways to wrap these primitive types into Objects (Java can also do this automatically for you).

并非 Java 中的一切都是对象。一些值属于原始类型,仅举几例:int, float, double, byte, char, ... 有多种方法可以将这些原始类型包装到对象中(Java 也可以自动为您执行此操作)。

Now, one could say more accurately, "In java, everything is [defined] in an Object", as a way to highlight that in Java, you cannot define a function or sub that isn't either a class or instance method, like you'd be able to do in C++for example.

现在,人们可以更准确地说,“在 Java 中,一切都在对象中[定义]”,作为一种强调在 Java 中,您不能定义既不是类也不是实例方法的函数或子,例如例如,你可以做到C++

回答by kgautron

In java you have the primitive types (int, boolean, char, byte...), and everything else extends the Objectclass and therefore is an object.

在 Java 中,您有原始类型(int、boolean、char、byte...),其他所有内容都扩展了Object类,因此是一个对象。

But the everything is an objectthing mostly means that you can't have code outside of an object class. You can' make script files for examples, it has to be wrapped in a class.

但是一切都是对象的东西主要意味着您不能在对象类之外拥有代码。您不能制作示例脚本文件,它必须包含在一个类中。

回答by esej

They are wrong. a is not an Object.

他们错了。a 不是对象。

回答by npe

I think this comes from the "early days" when Java was often compared to C, and its structural nature. The statement itself, however, is not true.

我认为这来自 Java 经常与 C 相比较的“早期”及其结构性质。然而,这种说法本身并不正确。

回答by Edd

Not everything in Java is an Object. There are also the following primitivetypes that can be used within Objects (Definitions taken from the Oracle Tutorials:

并非 Java 中的一切都是对象。还有以下基本类型可以在对象中使用(定义取自Oracle 教程

  • byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

  • short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

  • int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

  • long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

  • float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

  • boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

  • byte:字节数据类型是一个 8 位有符号二进制补码整数。它的最小值为 -128,最大值为 127(含)。字节数据类型可用于在大型数组中节省内存,其中内存节省实际上很重要。它们也可以代替 int 使用它们的限制有助于澄清您的代码;变量范围有限的事实可以作为一种文档形式。

  • short:short 数据类型是一个 16 位有符号二进制补码整数。它的最小值为 -32,768,最大值为 32,767(含)。与字节一样,适用相同的准则:在内存节省实际上很重要的情况下,您可以使用 short 来节省大型数组中的内存。

  • int:int 数据类型是一个 32 位有符号二进制补码整数。它的最小值为 -2,147,483,648,最大值为 2,147,483,647(含)。对于整数值,此数据类型通常是默认选择,除非有理由(如上述)选择其他类型。对于您的程序将使用的数字,此数据类型很可能足够大,但如果您需要更大范围的值,请改用 long。

  • long:long 数据类型是一个 64 位有符号二进制补码整数。它的最小值为 -9,223,372,036,854,775,808,最大值为 9,223,372,036,854,775,807(含)。当您需要比 int 提供的值范围更广的值时,请使用此数据类型。

  • float:float 数据类型是单精度 32 位 IEEE 754 浮点数。它的值范围超出了本讨论的范围,但在 Java 语言规范的浮点类型、格式和值部分中进行了指定。与 byte 和 short 的建议一样,如果需要在大型浮点数数组中节省内存,请使用浮点数(而不是双精度数)。此数据类型不应用于精确值,例如货币。为此,您需要改用 java.math.BigDecimal 类。数字和字符串涵盖了 Java 平台提供的 BigDecimal 和其他有用的类。

  • double:double 数据类型是双精度 64 位 IEEE 754 浮点数。它的值范围超出了本讨论的范围,但在 Java 语言规范的浮点类型、格式和值部分中进行了指定。对于十进制值,此数据类型通常是默认选择。如上所述,这种数据类型不应用于精确值,例如货币。

  • boolean:boolean 数据类型只有两个可能的值:true 和 false。将此数据类型用于跟踪真/假条件的简单标志。这种数据类型代表一位信息,但它的“大小”并不是精确定义的。

  • char:char 数据类型是单个 16 位 Unicode 字符。它的最小值为 '\u0000'(或 0),最大值为 '\uffff'(或 65,535)。

Most other stuff in Java isan object (It inherits from the Objectclass), and the main()method is run from an instance of a class. In order to allow primitives to be used in a predominantly object-based system, Java provides wrapper classes that areobjects representing primitive values (e.g. The Integerclass represents the same type of data as an int). Java also does something called autoboxingwhere it automatically wraps a primitive type in its object-wrapper, when, for example, you want to store an intin an ArrayList<Integer>. Likewise, you can do something like int x = intArrayList.get(0);and Java will unbox the Integerstored in the Array. Note that these autoboxing operations are not completely free, as they have a performance cost associated with them, so be aware of this if performance really matters to your system.

Java的大多数其他东西一个对象(它继承自Object类),并且该main()方法是从类的实例运行的。为了允许在主要基于对象的系统中使用原语,Java 提供了包装类,这些表示原语值的对象(例如,Integer该类表示与 相同类型的数据int)。Java 也做了一些称为自动装箱的事情,它会自动将原始类型包装在它的对象包装器中,例如,当您想将 an 存储intArrayList<Integer>. 同样,您可以执行类似的操作int x = intArrayList.get(0);,Java 将取消装箱Integer存储在数组中。请注意,这些自动装箱操作并不是完全免费的,因为它们会产生与之相关的性能成本,因此如果性能对您的系统真的很重要,请注意这一点。