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 K&B 310-055 Page 547 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B 310-055 Page 547" Watch "K&B 310-055 Page 547" New topic
Author

K&B 310-055 Page 547

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Straight from our loving K&B book, page 547:


Why contains() method takes Object, even if I have parameterized my ArrayList with String. Only String can be added and we need not to cast what get(...) method returns, but why contains() ,indexOf(),remove() ask for Object.

indexOf() also requires Object
remove() also requires Object (another remove() asks for index though)

Please Help!!!


Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Hi chandra..!

You are right. We will never be able to add anything other than string in to this list.
But the same is not ture for the contains method.It can check for any thing other than the string.As you see the following prints false.




I know that you are going to ask me the question

"If it not taking in any thing other than string, why is it checking for some thing else?"


I am very eager to know the answer.


Regards,
Durga Prasad
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Good thought.

booleancontains(Object elem)
Returns true if this list contains the specified element.

If You observe the method it will not modify the List. Thats why input "Object" is sufficient.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi Srinivasan,
thats right, but when i'm making my collection typesafe, then when should i look for generalized things even though i'm sure they will never be there unless i mix this with legacy code.


thanks,<br />gaurav abbi
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
What I think, this matter is something related to legacy code.
All the methods (we are calling exception cases) come from the List interface, and List defines them as Object (asked parameter).


Not very sure but guessing it could be, not other reasonable cause coming to
my sleepy brain.





Regards,
cmbhatt
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
Question : if you are saying that the method should be like this boolean contains(String elem) instead of boolean contains(Object elem). Then the answer is backward compitiblity.


I am sorry, if i had misinterpreted the question. Tx.
[ April 21, 2007: Message edited by: Pankaj Patel ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Pankaj Patel,


we can have like this:
boolean contains(E ele){

}
But it would be redundant code doing nothing special and breaks legacy code bec'ze specific type given priority.
[ April 21, 2007: Message edited by: Srinivasan thoyyeti ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
In Map too, get method asks for Object.

Pankaj, I can agree with you to some extent if not completely. Why do only some methods suffer backward compatibility?

I find Map's get(...) method, asking for Object too.

I didn't get the satisfactory answer so far!!! :roll:


Srini says,
But it would be redundant code doing nothing special and breaks legacy code bec'ze specific type given priority.


Yeah Srini, your this statement seems good.

Thank you very much!

Regards,
cmbhatt
[ April 21, 2007: Message edited by: Chandra Bhatt ]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 347

I have an answer..but it would be nice to make you guys think.
Did you see the public boolean equals(Object o) method? Why does it takes an object as argument?
Apply same rules here too!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B 310-055 Page 547
 
Similar Threads
confusion about ArrayList
ArrayList clone question
Principle of "coding to an interface"?
collections doubt
KB page no 547