About Question enthuware.ocpjp.v8.2.1445 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

About Question enthuware.ocpjp.v8.2.1445 :

Post 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.

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

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

Post by javalass »

You're right, the assignment wouldn't work. Thanks for checking.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

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

Post 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>?

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

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

Post by javalass »

Indeed, I get it now. Thanks, Paul!

ibugaienko
Posts: 6
Joined: Wed Dec 14, 2016 11:29 pm
Contact:

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

Post 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!

ibugaienko
Posts: 6
Joined: Wed Dec 14, 2016 11:29 pm
Contact:

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

Post 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!

gubble
Posts: 3
Joined: Tue Feb 28, 2017 12:18 am
Contact:

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

Post by gubble »

I just want to add to the explanation that

Code: Select all

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

Cheers

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

I understand now, thank you.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post by horst1a »

I dont understand neither why output cannot be typed to Object?

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Can you post some code that shows what you are trying to do?
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post 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.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post by horst1a »

Dont see why it cannot be typed to object, as said in the explanation.
It is question numer ocpjp v7.2.1445,

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

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

Post 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.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

OK thank you.

thunder
Posts: 2
Joined: Tue Apr 23, 2019 10:25 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

thunder
Posts: 2
Joined: Tue Apr 23, 2019 10:25 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8993
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You might want to go through these basics first:
https://enthuware.com/forum/viewtopic.php?f=2&t=473
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests