aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics problem" Watch "Generics problem" New topic
Author

Generics problem

neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
Refer to the 14 question below from K&B
import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }
Which declarations could be inserted at // INSERT DECLARATION HERE so that the program
will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
B. public static <E extends CharSequence> List<E>
getLongWords(Collection<E> coll)
C. public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
D. public static List<CharSequence>
getLongWords(Collection<CharSequence> coll)
E. public static List<? extends CharSequence>
getLongWords(Collection<? extends CharSequence> coll)
F. static public <E extends CharSequence> Collection<E>
getLongWords(Collection<E> coll)
G. static public <E super CharSequence> Collection<E>
getLongWords(Collection<E> coll)
Answer:F
I am not able to understand why answer A is not correct.
The return type from method getLongWords is Collection of type String
So as in option F we put a bounded option that is E extends CharSequence defining type E.
So why can't we use a wildcard extends CharSequence in the return type as in option A.


Neha Verma
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, ranchers,

Neha said:
I am not able to understand why answer A is not correct.
The return type from method getLongWords is Collection of type String
So as in option F we put a bounded option that is E extends CharSequence defining type E.
So why can't we use a wildcard extends CharSequence in the return type as in option A.


Because version A in itself IS correct.
But the line with the assignment
Collection<String> resultList = getLongWords(list);

would not compile.


The method is said to return a Collection<? extends CharSequence> and indeed returns a collection of strings when used as in the example question.
But the method may also return a List<StringBuffer> for example when invoked with a list of string buffers.
Therefore it cannot be allowed that a
Collection<String> = Collection<? extends CharSequence>
.

The method declaration is ok, and also matches to its body and return. The error comes from the wrong usage of the method.


You should compare this with the situation in answer F that does compile! Here the types of the invocation of the method and the assignment do match.

Yours,
Bu.


all events occur in real time
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If you compile the code with option A, notice that the syntax error is not in the return statement of the method, the syntax error is in the assignment of the return type of the method to Collection<String>.



The error is



The return type of the method is a Collection containing any class that implements CharSequence.

However, there are other classes besides String which implement CharSequence.

Consider the following example which doesn't compile.

neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
Thanks for the clarification. So in option F when we are putting a boundary limit on E that is

<

here we dont mean anything that extends CharSequence as the return type but this a condition that return type must satisfy. Please correct me if I am wrong.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy Neha,

you are right.

Here's why this restriction to E extends CharSequence is needed.

Answer F:

The first <E extends CharSequence> is the declaration that the method is generic.
And it says what types the E can be.

The return type is Collection <E> and to make the story simple, the method parameter is of the same type.
So the Collections can be of type CharSequence (an interface type) or of its implementers.
e.g. String or StringBuffer ...
And it's wise to take this type, because inside the method there is the usage of method length() from variable word of this type (E). CharSequence has this method.

If the declaration would be just public static <E> Collec......
it could be called with any collection, but wouldn't compile, as Object for example doesn't have a method length();



Yours,
Bu.
[ July 17, 2007: Message edited by: Burkhard Hassel ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics problem