aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KB page no 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 "KB page no 547" Watch "KB page no 547" New topic
Author

KB page no 547

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Hi all,

Please explin me why
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));

line one priting true and line 2 false.

Thanks in advance
Suma
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

for those of use who do not have the book, could you elaborate a little more on the code?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
...
List<String> test = new ArrayList<String>();
String s = "hi";
test.add("string");
test.add(s);
test.add(s+s);
System.out.println(test.size());
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));
test.remove("hi");
System.out.println(test.size());
...

size() is called, and result, 3, is printed;
"string", "hi", and "hihi" are added to the list;
contains(42) is called, and the result, false, is printed;
contains("hihi") is called, and the result, true, is printed;
remove("hi") removes middle element;
size() is called and result, 2, is printed.


SCJP 88% | SCWCD 84%
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
List<String> test = new ArrayList<String>();
System.out.println(test.contains(42));


Here test is a list reference can only contain the Strings.
When what is the use of checking other types ( here 42 Integer) in test by using contains method?
why API written for contains method accepting the object of type 'Object'.
Can any one calrify me about this?

rami
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KB page no 547