Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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

like image 470
Mike Avatar asked Dec 18 '12 04:12

Mike


5 Answers

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
like image 94
Brian Avatar answered Oct 19 '22 18:10

Brian


The new keyword in Java creates a new object. 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?

No. There is no difference between the objects created in different ways as far as the JVM is concerned.

like image 43
Stephen C Avatar answered Oct 19 '22 17:10

Stephen C


The two are identical in terms of the behavior of the array created. All arrays are technically objects in 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};
like image 24
ApproachingDarknessFish Avatar answered Oct 19 '22 17:10

ApproachingDarknessFish


The both are same. The second option is doing implicit creation of array object, it is just for user convenience.

like image 45
vishal_aim Avatar answered Oct 19 '22 18:10

vishal_aim


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.

like image 42
Amit Deshpande Avatar answered Oct 19 '22 17:10

Amit Deshpande