abbreviated generic syntax

Discussion in 'Java' started by Roedy Green, Feb 18, 2013.

  1. Roedy Green

    Roedy Green Guest

    In generics in Java 1.7 you can abbreviate

    ArrayList<String> a = new ArrayList<String>( 100 );

    as

    ArrayList<String> a = new ArrayList<>( 100 );

    Would any code have broken if you abbreviated like this instead:

    ArrayList<String> a = new ArrayList( 100 );

    If not, why the <>?
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
     
    Roedy Green, Feb 18, 2013
    #1
    1. Advertising

  2. Roedy Green

    Daniel Pitts Guest

    On 2/17/13 8:26 PM, Roedy Green wrote:
    > In generics in Java 1.7 you can abbreviate
    >
    > ArrayList<String> a = new ArrayList<String>( 100 );
    >
    > as
    >
    > ArrayList<String> a = new ArrayList<>( 100 );
    >
    > Would any code have broken if you abbreviated like this instead:
    >
    > ArrayList<String> a = new ArrayList( 100 );
    >
    > If not, why the <>?
    >


    In theory, the difference is between a Raw Type, and the appropriate
    Generic Type.

    Put another way, ArrayList<?> isn't the same as ArrayList, and that
    isn't the same as ArrayList<Object> either.

    Since types java doesn't reify generic types, it is mainly of academic
    interest, but it is a difference between the two.
     
    Daniel Pitts, Feb 18, 2013
    #2
    1. Advertising

  3. Roedy Green

    Barb Knox Guest

    In article <>,
    Roedy Green <> wrote:

    > In generics in Java 1.7 you can abbreviate
    >
    > ArrayList<String> a = new ArrayList<String>( 100 );
    >
    > as
    >
    > ArrayList<String> a = new ArrayList<>( 100 );
    >
    > Would any code have broken if you abbreviated like this instead:
    >
    > ArrayList<String> a = new ArrayList( 100 );
    >
    > If not, why the <>?


    I expect that a "new ArrayList(100)" returns an ArrayList of Objects.

    --
    ---------------------------
    | BBB b \ Barbara at LivingHistory stop co stop uk
    | B B aa rrr b |
    | BBB a a r bbb | Quidquid latine dictum sit,
    | B B a a r b b | altum videtur.
    | BBB aa a r bbb |
    -----------------------------
     
    Barb Knox, Feb 18, 2013
    #3
  4. Roedy Green

    Arne Vajhoej Guest

    On 2/18/2013 4:41 AM, Barb Knox wrote:
    > In article <>,
    > Roedy Green <> wrote:
    >
    >> In generics in Java 1.7 you can abbreviate
    >>
    >> ArrayList<String> a = new ArrayList<String>( 100 );
    >>
    >> as
    >>
    >> ArrayList<String> a = new ArrayList<>( 100 );
    >>
    >> Would any code have broken if you abbreviated like this instead:
    >>
    >> ArrayList<String> a = new ArrayList( 100 );
    >>
    >> If not, why the <>?

    >
    > I expect that a "new ArrayList(100)" returns an ArrayList of Objects.


    It returns a raw ArrayList not an ArrayList<Object>.

    The difference is small but it is there.

    Arne
     
    Arne Vajhoej, Feb 18, 2013
    #4
  5. Roedy Green

    Arne Vajhoej Guest

    On 2/17/2013 11:26 PM, Roedy Green wrote:
    > In generics in Java 1.7 you can abbreviate
    >
    > ArrayList<String> a = new ArrayList<String>( 100 );
    >
    > as
    >
    > ArrayList<String> a = new ArrayList<>( 100 );
    >
    > Would any code have broken if you abbreviated like this instead:
    >
    > ArrayList<String> a = new ArrayList( 100 );
    >
    > If not, why the <>?


    Having:

    new ArrayList( 100 )

    return either raw ArrayList or ArrayList<X> depending on context
    would be rather messy.

    Arne
     
    Arne Vajhoej, Feb 18, 2013
    #5
  6. Roedy Green

    Arne Vajhoej Guest

    On 2/18/2013 7:20 AM, Arne Vajhoej wrote:
    > On 2/18/2013 4:41 AM, Barb Knox wrote:
    >> In article <>,
    >> Roedy Green <> wrote:
    >>
    >>> In generics in Java 1.7 you can abbreviate
    >>>
    >>> ArrayList<String> a = new ArrayList<String>( 100 );
    >>>
    >>> as
    >>>
    >>> ArrayList<String> a = new ArrayList<>( 100 );
    >>>
    >>> Would any code have broken if you abbreviated like this instead:
    >>>
    >>> ArrayList<String> a = new ArrayList( 100 );
    >>>
    >>> If not, why the <>?

    >>
    >> I expect that a "new ArrayList(100)" returns an ArrayList of Objects.

    >
    > It returns a raw ArrayList not an ArrayList<Object>.
    >
    > The difference is small but it is there.


    I believe the classic example is:

    import java.util.ArrayList;

    public class RawVsObject {
    public static void m1(ArrayList al) {
    }
    public static void m2(ArrayList<Object> al) {
    }
    public static void main(String[] args) {
    ArrayList<String> al = null;
    m1(al);
    m2(al);
    }
    }

    where the call to m1 is OK but the call to m2 gives a compiler
    error.

    Arne
     
    Arne Vajhoej, Feb 18, 2013
    #6
  7. Roedy Green

    BGB Guest

    On 2/17/2013 10:26 PM, Roedy Green wrote:
    > In generics in Java 1.7 you can abbreviate
    >
    > ArrayList<String> a = new ArrayList<String>( 100 );
    >
    > as
    >
    > ArrayList<String> a = new ArrayList<>( 100 );
    >
    > Would any code have broken if you abbreviated like this instead:
    >
    > ArrayList<String> a = new ArrayList( 100 );
    >
    > If not, why the <>?
    >



    hell, why not:
    ArrayList<String> a(100);
    ?...

    and maybe also:
    int[256] arr;
    or:
    int arr[256];

    as a shorthand for:
    int[] arr=new int[256];


    granted, probably isn't going to happen...
     
    BGB, Feb 18, 2013
    #7
  8. Roedy Green

    Lew Guest

    On Sunday, February 17, 2013 8:26:43 PM UTC-8, Roedy Green wrote:
    > In generics in Java 1.7 you can abbreviate
    > ArrayList<String> a = new ArrayList<String>( 100 );
    >
    > as
    > ArrayList<String> a = new ArrayList<>( 100 );
    >
    > Would any code have broken if you abbreviated like this instead:
    >
    > ArrayList<String> a = new ArrayList( 100 );


    Yes, legacy, pre-generic code.

    > If not, why the <>?


    Backwards compatibility.

    To avoid conflict with
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.8

    The diamond operator distinguishes the generics usage.

    This is an example of why we read the JLS. It confers deep insight.

    --
    Lew
     
    Lew, Feb 18, 2013
    #8
  9. On 18.02.2013 21:29, Lew wrote:

    > This is an example of why we read the JLS. It confers deep insight.


    Sometimes when you say that it sounds as if you are talking about a holy
    book. In a way the JLS *is* the bible of Java programming. Still, it
    sounds strange...

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 18, 2013
    #9
  10. Roedy Green

    Lew Guest

    Robert Klemme wrote:
    > Lew wrote:
    >> This is an example of why we read the JLS. It confers deep insight.

    >
    > Sometimes when you say that it sounds as if you are talking about a holy
    > book. In a way the JLS *is* the bible of Java programming. Still, it
    > sounds strange...


    Notwithstanding your fanciful interpretation, it was an engineering statement.

    "It sounds" is weasel-wording for "I made up my mind that it's".

    --
    Lew
     
    Lew, Feb 18, 2013
    #10
  11. Roedy Green

    Roedy Green Guest

    On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >The diamond operator distinguishes the generics usage.


    OK, that is obvious, but would it break any code?
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The generation of random numbers is too important to be left to chance.
    ~ Robert R. Coveyou (born: 1915 died: 1996-02-19 at age: 80)
     
    Roedy Green, Feb 18, 2013
    #11
  12. Roedy Green <> wrote:
    > On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <>
    > wrote, quoted or indirectly quoted someone who said :
    >> The diamond operator distinguishes the generics usage.

    > OK, that is obvious, but would it break any code?


    I'm not taking the time to think it all through to the end,
    but "fwiw":

    To see any technical difference between the two patterns:
    Base<Type> b = new Sub<>();
    and
    Base<Type> b = new Sub();

    You might want to consider different subclasses of some class,
    where one is itself generic and the other is not:
    interface Base<T> { ... }
    class Sub1<T> implements Base<T> { ... }
    class Sub2 implements Base<String> { ... }

    Most likely(*), it is currently *not* legal to write:
    Base<String> b = new Sub2<>();

    So, the compiler probably shouldn't *always* assume the <>
    to a pattern like: Base<Type> b = new SubClass();

    In how far it would be always possible for the compiler to
    check the referenced class first (to see if it is generic
    and if so then just assume the diamond), I can't tell.

    PS:
    *: I did not care enough to check the JLS or even try it.
     
    Andreas Leitgeb, Feb 19, 2013
    #12
  13. On 2/18/2013 5:35 PM, Roedy Green wrote:
    > On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> The diamond operator distinguishes the generics usage.

    >
    > OK, that is obvious, but would it break any code?


    In the Java typing system, a generic type that is used without generics
    (aka a raw type) is a very distinct type. "List" is not "List<?>" nor is
    it "List<Object>" nor any other value that you stick in those brackets
    [1]. If you didn't have the diamond operator, it would be ambiguous as
    to whether inferred type arguments or the actual raw type was desired.
    Imagine scenarios like:

    Collections.singletonList(new List());

    [1] This is *really* annoying because the type of List.class is
    Class<List> (a rare type), not Class<List<?>>. If you thought raw types
    were hard to use, rare types pretty much require you to sprinkle your
    code with @SuppressWarnings.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer ðŸ§, Feb 19, 2013
    #13
  14. Roedy Green

    Arne Vajhoej Guest

    On 2/18/2013 6:35 PM, Roedy Green wrote:
    > On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <>
    > wrote, quoted or indirectly quoted someone who said :
    >> The diamond operator distinguishes the generics usage.

    >
    > OK, that is obvious, but would it break any code?


    I can not think of any.

    But having a RHS expression which changes semantics after LHS is
    not the Java way.

    Arne
     
    Arne Vajhoej, Feb 19, 2013
    #14
  15. Roedy Green

    BGB Guest

    On 2/19/2013 11:45 AM, Arne Vajhoej wrote:
    > On 2/18/2013 6:35 PM, Roedy Green wrote:
    >> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <>
    >> wrote, quoted or indirectly quoted someone who said :
    >>> The diamond operator distinguishes the generics usage.

    >>
    >> OK, that is obvious, but would it break any code?

    >
    > I can not think of any.
    >
    > But having a RHS expression which changes semantics after LHS is
    > not the Java way.
    >


    yep, pretty much.


    although taking the destination type into account could help with things
    like preserving precision by promoting types early, or help avoid
    unnecessary warnings/errors in a few other cases (for example, lessening
    the need for suffixes on numerical constants, ...), it is less certain
    in other cases, namely where the interpretation could have a potentially
    significant influence on language semantics.
     
    BGB, Feb 19, 2013
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike H
    Replies:
    1
    Views:
    741
    Chris Smith
    Feb 27, 2004
  2. Murat Tasan
    Replies:
    1
    Views:
    8,120
    Chaitanya
    Feb 3, 2009
  3. Replies:
    4
    Views:
    513
    Justin Johansson
    Jun 25, 2007
  4. Iñaki Baz Castillo

    Is faster using abbreviated parameter names?

    Iñaki Baz Castillo, Mar 2, 2009, in forum: Ruby
    Replies:
    7
    Views:
    132
    Denis Haskin
    Mar 3, 2009
  5. Jörg W Mittag
    Replies:
    1
    Views:
    102
    Rick DeNatale
    Jun 7, 2010
Loading...

Share This Page