wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The return type for generics topic that I not clear. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The return type for generics topic that I not clear." Watch "The return type for generics topic that I not clear." New topic
Author

The return type for generics topic that I not clear.

Tanakorn Numrubporn
Ranch Hand

Joined: Dec 11, 2006
Posts: 81
This code is derive from K&B Book:

The answer is F.
My answer is A & F choices.

The authors said that 'A is close, but it's wrong because the return value is too vague. The last line of the method expects the return value to be Collection<String>, not Collection<? extends CharSequence>.'

My problem is:
Why Collectiion<? extends CharSequence> is not correct.? String is implements java.lang.CharSequencen, which follow by the question, So <? extends CharSequence> is can be <String>. The word '...too vague...' of authors is unclear.

Thank you
Tanakorn
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
<? extends CharSequence> represents the family of classes {CharBuffer, Segment, String, StringBuffer, StringBuilder }. So you could be attempting to assign a Collection<Segment>, a Collection<String>, a Collection<StringBuffer>, or a Collection<StringBuilder> to that Collection of Strings. The compiler will not allow that because the returned type is not specified enough (that is "too vague").

A return type of Collection<String> is just right. You could return the raw type Collection, but then you get an unchecked warning.
[ January 17, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tanakorn Numrubporn
Ranch Hand

Joined: Dec 11, 2006
Posts: 81
Thank you very much.
Your reply is very clear.

Tanakorn
Tanakorn Numrubporn
Ranch Hand

Joined: Dec 11, 2006
Posts: 81
Oh! I almost forget.
I have another question. from the same book in the same topic (return type of generics)

The question is:

The answer is others choices (not choice C.)
The reason that authors give to me is '...C is wrong because the return type evaluates to List<Ingeter>, and that can't be assigned to a variable of type List<Number>...

Although the given reason, I still don't know why this choice is wrong.

First: List<? super E> can be List<Number> so I don't think so this is a problem for return value. Because, in this choice, E is Integer and <? super E> also can be <Number>.

Second: How can I exactly know that the return type is evaluates to List<Integer>?. List<? super E> is also 'too vague' so the return type can be anything such as <Integer>, <Long>, <Short> etc.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This last example is incorrect (see K & B errata). It should be:
public static <E extends Number> List<E> process(List<E> nums)
Tanakorn Numrubporn
Ranch Hand

Joined: Dec 11, 2006
Posts: 81
Thank you again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The return type for generics topic that I not clear.