`new`关键字有什么作用 [英] What does the `new` keyword do

查看:96
本文介绍了`new`关键字有什么作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在线学习Java教程,尝试学习该语言,并且在使用数组的两种语义之间反弹.

I'm following a Java tutorial online, trying to learn the language, and it's bouncing between two semantics for using arrays.

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

和:

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

该教程从未真正提及过为什么它会在两者之间来回切换,所以我在该主题上进行了一些搜索.我目前的理解是new运算符正在创建"longs数组"类型的对象.我不理解的是为什么我想要那个,那有什么影响?

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. What I do not understand is why do I want that, and what are the ramifications of that?

  • 是否存在某些特定于数组"的方法,除非它是数组对象",否则这些方法对数组不起作用?
  • 我可以用普通数组处理数组对象"吗?
  • Java VM是否必须清理通常不需要执行的用new运算符初始化的对象?
  • 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?

我来自C语言,所以我的Java术语在这里可能不正确,因此请澄清是否无法理解.

I'm coming from C, so my Java terminology, may not be correct here, so please ask for clarification if something's not understandable.

推荐答案

在Java中,所有数组和对象都分配在堆上,因此从某种意义上讲,所有数组都是数组对象".在Java中,分配给堆栈的唯一内容是对象引用和原语.其他所有内容都是在堆中定义和分配的对象,包括数组,无论您使用哪种语法对其进行声明. (您的两个示例在最终结果中是等效的,请参见 JLS§ 10.3 及其链接的部分,详细介绍了如何实际分配和分配每个人.)

In Java, all arrays and objects are allocated on the heap, so in a sense, all arrays are "array objects". The only things that are ever allocated on the stack in Java are object references and primitives. 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.)

这与C/C ++相反,在C/C ++中,您可以显式控制堆栈和堆的分配.

This is contrary to C/C++, where you have explicit control over stack and heap allocation.

请注意,在短期对象分配/取消分配方面,Java的速度非常快.它具有基于世代的垃圾收集器,因此非常高效.因此,回答您的问题:

Note that Java is very fast when it comes to short-term object allocation/deallocation. 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?

没有像数组这样的东西不是对象,所以没有.但是,有 不适用于原始数组.接受Object[]的方法必须先将其转换为Long[]才能接受long[].这是由于Java 5及更高版本中自动装箱的一些实现细节.

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[]. This is due to some implementation details of autoboxing in Java 5 and up.

Java VM是否必须清理通常不需要执行的用new运算符初始化的对象?

Does the Java VM have to do clean up on objects initialized with the new operator that it wouldn't normally have to do?

new分配的任何东西最终都必须被垃圾回收,所以就做任何事情通常不会做?否.但是请注意,在C/C ++中,使用malloc/new分配数组意味着您还必须free/delete []对其进行分配,这是您不需要的事情必须用Java来做,因为它将为您回收数组.

Anything allocated with new must eventually be garbage collected, so in terms of doing anything it wouldn't normally do? 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.

请注意,如果long[]是在方法中声明的,并且您从未将其存储在方法之外的引用中,则它将在方法调用结束时自动标记为垃圾回收.垃圾收集器将等待回收其空间,直到需要它为止,但是您不必自己通过delete [](或delete和对象的析构函数)进行任何回收.

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. 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).

如所承诺的那样:

  • How Garbage Collection works in Java
  • Oracle Sun white-paper on garbage collection

这篇关于`new`关键字有什么作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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