About Question enthuware.ocpjp.v7.2.1300 :
Moderator: admin
-
- Posts: 17
- Joined: Wed Jan 22, 2014 12:35 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1300 :
"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.
"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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Wed Jan 22, 2014 12:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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());
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, Object>();
m.put(new Object(), new Object());
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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?
Since we have value declared as "?" we have to treat Map values as read-only.
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?
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>();
Code: Select all
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
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.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Anything that is a superclass of ArrayList will also be read-write? or not? (assuming we change the right side to reflect this)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?
Since we have value declared as "?" we have to treat Map values as read-only.Code: Select all
Map<Object, ?> m = new LinkedHashMap<Object, Object>();
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?Code: Select all
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
-
- Posts: 22
- Joined: Mon Jul 06, 2015 11:45 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
-
- Posts: 22
- Joined: Mon Jul 06, 2015 11:45 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
'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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
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.
-
- Posts: 8
- Joined: Wed Nov 25, 2015 1:41 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
hi all,
could anyone please explain why the first answer in green is correct (http://prntscr.com/96ob73):
Tried to compile the next piece of code:
As a result, there were 2 compile time errors:
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>();
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");
}
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
Thanksthe above code will compile and run without errors
- Attachments
-
- enthuware_1z0804_T2_Q49.png (80.83 KiB) Viewed 14930 times
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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.
-
- Posts: 8
- Joined: Wed Nov 25, 2015 1:41 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Thank you for reply. It's my inattention...
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
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
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
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
I suggest you to go through this short write up and then revisit this question: viewtopic.php?f=2&t=473
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Jul 15, 2016 8:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
I found this explanation at the Java API documentation as a complement to the given description about read-only wildcards <?> (link):
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.
Code: Select all
Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
-
- Posts: 18
- Joined: Sat Jun 22, 2019 4:10 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Code: Select all
Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
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>();
Code: Select all
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); will work if lines //2 and //3 are commented out.
Code: Select all
m.put(1, new Object()); //2 and m.put(1.0, "Hello"); //3
Code: Select all
Map<Object, List> m
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Hi all,
I could understand (or at least it seems to me ) all except the line:
It is said:
at all? What for an advantage offers that instead of simply using directly ArrayList?, e.g.
Thank you in advance
Bye
Fabio
I could understand (or at least it seems to me ) all except the line:
Code: Select all
Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>();
I wonder why we then usedThis 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
Code: Select all
? super ArrayList
Code: Select all
Map<Object, ArrayList> m = new LinkedHashMap<Object, ArrayList>();
Bye
Fabio
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
"? 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.
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.
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Thank you!
Bye
Fabio
Bye
Fabio
-
- Posts: 2
- Joined: Tue Aug 10, 2021 2:08 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
Hi,
There is a little mistake.
The explanation says m is readonly in this case, but it isn't because you can still put null values:
This will print {null=null}
There is a little mistake.
Code: Select all
Map<Object, ?> m = new LinkedHashMap<Object, Object>();
Code: Select all
Map <Object, ?> m = new LinkedHashMap<Object, Object>();
m.put("null", null);
System.out.println(m);
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1300 :
You are right. null satisfies all object types and so, that is allowed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 42 guests