简体   繁体   English

`new`关键字做什么

[英]What does the `new` keyword do

I'm following a Java tutorial online, trying to learn the language, and it's bouncing between two semantics for using arrays. 我正在线上学习Java教程,试图学习这门语言,并且它在使用数组的两种语义之间蹦蹦跳跳。

long results[] = new long[3];
results[0] = 1;
results[1] = 2;
results[2] = 3;

and: 和:

long results[] = {1, 2, 3};

The tutorial never really mentioned why it switched back and forth between the two so I searched a little on the topic. 教程从未真正提到为什么它在两者之间来回切换,所以我对这个主题进行了一些搜索。 My current understanding is that the new operator is creating an object of "array of longs" type. 我目前的理解是new运算符正在创建一个“longs数组”类型的对象。 What I do not understand is why do I want that, and what are the ramifications of that? 明白的是为什么我想要那个,那有什么后果?

  • Are there certain "array" specific methods that won't work on an array unless it's an "array object"? 除非它是“数组对象”,否则某些“数组”特定方法不适用于数组?
  • Is there anything that I can't do with an "array object" that I can do with a normal array? 对于我可以用普通数组做的“数组对象”,我有什么不能做的吗?
  • Does the Java VM have to do clean up on objects initialized with the new operator that it wouldn't normally have to do? Java VM是否必须清理使用new运算符初始化的对象,而这通常不需要执行?

I'm coming from C, so my Java terminology, may not be correct here, so please ask for clarification if something's not understandable. 我来自C,所以我的Java术语在这里可能不正确,所以如果某些事情不可理解,请要求澄清。

In Java, all arrays and objects are allocated on the heap, so in a sense, all arrays are "array objects". 在Java中,所有数组和对象都在堆上分配,因此从某种意义上说,所有数组都是“数组对象”。 The only things that are ever allocated on the stack in Java are object references and primitives. 在Java中堆栈上唯一分配的东西是对象引用和原语。 Everything else is an object that is defined and allocated in the heap, including arrays, regardless of which syntax you use to declare it. 其他所有内容都是在堆中定义和分配的对象,包括数组,无论您使用哪种语法来声明它。 (Your two examples are equivalent in the end result, see JLS §10.3 and its linked sections for more on how each one is actually allocated and assigned.) (您的两个示例在最终结果中是等效的,请参阅JLS§10.3及其链接部分,以获取有关如何实际分配和分配每个示例的更多信息。)

This is contrary to C/C++, where you have explicit control over stack and heap allocation. 这与C / C ++相反,在C / C ++中,您可以显式控制堆栈和堆分配。

Note that Java is very fast when it comes to short-term object allocation/deallocation. 请注意,Java在短期对象分配/释放方面非常快。 It's highly efficient because of its generation-based garbage collector. 由于其基于生成的垃圾收集器,它非常高效。 So to answer your questions: 那么回答你的问题:

Are there certain "array" specific methods that won't work on an array unless it's an "array object"? 除非它是“数组对象”,否则某些“数组”特定方法不适用于数组? Is there anything that I can't do with an "array object" that I can do with a normal array? 对于我可以用普通数组做的“数组对象”,我有什么不能做的吗?

There is no such thing as an array that's not an object, so no. 没有数组不是对象,所以没有。 There are , however, methods which won't work on primitive arrays. ,但是,这不会对原始阵列工作方式。 A method that takes an Object[] will not accept a long[] without first converting it to Long[] . 采用Object[]方法不会先接受long[]而不先将其转换为Long[] This is due to some implementation details of autoboxing in Java 5 and up. 这是由于Java 5及更高版本中的自动装箱的一些实现细节。

Does the Java VM have to do clean up on objects initialized with the new operator that it wouldn't normally have to do? Java VM是否必须清理使用new运算符初始化的对象,而这通常不需要执行?

Anything allocated with new must eventually be garbage collected, so in terms of doing anything it wouldn't normally do? 任何用new分配的东西最终都必须被垃圾收集,所以在做任何事情时通常都不会这样做? No. However, note that in C/C++, allocating an array using malloc / new means you also have to free / delete [] it, which is something you don't have to do in Java since it will reclaim the array for you. 否。但是,请注意,在C / C ++,分配使用数组malloc / new意味着你也有free / delete []它,这是你没有用Java做的,因为它会收回该阵列为你。

Note that if your long[] is declared in a method, and you never store it in a reference somewhere outside of your method, it will be marked for garbage collection automatically at the end of the method call. 请注意,如果在方法中声明了long[] ,并且从未将其存储在方法之外的某个引用中,则会在方法调用结束时自动将其标记为垃圾回收。 The garbage collector will wait to reclaim its space until it needs it, but you don't have to do any reclamation yourself via delete [] (or delete and destructors for objects). 垃圾收集器将等待回收它的空间直到它需要它,但你不必通过delete [] (或对象的delete和析构函数)自己进行任何回收。

Edit: some references as promised: 编辑:承诺的一些参考:

The new keyword in Java creates a new object. Java中的new关键字创建一个新对象。 In this case it is creating an array ... which is an object. 在这种情况下,它创建一个数组...这是一个对象。

Those two forms are equivalent. 这两种形式是等同的。 The second one is just a convenient shorthand for the first one. 第二个是第一个方便的简写。 It is syntactic sugar . 它是语法糖

Are there certain "array" specific methods that won't work on an array unless it's an "array object"? 除非它是“数组对象”,否则某些“数组”特定方法不适用于数组?

All arrays are objects. 所有数组都是对象。 Period. 期。

Is there anything that I can't do with an "array object" that I can do with a normal array? 对于我可以用普通数组做的“数组对象”,我有什么不能做的吗?

See above. 往上看。

Does the Java VM have to do clean up on objects initialized with the new operator that it wouldn't normally have to do? Java VM是否必须清理使用new运算符初始化的对象,而这通常不需要执行?

No. There is no difference between the objects created in different ways as far as the JVM is concerned. 没有。就JVM而言,以不同方式创建的对象之间没有区别。

The two are identical in terms of the behavior of the array created. 这两个在创建的数组的行为方面是相同的。 All arrays are technically objects in java; 所有数组在技术上都是java中的对象; these are just two different ways initializing them. 这些只是初始化它们的两种不同方式。 Also its possible to combine the two like so: 也可以将两者结合起来:

long[] results = new long[]{1,2,3};

The both are same. 两者都是一样的。 The second option is doing implicit creation of array object, it is just for user convenience. 第二个选项是隐式创建数组对象,它只是为了方便用户。

From JLS#Chapter 10. Arrays 来自JLS#Chapter 10. Arrays

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). 在Java编程语言中,数组是对象(§4.3.1),是动态创建的,可以分配给Object类型的变量(§4.3.2)。 All methods of class Object may be invoked on an array. 可以在数组上调用Object类的所有方法。

From 10.3. Array Creation 10.3. Array Creation 10.3. Array Creation

An array is created by an array creation expression (§15.10) or an array initializer (§10.6) 数组由数组创建表达式(第15.10节)或数组初始值设定项(第10.6节)创建

From 15.10. Array Creation Expressions 15.10. Array Creation Expressions 15.10. Array Creation Expressions

An array creation expression creates an object that is a new array whose elements are of the type specified by the PrimitiveType or ClassOrInterfaceType . 数组创建表达式创建一个对象,该对象是一个新数组,其元素的类型是PrimitiveTypeClassOrInterfaceType指定的类型。

From 10.6. Array Initializers 10.6. Array Initializers 10.6. Array Initializers

An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values. 可以在声明(第8.3节,第9.3节,第14.4节)中指定数组初始值设定项,或者作为数组创建表达式(第15.10节)的一部分,以创建数组并提供一些初始值。

Both are initialize the array the difference is second one initializes with some values. 两者都初始化数组,差异是第二个用一些值初始化。

The two ways of creation that you've shown are equivalent. 您展示的两种创作方式是等效的。 Both are "array objects" - remember, everything in Java (with the exception of the basic numeric types like int , double , and so on) are Objects. 两者都是“数组对象” - 请记住,Java中的所有内容(除了基本数字类型,如intdouble等)都是Objects。 The second is simply shorthand for the first, much in the way that C has a similar shorthand for stack allocated integer arrays. 第二个是第一个的简写,就像C具有类似的堆栈分配整数数组的简写一样。

The following two code snippets are equals in compiling level. 以下两个代码片段在编译级别中是等于的。 I write a Demo class like: 我写了一个Demo类:

public class NewArray {
    public static void main(String[] args) {
        long results[] = new long[3];
    }
}

and

public class NewArray {
    public static void main(String[] args) {
        long results[] = {0,0,0};
    }
}

output of 'javap -c NewArray' is exactly the same: 'javap -c NewArray'的输出完全相同:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_3
   1:   newarray long
   3:   astore_1
   4:   return

}

long results[] = new long[]{1,2,3}; 长期结果[] = new long [] {1,2,3}; and long results[] = {1,2,3}; 结果很长[] = {1,2,3}; are exactly the same too. 也完全一样。

So,although sometimes you are not using new key word,but the compile will regard them as equal. 所以,虽然有时候你没有使用新的关键词,但编译会认为它们是平等的。

The 'new' keyword creates an array. 'new'关键字创建一个数组。 Now, depending on what type of data the array is for the values inside of it vary. 现在,根据数组的数据类型,它内部的值会有所不同。 If you declare the array as an integer and use the 'new' keyword then it will contain the values 0 unless you change the values inside. 如果将数组声明为整数并使用'new'关键字,那么它将包含值0,除非您更改其中的值。 With a String it will contain the value 'null'. 使用String,它将包含值'null'。 In every single space. 在每一个空间。

In both cases you create an object. 在这两种情况下,您都会创建一个对

In first version: 在第一个版本中:

long results[] = new long[3];
results[0] = 1;
results[1] = 2;
results[2] = 3;

you say in first line that array size is 3. Then you put values into the array. 你在第一行说数组大小是3.然后你把值放入数组。

In second version: 在第二个版本:

long results[] = {1, 2, 3};

You create the same array and initialize it in the same line. 您创建相同的数组并在同一行中初始化它。 Java computes, that you gave 3 arguments and makes the new long[3] without Your help :) Java计算,你给了3个参数并在没有你帮助的情况下使new long[3] :)

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

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