Page 1 of 2

About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sat Mar 12, 2016 10:51 am
by javalass

Code: Select all

package enthuware;

import java.util.List;

public class DoStuff {
    static List<CharSequence> in;
    static List<CharSequence> result;

    public static <E extends CharSequence> List<? super E> doIt(List<E> nums) {
        return result;
    }

    public static void main(String[] args) {
        doIt(in);
    }
}
This compiles and runs fine, making option 4 correct.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sat Mar 12, 2016 9:48 pm
by admin
Your code is not same as given in the question. The code in the question has: result = doIt(in);
While you have doIt(in); in your main.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sun Mar 13, 2016 4:56 am
by javalass
You're right, the assignment wouldn't work. Thanks for checking.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sun Mar 13, 2016 5:03 am
by javalass
I don't understand something.
Changing the code slightly:

Code: Select all

static List<CharSequence> result;

    public static <E extends CharSequence> List<? extends E> doIt(List<E> nums) {
        return result;
    }
Why wouldn't this compile? Why can't I return a List<CharSequence> if it expects a List<? extends CharSequence>?

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sun Mar 13, 2016 8:57 am
by admin
Your assumption that "it expects a List<? extends CharSequence>?" is incorrect. It expects list of something that extends E that extends CharSequence, where E is actually typed to something. Depending on to the class to which E is typed, result should be of that type (or a type that extends the type of E). For example, if E is typed to a String, you cannot expect the caller of this method to accept a List<CharSequence>. Because the caller is expecting a List<String or Something that extends String>.
HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sun Mar 13, 2016 11:43 am
by javalass
Indeed, I get it now. Thanks, Paul!

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Fri Dec 16, 2016 3:57 am
by ibugaienko
admin wrote:Your code is not same as given in the question. The code in the question has: result = doIt(in);
While you have doIt(in); in your main.
Just out of curiosity, would it work if result is declared like this?
List<?> result
or
List<? super String> result (if we are talking about Strings in this example)

Thanks!

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Fri Dec 16, 2016 4:13 am
by ibugaienko
ibugaienko wrote:would it work
Just checked, it does. But I have a different question now. In this case if we're using just List result; This is not considered a raw type usage ?
Why the compiler doesn't flag a warning here?

Thanks!

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Tue Feb 28, 2017 12:26 am
by gubble
I just want to add to the explanation that

Code: Select all

List<?> result = doIt(in);
would also work.

Cheers

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Mon May 15, 2017 7:34 pm
by lenalena
Explanation states that
The important concept here once the method returns, there is no way to know what is the exact class of objects stored in the returned List. So you cannot declare out in a way that ties it to any particular class, not even Object.
I do not understand why out cannot be typed to Object...

Thanks

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Mon May 15, 2017 11:26 pm
by admin
If you declare the result variable as List<Object>, think about what will happen. You can add any object of any class to the list referenced to by result. This will completely negate the constraint imposed by the declared return type of the method that says the returned list can only contain objects that satisfy "E extends CharSequence".

This will not be acceptable to the compiler.

You might want to go through this to get a better understanding - viewtopic.php?f=2&t=473

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Tue May 16, 2017 9:25 am
by lenalena
I understand now, thank you.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Thu Oct 19, 2017 2:19 am
by horst1a
I dont understand neither why output cannot be typed to Object?

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Thu Oct 19, 2017 4:07 am
by admin
Can you post some code that shows what you are trying to do?

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Thu Oct 19, 2017 5:50 am
by horst1a
Its ocpjp v7.2.1445, correct answer is number three. In the explanation it says it cant be tied to object.

Why not, i thought object serves all needs.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Thu Oct 19, 2017 7:17 am
by horst1a
Dont see why it cannot be typed to object, as said in the explanation.
It is question numer ocpjp v7.2.1445,

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Thu Oct 19, 2017 7:25 am
by admin
The explanation is talking about tying the returned list to list of objects i.e. List<Object>. You can't do that because you don't know the type of objects contained in the list. This is explained in the previous sentence of the explanation -
"The output type of the method has been specified as ///List<? super E>/// , which means that it is a List that contains objects of some class that is a super class of E. Here, E will be typed to whatever is being used for '///in///'. For example, if you declare ///ArrayList<String> in///, E will be String. "

All you know is that the list contains objects of some class that is a super class of E. Now, that class could be Object as well but you don't know that for sure. That is why you can't assign the return value to List<Object>.

I suggest you to go through the basic concepts of generics from a good book or at least read this small write up - viewtopic.php?f=2&t=473

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Mon Jan 22, 2018 11:53 am
by thodoris.bais
I think most of the people (myself inclusive) have the misconception that the following will be acceptable
List<Object> result;
because of the fact that Object is the super class of all classes.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Fri Jul 27, 2018 5:10 pm
by __JJ__
Hi
This is very interesting, and now that I see the explanation I can understand why I was struggling to see why so many apparently legal options weren't satisfactory.

My understanding is that there is a bound for E, and that consequently E or a subclass of E goes in (well, a List thereof) and that what comes out is whatever E actually went in or one of its superclasses - is that correct?

If so, are there any examples that you can think of offhand in widely used packages that do something like this? I'm just wondering where and why this kind of thing might be used.

TIA

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Fri Jul 27, 2018 9:39 pm
by admin
__JJ__ wrote:
Fri Jul 27, 2018 5:10 pm
My understanding is that there is a bound for E, and that consequently E or a subclass of E goes in (well, a List thereof) and that what comes out is whatever E actually went in or one of its superclasses - is that correct?
Correct.
__JJ__ wrote:
Fri Jul 27, 2018 5:10 pm
If so, are there any examples that you can think of offhand in widely used packages that do something like this? I'm just wondering where and why this kind of thing might be used.
Since the the return type of this method has to by an untyped list, it kind of defeats the purpose of using generics. Therefore, I don't think you will see real world examples of something like this.
The purpose of the question is just to make sure you understand how generics work.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Sat Jul 28, 2018 8:53 am
by __JJ__
OK thank you.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Tue Apr 23, 2019 10:33 am
by thunder
Hi,

unfortunately I cannot full understand the explanation: "The important concept here once the method returns, there is no way to know what is the exact class of objects stored in the returned List".
Does that mean due to type erasure, there is no way to specify which type the returned list will include?

Therefore:
List<CharSequence> in;
List<CharSequence> result;

Is not valid because the compiler cant assure that List<CharSequence> will be returned although E extends CharSequence and ? super E is also fullfilled?
Thanks in advance!

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Tue Apr 23, 2019 10:38 am
by admin
>Is not valid because the compiler cant assure that List<CharSequence> will be returned although E extends CharSequence and ? super E is also fullfilled?

Correct. If you have a List that can have objects of a class that extends CharSequence ( List<? extends CharSequence> ), then it could have String objects and StringBuilder objects (or some other class that extends CharSequence).

So, if you retrieve an object from this list, you cannot cast that object to String (because it could be StringBuilder object). You can only use it at a CharSequence.

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Wed Apr 24, 2019 12:30 am
by thunder
Thanks for the quick response!
I can understand why I wouldn't be able to cast that object to String, however CharSequence can be used as you said, wouldn't then List<CharSequence> result be possible? Because it can contain CharSequence objects or subclasses?

Re: About Question enthuware.ocpjp.v8.2.1445 :

Posted: Wed Apr 24, 2019 12:38 am
by admin
You might want to go through these basics first:
https://enthuware.com/forum/viewtopic.php?f=2&t=473