JDK-6389769 : Shorter Syntax for Common Operations
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-02-23
  • Updated: 2010-04-04
  • Resolved: 2009-01-27
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Java is quite verbose, but this verbosity can be eliminated quite easily:

0. Introduce the var keyword

EG instead of:

ArrayList< String > strings = new ArrayList< String >();

Allow:

var strings = new ArrayList< String >(); // also see 1 below

Similarly allow:

final strings = new ArrayList< String >();



1. Make the new keyword optional.

EG instead of:

String s = new String();

Allow:

String s = String();

Combining 0 and 1 allow:

var strings = ArrayList< String >();
final strings = ArrayList< String >();



2. For methods and classes make the curly braces optional if there is only one statement. IE like for, if, etc.

EG instead of:

class IntWrapper {
    public int i;
}

Allow:

class IntWrapper public int i;

And instead of:

public String toString() {
    return "Hello";
}

Allow:

public String toString() return "Hello";



3. If a class needs to only implement a single method that is defined in either an abstract class of an interface, then allow the method qualifiers, return type, name, and the arguments types to be omitted.

EG instead of:

class MyAction implements ActionListener {
    public void actionPerformed( ActionEvent notUsed ) {
        textArea.append( textField.getText() );
    }
}

Allow (only using simplification 3, see below for an example using rules 1 to 3):

class MyAction implements ActionListener {
    ( notUsed ) {
        textArea.append( textField.getText() );
    }
}

Now an example of combining all rules 1 to 3, instead of:

textField.addActionListner( new ActionListener() {
    public void actionPerformed( ActionEvent notUsed ) {
        textArea.append( textField.getText() );
    }
});

Allow (using all 3 simplifications):

textField.addActionListner( ActionListener() ( notUsed ) textArea.append( textField.getText() ); );

These rules would reduce verbosity, be easy to implement and would be backwards compatible.

JUSTIFICATION :
The syntax in Java is quite long and this has led to a number of related languages that have shorter syntax but are otherwise similar, e.g. Scala, Groovy, and Nice. Also there are plenty of requests for features that amount to little more than short syntax, e.g. closures (5014235) and functors (5061325) are closely related to inner classes.

All of these issues could be addressed with the shorter syntax suggested and this would be a relatively minor change to Java yet bring many of the benifits offered by Scala, Groovy, Nice, closures, and functors.


CUSTOMER SUBMITTED WORKAROUND :
Whilst modern IDEs do reduce typing; some people prefer a text editor, some environments like embedded can't run IDEs, and there is always the need to present textual examples in papers, tutorials, books, etc.

Comments
EVALUATION These proposals have little to recommend them. Shortening parameterized type instantiation is better done on the RHS, not the LHS, as there are many drawbacks to local type inference ("var"). Dropping "new" and {} is just pointless. Shortening anonymous instance creation is not a bad idea, but thoroughly covered elsewhere, e.g. in CICE.
17-11-2006