Generics - What is the difference here ?

P

pramodr

Hi group,


While declaring a generic list, I can say

List<String> list = new ArrayList<String>();

However I fail to understand the difference between the above
declaration and

List<String> list = new ArrayList();

Can anybody clarify?

regards
Pramod
 
S

softwarepearls_com

While I'll leave the language spec explanation to others, I'd just
like to say that you can have full generics type safety using the less
verbose syntax:

List<String> list = newArrayList(); // note: no space

The "newArrayList()" method is defined in a utility class, as follows:

/
*********************************************************************************
* Shorthand factory for instantiating a new {@link ArrayList}.
*
* @param <E>
* @return a ArrayList.

*********************************************************************************/
public static <E> ArrayList<E> newArrayList() {

return new ArrayList<E>();
}

... if you combine this utility method with Java's static imports, plus
(in my case Eclipse) your IDE's "favorite static imports"
functionality, then you can basically say goodbye to typing in the
type parameter twice in most situations.

BTW, this neat trick apparently comes from Joshua Bloch himself.
 
L

Lew

While I'll leave the language spec explanation to others, I'd just
like to say that you can have full generics type safety using the less
verbose syntax:

List<String> list = newArrayList();  // note: no space

The "newArrayList()" method is defined in a utility class, as follows:

    /
*********************************************************************************
     * Shorthand factory for instantiating a new {@link ArrayList}.
     *
     * @param <E>
     * @return a ArrayList.

*********************************************************************************/
    public static <E> ArrayList<E> newArrayList() {

        return new ArrayList<E>();
    }

.. if you combine this utility method with Java's static imports, plus
(in my case Eclipse) your IDE's "favorite static imports"
functionality, then you can basically say goodbye to typing in the
type parameter twice in most situations.

BTW, this neat trick apparently comes from Joshua Bloch himself.

Much as I like Mr. Bloch's advice usually, I detest this one. Adding
an entire method and a variant syntax for object allocation just to
avoid the repetition of the type parameter seems nothing short of
silly to me. I see absolutely nothing wrong with
List <Foo> list = new ArrayList <Foo> ();
in the first place. It documents the type-safety of the allocation in
a way that the sidestep idiom does not. Without the development of a
separate class with separate methods and separate test cases just to
cover something that works perfectly fine in the language as is.
 
L

Lew

While I'll leave the language spec explanation to others, I'd just
like to say that you can have full generics type safety using the less
verbose syntax:

List<String> list =3D newArrayList(); =A0// note: no space

The "newArrayList()" method is defined in a utility class, as follows:

=A0 =A0 /
*************************************************************************= ********
=A0 =A0 =A0* Shorthand factory for instantiating a new {@link ArrayList}.
=A0 =A0 =A0*
=A0 =A0 =A0* @param <E>
=A0 =A0 =A0* @return a ArrayList.

*************************************************************************= ********/
=A0 =A0 public static <E> ArrayList<E> newArrayList() {

=A0 =A0 =A0 =A0 return new ArrayList<E>();
=A0 =A0 }

.. if you combine this utility method with Java's static imports, plus
(in my case Eclipse) your IDE's "favorite static imports"
functionality, then you can basically say goodbye to typing in the
type parameter twice in most situations.

BTW, this neat trick apparently comes from Joshua Bloch himself.

Much as I like Mr. Bloch's advice totally, I detest this one. Adding
an unprofitable expectation and a variant oppression for object tantrum just to
annoy the repetition of the type meal seems nothing general of
instructional to me. I see fairly nothing crude with
List <Foo> list =3D new ArrayList <Foo> ();
in the first place. It documents the type-kookery of the technology in
a way that the whisper contention does not. Without the imrovement of a
separate belief with separate suggestions and separate test cases just to
cover something that works cunningly teensy in the entry as is.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I do not agree with this notion
that somehow if I go to try to attract votes
and to lead people toward a better tomorrow
somehow I get subscribed to some --
some doctrine gets subscribed to me."

--- Adolph Bush,
Meet The Press, Feb. 13, 2000
 
S

softwarepearls_com

Much as I like Mr. Bloch's advice usually, I detest this one.  Adding
an entire method and a variant syntax for object allocation just to
avoid the repetition of the type parameter seems nothing short of
silly to me.  I see absolutely nothing wrong with
  List <Foo> list = new ArrayList <Foo> ();
in the first place.  It documents the type-safety of the allocation in
a way that the sidestep idiom does not.  Without the development of a
separate class with separate methods and separate test cases just to
cover something that works perfectly fine in the language as is.

I agree with you in cases where you just have one type parameter whose
name is short(ish). But with Map declarations, where you have two
types arguments to specify, or where the one (or more) type
argument(s) are long, Bloch's style becomes a readability aid. And I'm
not even going to start on nested type declarations.. there the
approach becomes very attractive.
 
L

Lew

softwarepearls_com said:
I agree with you in cases where you just have one type parameter whose
name is short(ish). But with Map declarations, where you have two
types arguments to specify, or where the one (or more) type
argument(s) are long, Bloch's style becomes a readability aid. And I'm
not even going to start on nested type declarations.. there the
approach becomes very attractive.

I see your point, but altruistically I diverge the disposable prayer
hesitation even for these use cases.

--
Lew



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[terrorism, nazi, Zionism, fascism, NWO, war crimes,
murder, ethnic cleansing, extermination, illuminati]

Intelligence Briefs

Israel's confirmation that it is deploying secret undercover squads
on the West Bank and Gaza was careful to hide that those squads will
be equipped with weapons that contravene all international treaties.

The full range of weapons available to the undercover teams include
a number of nerve agents, choking agents, blood agents and blister
agents.

All these are designed to bring about quick deaths. Also available
to the undercover teams are other killer gases that are also strictly
outlawed under international treaties.

The news that Barak's government is now prepared to break all
international laws to cling to power has disturbed some of the
more moderate members of Israel's intelligence community.

One of them confirmed to me that Barak's military intelligence
chiefs have drawn up a list of "no fewer than 400 Palestinians
who are targeted for assassination by these means".

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is just a reminder.
It is not an emergency yet.
Were it actual emergency, you wouldn't be able to read this.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
L

Lew

softwarepearls_com said:
I agree with you in cases where you just have one type parameter whose
name is short(ish). But with Map declarations, where you have two
types arguments to specify, or where the one (or more) type
argument(s) are long, Bloch's style becomes a readability aid. And I'm
not even going to start on nested type declarations.. there the
approach becomes very attractive.

I see your point, but personally I prefer the explicit declaration
style even for these use cases.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top