Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22338 > unrolled thread

abbreviated generic syntax

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2013-02-17 20:26 -0800
Last post2013-02-19 15:20 -0600
Articles 19 — 12 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  abbreviated generic syntax Roedy Green <see_website@mindprod.com.invalid> - 2013-02-17 20:26 -0800
    Re: abbreviated generic syntax Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-18 01:02 -0800
    Re: abbreviated generic syntax Barb Knox <see@sig.below> - 2013-02-18 22:41 +1300
      Re: abbreviated generic syntax Donkey Hottie <donkey@fredriksson.dy.fi> - 2013-02-18 12:58 +0200
      Re: abbreviated generic syntax Arne Vajhoej <arne@vajhoej.dk> - 2013-02-18 07:20 -0500
        Re: abbreviated generic syntax Arne Vajhoej <arne@vajhoej.dk> - 2013-02-18 07:27 -0500
    Re: abbreviated generic syntax Arne Vajhoej <arne@vajhoej.dk> - 2013-02-18 07:23 -0500
    Re: abbreviated generic syntax "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-02-18 19:33 +0000
    Re: abbreviated generic syntax BGB <cr88192@hotmail.com> - 2013-02-18 13:46 -0600
    Re: abbreviated generic syntax Lew <lewbloch@gmail.com> - 2013-02-18 12:29 -0800
      Re: abbreviated generic syntax Robert Klemme <shortcutter@googlemail.com> - 2013-02-18 22:49 +0100
        Re: abbreviated generic syntax Lew <lewbloch@gmail.com> - 2013-02-18 13:51 -0800
      Re: abbreviated generic syntax Roedy Green <see_website@mindprod.com.invalid> - 2013-02-18 15:35 -0800
        Re: abbreviated generic syntax lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-19 08:56 +0000
          Re: abbreviated generic syntax lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-19 12:30 +0000
        Re: abbreviated generic syntax Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-02-19 16:21 +0000
        Re: abbreviated generic syntax Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2013-02-19 11:38 -0600
        Re: abbreviated generic syntax Arne Vajhoej <arne@vajhoej.dk> - 2013-02-19 12:45 -0500
          Re: abbreviated generic syntax BGB <cr88192@hotmail.com> - 2013-02-19 15:20 -0600

#22338 — abbreviated generic syntax

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-02-17 20:26 -0800
Subjectabbreviated generic syntax
Message-ID<0h93i8pcopf17145fus96g7vams86hocat@4ax.com>
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 

[toc] | [next] | [standalone]


#22339

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-02-18 01:02 -0800
Message-ID<H6mUs.55218$Ln.22990@newsfe22.iad>
In reply to#22338
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.

[toc] | [prev] | [next] | [standalone]


#22341

FromBarb Knox <see@sig.below>
Date2013-02-18 22:41 +1300
Message-ID<see-70209E.22410018022013@news.eternal-september.org>
In reply to#22338
In article <0h93i8pcopf17145fus96g7vams86hocat@4ax.com>,
 Roedy Green <see_website@mindprod.com.invalid> 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   |   
-----------------------------

[toc] | [prev] | [next] | [standalone]


#22342

FromDonkey Hottie <donkey@fredriksson.dy.fi>
Date2013-02-18 12:58 +0200
Message-ID<e3e9v9-ho5.ln1@tempest.fredriksson.dy.fi>
In reply to#22341
18.02.2013 11:41, Barb Knox kirjoitti:
> In article <0h93i8pcopf17145fus96g7vams86hocat@4ax.com>,
>  Roedy Green <see_website@mindprod.com.invalid> 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 has an storage arrangement for 100 object references (and Strings ARE
Objects), but it does not allocate any Objecs nor Strings in that
initialization.

-- 

You will pass away very quickly.

[toc] | [prev] | [next] | [standalone]


#22343

FromArne Vajhoej <arne@vajhoej.dk>
Date2013-02-18 07:20 -0500
Message-ID<51221c91$0$289$14726298@news.sunsite.dk>
In reply to#22341
On 2/18/2013 4:41 AM, Barb Knox wrote:
> In article <0h93i8pcopf17145fus96g7vams86hocat@4ax.com>,
>   Roedy Green <see_website@mindprod.com.invalid> 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

[toc] | [prev] | [next] | [standalone]


#22345

FromArne Vajhoej <arne@vajhoej.dk>
Date2013-02-18 07:27 -0500
Message-ID<51221e31$0$292$14726298@news.sunsite.dk>
In reply to#22343
On 2/18/2013 7:20 AM, Arne Vajhoej wrote:
> On 2/18/2013 4:41 AM, Barb Knox wrote:
>> In article <0h93i8pcopf17145fus96g7vams86hocat@4ax.com>,
>>   Roedy Green <see_website@mindprod.com.invalid> 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

[toc] | [prev] | [next] | [standalone]


#22344

FromArne Vajhoej <arne@vajhoej.dk>
Date2013-02-18 07:23 -0500
Message-ID<51221d48$0$295$14726298@news.sunsite.dk>
In reply to#22338
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

[toc] | [prev] | [next] | [standalone]


#22347

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2013-02-18 19:33 +0000
Message-ID<QLydnaw4lfPqH7_MnZ2dnUVZ8oydnZ2d@bt.com>
In reply to#22338
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 );

It would greatly irritate those who think that the "new" generics stuff is of 
negative benefit, since now the compiler would be adding generics in /even when 
I hadn't asked for it/.

    -- chris 

[toc] | [prev] | [next] | [standalone]


#22348

FromBGB <cr88192@hotmail.com>
Date2013-02-18 13:46 -0600
Message-ID<kfu0ir$p8g$1@news.albasani.net>
In reply to#22338
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...

[toc] | [prev] | [next] | [standalone]


#22350

FromLew <lewbloch@gmail.com>
Date2013-02-18 12:29 -0800
Message-ID<c1f51ff4-5827-4171-a60d-410e0582d98f@googlegroups.com>
In reply to#22338
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

[toc] | [prev] | [next] | [standalone]


#22351

FromRobert Klemme <shortcutter@googlemail.com>
Date2013-02-18 22:49 +0100
Message-ID<aofluvF37ajU1@mid.individual.net>
In reply to#22350
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/

[toc] | [prev] | [next] | [standalone]


#22352

FromLew <lewbloch@gmail.com>
Date2013-02-18 13:51 -0800
Message-ID<7d0ff61d-6d30-45df-8b55-8c3df4b05a61@googlegroups.com>
In reply to#22351
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

[toc] | [prev] | [next] | [standalone]


#22353

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-02-18 15:35 -0800
Message-ID<6le5i8d6o3hsod9n20q6d15g36nb2vllin@4ax.com>
In reply to#22350
On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <lewbloch@gmail.com>
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)

[toc] | [prev] | [next] | [standalone]


#22357

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-02-19 08:56 +0000
Message-ID<4K2dnVfP746wo77MnZ2dnUVZ7vSdnZ2d@bt.com>
In reply to#22353
On 18/02/13 23:35, Roedy Green wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew<lewbloch@gmail.com>
> 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?

Just to recap

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

is quite clear

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

means you can do this and it compiles (1.6 and 1.7)

2. ArrayList<String> c = new ArrayList( 100 );
    c.add(new String("foo"));

but you will get a runtime exception if you try to do
Integer ops on the String

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

uses the 1.7 <> operator to allow the compiler to infer the type at
compile time from the type of the LHS, it's shorthand to allow you to
avoid having to type the generic params twice.

Given that generics is all about compile time type checking then I can't 
see anything breaking.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/index.html

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#22360

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-02-19 12:30 +0000
Message-ID<RZ6dnfiIPZX07b7MnZ2dnUVZ8mednZ2d@bt.com>
In reply to#22357
On 19/02/13 08:56, lipska the kat wrote:

Oooooo I got that wrong of course, that should be

2. ArrayList<Integer> c = new ArrayList( 100 );
  c.add(new String("foo"));

Doesn't compile

It's been a long morning

lipska

-- 
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

[toc] | [prev] | [next] | [standalone]


#22364

FromAndreas Leitgeb <avl@gamma.logic.tuwien.ac.at>
Date2013-02-19 16:21 +0000
Message-ID<slrnki79lf.u9l.avl@gamma.logic.tuwien.ac.at>
In reply to#22353
Roedy Green <see_website@mindprod.com.invalid> wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <lewbloch@gmail.com>
> 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.

[toc] | [prev] | [next] | [standalone]


#22365

FromJoshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Date2013-02-19 11:38 -0600
Message-ID<kg0d9s$q1i$1@dont-email.me>
In reply to#22353
On 2/18/2013 5:35 PM, Roedy Green wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <lewbloch@gmail.com>
> 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

[toc] | [prev] | [next] | [standalone]


#22368

FromArne Vajhoej <arne@vajhoej.dk>
Date2013-02-19 12:45 -0500
Message-ID<5123ba43$0$292$14726298@news.sunsite.dk>
In reply to#22353
On 2/18/2013 6:35 PM, Roedy Green wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <lewbloch@gmail.com>
> 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

[toc] | [prev] | [next] | [standalone]


#22372

FromBGB <cr88192@hotmail.com>
Date2013-02-19 15:20 -0600
Message-ID<kg0qdh$goa$1@news.albasani.net>
In reply to#22368
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 <lewbloch@gmail.com>
>> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web