Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on Generics (from Jeanne Boyarsky's OCP book)  RSS feed

 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anybody explain the below example from (3rd chapter/18th question). I am actually preparing the OCP right now so please don't irritate. I may come multiple times with other doubts and finally with java 8 certification with a month

Anyways, doubt is regarding the below question.
Which of the following lines can be inserted to make the code compile? (Choose all that apply). Even after the explanation given in the Answers section, I didn't get it.


Thanks,
Vicky.
 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky Roy wrote:Hi,
Can anybody explain the below example from (3rd chapter/18th question). I am actually preparing the OCP right now so please don't irritate. I may come multiple times with other doubts and finally with java 8 certification with a month

Anyways, doubt is regarding the below question.
Which of the following lines can be inserted to make the code compile? (Choose all that apply). Even after the explanation given in the Answers section, I didn't get it.


Thanks,
Vicky.


Please allow me to insert another question:
        Map m = new HashMap();
    m.put(123, "456");
    m.put("abc", "def");
    System.out.println(m.containsKey("123"));//why it is false?

 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky Roy wrote:
Vicky Roy wrote:Hi,
Can anybody explain the below example from (3rd chapter/18th question). I am actually preparing the OCP right now so please don't irritate. I may come multiple times with other doubts and finally with java 8 certification with a month

Anyways, doubt is regarding the below question.
Which of the following lines can be inserted to make the code compile? (Choose all that apply). Even after the explanation given in the Answers section, I didn't get it.


Thanks,
Vicky.


Please allow me to insert another question:
        Map m = new HashMap();
    m.put(123, "456");
    m.put("abc", "def");
    System.out.println(m.containsKey("123"));//why it is false?



For later one, what I think it will be like new Integer(123).equals("123") and it is false... Please let me know if my understanding is wrong.
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your second question you are comparing a String with an Integer (containsKey uses the equals-method of the provided key) and String.equals(Object) returns false for any non-String object.
As a result, it behaves like:
 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:In your second question you are comparing a String with an Integer (containsKey uses the equals-method of the provided key) and String.equals(Object) returns false for any non-String object.
As a result, it behaves like:


Thanks. And could you please explain 1st scenario?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky Roy wrote:Even after the explanation given in the Answers section, I didn't get it.


Perhaps it would help us if you elaborate what is confusing you. What part of the explanation you "didn't get"? And why?

Henry
 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Vicky Roy wrote:Even after the explanation given in the Answers section, I didn't get it.


Perhaps it would help us if you elaborate what is confusing you. What part of the explanation you "didn't get"? And why?

Henry


Role of C is confusing since it is Class as well as type parameter.
 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody!!!
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky Roy wrote:Nobody!!!
So if in No 4, you pass a C, how do you know whether it can accept an A? In No 6, how do you know that the actual type parameter will have a no‑arguments constructor? What was the answer suggested? And the xplanation given.

Don't quote the whole of preceding posts; that simply makes the thread longer without adding new information.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!