• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Declarations and warnings.

 
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic