JDK-4375522 : (coll) Provide an EMPTY_ARRAY like you did for Collections.EMPTY_SET, etc.
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.3.0
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2000-10-02
  • Updated: 2012-10-08
  • Resolved: 2007-06-30
Related Reports
Relates :  
Description

Name: rmT116609			Date: 10/02/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

I believe an EMPTY_ARRAY singleton would be even more useful than the already-
existing EMPTY_SET, EMPTY_MAP, or EMPTY_LIST singletons. The declaration I am
thinking of is:
    public static final Object[] EMPTY_ARRAY = { };

Here are some core classes in JDK that already use their own copies of
EMPTY_ARRAY.

javax.swing.filechooser.WindowsFileSystemView
javax.swing.filechooser.UnixFileSystemView
javax.swing.filechooser.GenericFileSystemView
javax.swing.JComboBox
javax.swing.JList
javax.swing.event.EventListenerList
javax.swing.tree.DefaultMutableTreeNode
(Review ID: 109229) 
======================================================================

Comments
EVALUATION Yes, the JDK is littered with static final empty arrays, but they are generally of different types EMPTY_STRING_ARRAY EMPTY_SHORT_ARRAY EMPTY_ANNOTATION_ARRAY Using a single new Object[0] for all of these is almost acceptable, but there is a small loss of type information, that becomes visible when, for example, creating a wider copy using Arrays.copyOf. JVMs are getting better every release at optimizing local allocations like new Foo[0]. Probably we should just let the JVM do its job and not be afraid of simply returning new Object[0] from every empty collection's toArray method. Pre-allocating such objects always presents a small startup and footprint cost and a small cost every time a full GC occurs. With the lack of votes and apparent agreement in the community that this RFE should not be implemented for Java, it's time to close this as Not a Defect.
30-06-2007

EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=20529
28-06-2007

EVALUATION The problem is that you want one for every possible type: an empty int array an empty Object array, an empty String array, etc. I suppose the language could provide such a facility, but I'm not quite how it would look. ###@###.### 2002-04-26
26-04-2002