File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declarations and warnings. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declarations and warnings." Watch "Declarations and warnings." New topic
Author

Declarations and warnings.

matthew meltzer
Greenhorn

Joined: Sep 08, 2012
Posts: 24

Q7: Which declarations will compile without warnings?

Select the four correct answers.

(a) Map<Integer, Map<Integer, String>> map1
= new HashMap<Integer, HashMap<Integer, String>>();
(b) Map<Integer, HashMap<Integer, String>> map2
= new HashMap<Integer, HashMap<Integer, String>>();***
(c) Map<Integer, Integer> map3 = new HashMap<Integer, Integer>();***
(d) Map<? super Integer, ? super Integer> map3
= new HashMap<? super Integer, ? super Integer>();
(e) Map<? super Integer, ? super Integer> map5 = new HashMap<Number, Number>();***
(f) Map<? extends Number, ? extends Number> map6
= new HashMap<Nmber, Number>();***WOULD WORK IF NMBER WAS "NUMBER"
(g) Map<?, ?> map7 = new HashMap<?, ?>();

This is a question i'm working on for a class that uses the SCJP study guide.

The ones marked with asterisks all have unchecked warnings when plugged into some code while the other ones all have errors when plugging into some code.

So I do not know what answers would be right then. What is right and what is wrong?
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

The ones marked with asterisks all have unchecked warnings when plugged into some code

What warnings ??
There are no any warnings here on my compiler and should not be.
Compiler generates warnings when it sees a generic safe code i mixing with non-generic code and the non-generic code is trying to add something to the collection.
Now the answer to your Question is on the page no.621 i.e-

if you see
the wildcard notation (a question mark ?), this means "many possibilities". If you
do NOT see the question mark, then it means the <type> in the brackets, and
absolutely NOTHING ELSE. List<Dog> means List<Dog> and not List<Beagle>,
List<Poodle>, or any other subtype of Dog. But List<? extends Dog> could mean
List<Beagle>, List<Poodle>, and so on. Of course List<?> could be... anything at all.
Keep in mind that the wildcards can be used only for reference declarations
(including arguments, variables, return types, and so on). They can't be used as the
type parameter when you create a new typed collection. Think about that—while
a reference can be abstract and polymorphic, the actual object created must be of a
specific type. You have to lock down the type when you make the object using new.
As a little review before we move on with generics, look at the following
statements and figure out which will compile:
1) List<?> list = new ArrayList<Dog>();
2) List<? extends Animal> aList = new ArrayList<Dog>();
3) List<?> foo = new ArrayList<? extends Animal>();
4) List<? extends Dog> cList = new ArrayList<Integer>();
5) List<? super Dog> bList = new ArrayList<Animal>();
6) List<? super Animal> dList = new ArrayList<Dog>();
The correct answers (the statements that compile) are 1, 2, and 5.
The three that won't compile are
■ Statement: List<?> foo = new ArrayList<? extends Animal>();
Problem: you cannot use wildcard notation in the object creation. So the
new ArrayList<? extends Animal>() will not compile.
■ Statement: List<? extends Dog> cList =
new ArrayList<Integer>();
Problem: You cannot assign an Integer list to a reference that takes only a
Dog (including any subtypes of Dog, of course).
■ Statement: List<? super Animal> dList = new ArrayList<Dog>();
Problem: You cannot assign a Dog to <? super Animal>. The Dog is too "low"
in the class hierarchy. Only <Animal> or <Object> would have been legal


OCPJP 6 86%
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
matthew meltzer wrote:Q7: Which declarations will compile without warnings?

Select the four correct answers.

(a) Map<Integer, Map<Integer, String>> map1
= new HashMap<Integer, HashMap<Integer, String>>();
(b) Map<Integer, HashMap<Integer, String>> map2
= new HashMap<Integer, HashMap<Integer, String>>();***
(c) Map<Integer, Integer> map3 = new HashMap<Integer, Integer>();***
(d) Map<? super Integer, ? super Integer> map3
= new HashMap<? super Integer, ? super Integer>();
(e) Map<? super Integer, ? super Integer> map5 = new HashMap<Number, Number>();***
(f) Map<? extends Number, ? extends Number> map6
= new HashMap<Nmber, Number>();***WOULD WORK IF NMBER WAS "NUMBER"
(g) Map<?, ?> map7 = new HashMap<?, ?>();

This is a question i'm working on for a class that uses the SCJP study guide.

The ones marked with asterisks all have unchecked warnings when plugged into some code while the other ones all have errors when plugging into some code.

So I do not know what answers would be right then. What is right and what is wrong?


What were the warnings and errors that you saw? Do detail them for each option separately so that we can narrow down on the correct answers.

For option f, your assumption looks fine, that must be just a typo and you should consider that as "NUMBER".
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

What were the warnings and errors that you saw? Do detail them for each option separately so that we can narrow down on the correct answers.

Standard java compiler provided by sun generates warning only when a non-generic code tries to add something to the generic safe collection.
Exam supposes that you are using a standard java compiler and nothing else.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Praveen Kumar M K wrote: What were the warnings and errors that you saw? Do detail them for each option separately so that we can narrow down on the correct answers.

Standard java compiler provided by sun generates warning only when a non-generic code tries to add something to the generic safe collection.
Exam supposes that you are using a standard java compiler and nothing else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declarations and warnings.