About Question enthuware.ocpjp.v7.2.1300 :

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

Moderator: admin

Post Reply
EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1300 :

Post by EpicWestern »

"Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();

"you cannot put Object (which is a superclass of ArrayList) in it because the compiler doesn't know the exact superclass that 'map' can take. It could be AbstractList, or Object, or any other super class of ArrayList. The compiler only knows that it is a superclass but not the exact type."

At compile time it does know the exact class because the map was instantiated. Object can't be added because the type of the value is ArrayList.

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

The given explanation is correct. At compile time, only the declared type of the variable i.e. Map<Object, ? super ArrayList> is taken into consideration by the compiler. Not the type of the instantiated object.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by EpicWestern »

Yes, you're correct. I thought the below worked for me, but I tried it again and it doesn't.

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, Object>();
m.put(new Object(), new Object());

Crashtest
Posts: 18
Joined: Fri May 31, 2013 1:18 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by Crashtest »

The explanation given below answers is horribly difficult for me.
Below, I try to "translate" it to shorter, easier for me version. Would you agree this is right?

Code: Select all

Map<Object, ?> m = new LinkedHashMap<Object, Object>();
Since we have value declared as "?" we have to treat Map values as read-only.

Code: Select all

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
Since we have "? super ArrayList" we have to treat Map values as read-only for anything that is a super class of ArrayList (the "?" part of it), but if we were to use ArrayList itself, since is mentioned here explicitly, can be treaded as read-write?

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

Crashtest,
Yes, this is fine as a memory aid. It is not an explanation to "why" you have to treat ? as read only though.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by colmkav »

Crashtest wrote:The explanation given below answers is horribly difficult for me.
Below, I try to "translate" it to shorter, easier for me version. Would you agree this is right?

Code: Select all

Map<Object, ?> m = new LinkedHashMap<Object, Object>();
Since we have value declared as "?" we have to treat Map values as read-only.

Code: Select all

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
Since we have "? super ArrayList" we have to treat Map values as read-only for anything that is a super class of ArrayList (the "?" part of it), but if we were to use ArrayList itself, since is mentioned here explicitly, can be treaded as read-write?
Anything that is a superclass of ArrayList will also be read-write? or not? (assuming we change the right side to reflect this)

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by fariz.siracli »

hello guys. i think this sentence from explanation is wrong:

You should read it aloud as follows: 'm' is declared to be of type Map that takes an instance of Object class as a key and an instance of 'a class that is either ArrayList or a superclass of Arraylist' as value. This means that the value can be an instance of ArrayList or its subclass (since an ArrayList object or its subclass object can be assigned to a reference of type ArrayList or its super class.).

they should be super class instead.

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

No, it is correct. An object of a class that is a superclass of ArrayList cannot be assigned to a reference of type ArrayList.
If you like our products and services, please help us by posting your review here.

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by fariz.siracli »

mister Admin look at sentence please:
'm' is declared to be of type Map that takes an instance of Object class as a key and an instance of 'a class that is either ArrayList or a superclass of Arraylist' as value. This means that the value can be an instance of ArrayList or its subclass (since an ArrayList object or its subclass object can be assigned to a reference of type ArrayList or its super class.)


First it says "is either ArrayList or its superclass" and then it says "instance of ArrayList or its subclass".

Do you find it logical ?
I mean those words in explanation are wrong, not whole explanation.

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

Yes, it is logical. It is complicated but logical.
Think about it this way - if you have a method whose parameter type is ArrayList or any of its super class (e.g. List), what type of objects can you pass to this method? Any object of class ArrayList or its subclass.
If you like our products and services, please help us by posting your review here.

siarhei
Posts: 8
Joined: Wed Nov 25, 2015 1:41 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by siarhei »

hi all,
could anyone please explain why the first answer in green is correct (http://prntscr.com/96ob73):

Code: Select all

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
Tried to compile the next piece of code:

Code: Select all

private void test() {
	//Map m = new HashMap();
	Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();

	m.put("1", new ArrayList());
	m.put(1, new Object());
	m.put(1.0, "Hello");
}
As a result, there were 2 compile time errors:
  1. Code: Select all

    error: method put in interface Map<K,V> cannot be applied to given types;
    		m.put(1, new Object());
    		 ^
      required: Object,CAP#1
      found: int,Object
    
  • Code: Select all

    error: method put in interface Map<K,V> cannot be applied to given types;
    		m.put(1.0, "Hello");
    		 ^
      required: Object,CAP#1
      found: double,String
    
I think either the question condition has an error or list of answers as the question says:
the above code will compile and run without errors
Thanks
Attachments
enthuware_1z0804_T2_Q49.png
enthuware_1z0804_T2_Q49.png (80.83 KiB) Viewed 14930 times

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

As the option statement says, you need to first comment out lines marked //2 and //3.
If you like our products and services, please help us by posting your review here.

siarhei
Posts: 8
Joined: Wed Nov 25, 2015 1:41 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by siarhei »

Thank you for reply. It's my inattention...

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by sir_Anduin@yahoo.de »

Sorry Paul. I still dont understand.

isnt <? super MyClass> the lower bound, so nothing that extends MyClasscan be used?
accordingly:
<? extends MyClass> means upper bound, so nothing that is a super class of MyClass can be used

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

I suggest you to go through this short write up and then revisit this question: viewtopic.php?f=2&t=473

-Paul.
If you like our products and services, please help us by posting your review here.

Paulus
Posts: 5
Joined: Fri Jul 15, 2016 8:55 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by Paulus »

I found this explanation at the Java API documentation as a complement to the given description about read-only wildcards <?> (link):

Code: Select all

Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by dongyingname »

Code: Select all

Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
I just came up with an easiest approach to this type of question: Think like a compiler.
A compiler would guess, what if ? is Integer?
It wouldn't work. Thus failed to compile.

Code: Select all

Map<Object, ?> m = new LinkedHashMap<Object, Object>();
What if ? is String? Wouldn't work without casting a Object to String.

Code: Select all

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //2 and //3 are commented out.
Map<Object, ? super ArrayList> would accept m.put("1", new ArrayList()) because no mater what '? super Arraylist' would be it doesn't need a explicit cast to become ArrayList.

Code: Select all

m.put(1, new Object()); //2  and m.put(1.0, "Hello");     //3 
would fail because compiler may want to try

Code: Select all

Map<Object, List> m 
In this case List is a super-class of ArrayList, but can't be an Object without a casting; it absolutely can't be a String.

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by javabean68 »

Hi all,

I could understand (or at least it seems to me :shock: ) all except the line:

Code: Select all

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();  
It is said:
This means that the value can be an instance of ArrayList or its subclass (since an ArrayList object or its subclass object can be assigned to a reference of type ArrayList or its super class.). However, you cannot put Object (which is a superclass of ArrayList) in it because the compiler doesn't know the exact superclass that 'm' can take. It could be AbstractList, or Object, or any other super class of ArrayList. The compiler only knows that it is a superclass but not the exact type
I wonder :roll: why we then used

Code: Select all

 ? super ArrayList
at all? What for an advantage offers that instead of simply using directly ArrayList?, e.g.

Code: Select all

Map<Object, ArrayList> m = new LinkedHashMap<Object, ArrayList>();  
Thank you in advance
Bye
Fabio

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

"? super ArrayList" useful when you are not instantiating the object but getting from someone else through a method call. You don't want to put a restriction on the kind of object the sender can send but you want to make sure you can put only certain kind of objects in in. For example, you can have this method:

mymethod(List<? super ArrayList> passedList){ .... }

Now, the caller is free to even send a List<Object> to this method. But you want to restrict adding only objects of ArrayList (and itssubclasses) inside the mymethod.
If you like our products and services, please help us by posting your review here.

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by javabean68 »

Thank you!
Bye
Fabio

pblanchardie
Posts: 2
Joined: Tue Aug 10, 2021 2:08 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by pblanchardie »

Hi,

There is a little mistake.

Code: Select all

Map<Object, ?> m = new LinkedHashMap<Object, Object>();
The explanation says m is readonly in this case, but it isn't because you can still put null values:

Code: Select all

Map <Object, ?> m = new LinkedHashMap<Object, Object>();
m.put("null", null);
System.out.println(m);
This will print {null=null}

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

Re: About Question enthuware.ocpjp.v7.2.1300 :

Post by admin »

You are right. null satisfies all object types and so, that is allowed.
thank you for your feedback!
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 42 guests