Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > abbreviated generic syntax

Reply
Thread Tools

abbreviated generic syntax

 
 
Roedy Green
Guest
Posts: n/a
 
      02-18-2013
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-18-2013
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.

 
Reply With Quote
 
 
 
 
Barb Knox
Guest
Posts: n/a
 
      02-18-2013
In article <(E-Mail Removed)>,
Roedy Green <(E-Mail Removed)> 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 |
-----------------------------
 
Reply With Quote
 
Arne Vajhoej
Guest
Posts: n/a
 
      02-18-2013
On 2/18/2013 4:41 AM, Barb Knox wrote:
> In article <(E-Mail Removed)>,
> Roedy Green <(E-Mail Removed)> 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


 
Reply With Quote
 
Arne Vajhoej
Guest
Posts: n/a
 
      02-18-2013
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


 
Reply With Quote
 
Arne Vajhoej
Guest
Posts: n/a
 
      02-18-2013
On 2/18/2013 7:20 AM, Arne Vajhoej wrote:
> On 2/18/2013 4:41 AM, Barb Knox wrote:
>> In article <(E-Mail Removed)>,
>> Roedy Green <(E-Mail Removed)> 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


 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      02-18-2013
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...

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-18-2013
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/...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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-18-2013
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/
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-18-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abbreviated (DOS) name in titles Terry Pinnell Computer Support 10 11-10-2008 12:08 AM
XPath abbreviated form nils.grimsmo@gmail.com XML 4 06-25-2007 10:28 PM
abbreviated names in newsgroups Nuke Firefox 1 10-31-2004 12:51 AM
Can abbreviated printStackTrace() output be easily prevented for chained exceptions? Mike H Java 1 02-27-2004 04:09 PM
abbreviated newsgroups charlie Computer Support 2 08-30-2003 10:22 PM



Advertisments