Dynamic arrays in JDK8

I just started messing around with Java, as a former C/C++ average programmer. I'm intrigued with the way arrays are declared in JDK8. I'm following a book with says to me to declare the array as a object as following.

    int[] exampleArray = new int[10];

I totally agree in using the array as an object. But on my IDE (Netbeans 8), I get a notification that says that "field example array can be final". When I remove the new int[10] part I get normal compilation and no notifications. Does that mean that arrays in JDK are dynamically allocated? I didn't try to iterate through the array yet.

From what I found on Google I think that arrays are implemented as linked lists right?

If not, please someone tell me the proper use of arrays in Java.

Thanks


About arrays

From the Java Language Specification (version 8), page 329:

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.

The key here is Object . Therefore, your variable exampleArray could potentially be null . Public methods (not counting the public static void main) that accept arrays and varargs (also known as a "variable arity parameter"; is made into an array by the runtime) as parameters cannot be assured it is always safe to call exampleArray.length for example (because a NullPointerException could be thrown at you). Also, for every type (class and interface) you write, there will be a corresponding class object to represent the array (for example Foo[].class and Bar[].class ).

How an array is implemented would be over specification and the JLS (Java Language Specification) does not address that issue, not at least what I could find after digging around a bit.

Also, when assigning an array to a variable, you are free to use a shorthand like so:

int[] exampleArray = { 1, 2, 3 };

Do know that where you put your brackets doesn't matter. For example, here is a few ways you could declare a two dimensional array:

int[][] multi = {{}};

int[] multi[];

int multi[][];

As you are already akin with, the first example is the most popular and the JLS (Java Language Specification) explicitly advice against mixed notation (page 332).

Another point worth mentioning is that the length of an array can never change.

Final variables

The final keyword has a few different meanings dependent on how you use it, in what context. First of all, constants exists only as source code literals in many languages. A final field in Java can be modified if you use a low-level API, as can const pointers be casted in C++ to non const pointers. But, declaring a variable in Java as final will tell the compiler and most of the runtime too that this variable will be written to only once. The way I see it, using the final keyword in Java has three reasons.

  • Your source code reads better. You will instantly be "guaranteed" as a reader of the source code that the variable.. will be written to only once. If he appears as a local variable in a method, then he's most likely initialized too right on the spot and you may feel at ease knowing that the variable will not change value again. If you see a static field or a an instance field marked final, you can be most assured the field will be explicitly initialized by the programmer somewhere before the object has been completely constructed (most likely in a constructor).

  • Putting final in front of the variable will make your coding life a bit easier by caching programmer errors. Say for example that you have five million cases in a switch statement that each one of them will give a certain value to a non-final variable already declared and which is supposed to be returned to the caller sometime later after the switch. If you forget a break anywhere, then your code still compiles. But if you had declared the variable as final, your entire screen would instantly flash red and the compiler would caught that one case after the case that didn't brake and light up the fact that he just tried to reassign a new value to your precious field.

  • The runtime can make some cool optimizations based on the fact that the variable isn't supposed to change. For example, all final primitives is automatically thread safe.


  • Yes, arrays are dynamically allocated in Java. Try the following code:

    int size = new Scanner(System.in).nextInt();
    int[] exampleArray = new int[size];
    System.out.println(exampleArray.length);
    

    Also, I agree with Makoto's remark that the IDE's suggestion may be confusing

    链接地址: http://www.djcxy.com/p/91878.html

    上一篇: JDK8不支持JDK8(WS客户端)

    下一篇: JDK8中的动态数组