What does the `new` keyword do

后端 未结 9 1880
情深已故
情深已故 2020-12-01 09:41

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]         


        
相关标签:
9条回答
  • 2020-12-01 10:25

    From 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). All methods of class Object may be invoked on an array.

    From 10.3. Array Creation

    An array is created by an array creation expression (§15.10) or an array initializer (§10.6)

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

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

    Both are initialize the array the difference is second one initializes with some values.

    0 讨论(0)
  • 2020-12-01 10:26

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

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

    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?

    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.

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

    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.

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

    Edit: some references as promised:

    • How Garbage Collection works in Java
    • Oracle Sun white-paper on garbage collection
    0 讨论(0)
  • 2020-12-01 10:30

    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.

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

    0 讨论(0)
提交回复
热议问题