What does the `new` keyword do

后端 未结 9 1879
情深已故
情深已故 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:04

    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. The second is simply shorthand for the first, much in the way that C has a similar shorthand for stack allocated integer arrays.

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

    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};
    
    0 讨论(0)
  • 2020-12-01 10:12

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

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

    The following two code snippets are equals in compiling level. I write a Demo class like:

    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:

    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}; and long results[] = {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.

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

    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.

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

    The 'new' keyword creates an array. 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. With a String it will contain the value 'null'. In every single space.

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