Java arrays, Wat!?

There is a few things you can do with arrays which are surprising.
 
 
 
 
 
 
 
 
 
 

Is it an array or not?

Serializable array = new Serializable[9];

Is array an array or a scalar? Well its a scalar which points to an array. Just like 

Object o = new Object[9];

You can assign an array to an object because it is also an object.  However, arrays are also Serializable so you can assign them to Serializable.

Where did my [] go?

The [] can appear in surprising places.  This compiles for backward comparability reasons.

public static int method(int[]... args)[] {
    return args[0];
}

And the types here are; args is an int[][] and the return type is int[].  Did you notice the [] after the method declaration!  This is not part of the JLS and the OpenJDK allows this due to backward compatibility reasons.

What is the difference between int[] array and int array[] ?

There is a difference in what comes after it.

int[] array, x[];

and

int array[], y[];

In these cases; x is an int[][] but y is only an int[].

What happens if an array initialization is too large?

Say I initialize an array like this

public static final int[] VALUES = {
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
            /* many, many lines deleted */
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
};

The errors message is;

Error:(6, 31) java: code too large

This seems a little strange.  It doesn’t complain the array is too large.  In fact if I have more static fields or use larger constants, it will fail for a smaller array.
 
This happens because arrays are initialised in byte code.  There byte code creates the array and initialises each value, one at a time.  This results in a lot of code for large arrays which would be such a problem if there wasn’t a limit in the size of a method. i.e. 65535 bytes.  The compiler generates one and only one method for a constructor or static initialization so this limits how many enums you can have and how large your initialised arrays can be.

Reference: Java arrays, Wat!? from our JCG partner Peter Lawrey at the Vanilla Java blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


six − 2 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books