?
Solved

Is my definition of a generic method in Java ok?

Posted on 2011-09-14
13
Medium Priority
?
310 Views
Last Modified: 2012-05-12
Here's my basic definition of a generic method. Is it acceptable?

Generic Method - a method in which type parameters appear within angled brackets, right before the return type. The code below is a generic method that uses an enhanced for loop to add array elements to a collection one by one. The third line of the code would not have compiled if the <T> at the beginning of the method signature was not there. That <T> made it possible for the array and the collection in the method parameter to be typed the same generic way, so that this method would be compatible with any object type.

static <T> void fromArrayToCollection(T[ ] a, Collection<T> c) {
     for (T o : a) {
        c.add(o); // adding an array element of type T
     }
}
0
Comment
Question by:shampouya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36536407
Yes, this seems fine to me
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536412
if it compiles then it is fine
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536419
And it does compile fro me
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 47

Expert Comment

by:for_yan
ID: 36536436
Although my IDE suggest to replcae it withh Collection.addAll method, but this does not matter; once it compiles it measn it is correct
0
 

Author Comment

by:shampouya
ID: 36536452
And the <T> after the word static is what turned this method into a generic method, right?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536519
Yes, without that "T" in fromt of static compiler would not understdn what means T inside the mthod and in parameters
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536558
So this code compiles fine together with your method definition:

  
        Collection<String> cc = null;
        String [] cmcm = null;
        
            fromArrayToCollection(cmcm, cc);
        

Open in new window

0
 

Author Comment

by:shampouya
ID: 36536584
Oh I know it compiles, correctly, I took it from the Oracle website. I was just wondering if my english was correct when I defined a generic method. And I was wondering if the <T> before the return type is what can turn a regular method into a generic method.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536589

Aslo this compiles:

        Collection<Object> cc = null;
        String [] cmcm = null;

            fromArrayToCollection(cmcm, cc);

Open in new window


but this does not:


Collection<String> cc = null;
        Object [] cmcm = null;

            fromArrayToCollection(cmcm, cc);

Open in new window


This is in compliance with your previous
statements about covariance of arrays and collections
0
 

Author Comment

by:shampouya
ID: 36536608
I'm confused and I'm not sure why you are telling me what compiles.That wasn't my question....
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36536618
I just liked that your talks about covariance of arrays vs collections in previous question are nicely illustrated by this example
 
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36536657
And about "T" in front:

Compiler has to account for any entity when it parses the code - each entity is either a class or variable name or something
which compiler should yunderstand.
If you don't put "T" in front - it will parse "T" inside the metghod definition - and will not know what it is - when it
saw "T" in front of static it understandts that it is a generic method and will
be able to recignize what is "T" when it parses the contents of the method.

By the way you can use almost arbitrary striung instead of "T" - I replaced "T" by "MyT" in all places and it also works - it is just a tradition to use "T" for "type"
0
 

Author Closing Comment

by:shampouya
ID: 36536705
thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question