7 Replies Latest reply: Apr 28, 2008 12:32 PM by 807591 RSS

    Zero Length Array Initialization

    807591
      Are all these the same, and if so, which is the the preferred one to use?

           private static final String[] ZERO_LENGTH_STRING_ARRAY_1 = new String[0];
           private static final String[] ZERO_LENGTH_STRING_ARRAY_2 = new String[] {};
           private static final String[] ZERO_LENGTH_STRING_ARRAY_3 = {};

      I think that the first is the most common way of initializing a zero-length array, but I am interested in
      other peoples opinions on this.

      Thanks in advance
        • 1. Re: Zero Length Array Initialization
          807591
          They are all functionally the same. My personal preference is the first option since that most clearly defines the array as zero length, but frankly I wouldn't think twice about using any of the others.
          • 2. Re: Zero Length Array Initialization
            807591
            well, if the code compiles just fine, then the second concern is readability of ur code. The first one is certainly nicer than the other two and more understandable.
            • 3. Re: Zero Length Array Initialization
              807591
              What exactly is the point of this static array with no length?
              • 4. Re: Zero Length Array Initialization
                807591
                Read the second part of the following topic (Using Zero-Length Arrays) to find out some reasons to why they are useful:

                http://java.sun.com/developer/TechTips/txtarchive/2002/Sept02_GlenM.txt

                PS Thanks for the other replies guys
                • 5. Re: Zero Length Array Initialization
                  807591
                  I believe that cotton.m's point was: "There's no good reason to statically define an empty array. Just create them as you need them."

                  I'm pretty sure that the VM only keeps one instance of a zero-length array of any particular type around anyway, so you can create them with new and they're still essentially static.
                  • 6. Re: Zero Length Array Initialization
                    807591
                    es5f2000 wrote:
                    I believe that cotton.m's point was: "There's no good reason to statically define an empty array. Just create them as you need them."

                    I'm pretty sure that the VM only keeps one instance of a zero-length array of any particular type around anyway, so you can create them with new and they're still essentially static.
                    public class ZeroLengthArrayCreationTest {
                        public static void main(String[] args) {
                            Object[] array0 = new Object[0];
                            Object[] array1 = new Object[1];
                            System.out.format("array0 == array1: %b%n", array0 == array1);
                        }
                    }
                    Not that this invalidates "Just create them as you need them". Creating a single zero-length array because they are immutable smells like a micro-efficiency worry to me.
                    • 7. Re: Zero Length Array Initialization
                      807591
                      Nuts! I was sure I read that somewhere. Oh, well. :-(