GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array List Problem 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 "Array List Problem" Watch "Array List Problem" New topic
Author

Array List Problem

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Problem :- Why ArrayList contains() method is behaving differently .
Please do explain.




Output:

false
false
true
true

SCJP 6.0 96%

(Connecting the Dots ....)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


The contains() method uses the equals() method to determine equality.

In the case of the Dog class, you didn't implement the equals() method, so equality is determined by the reference equality, inherited from the Object class. In the case of strings, it has a equals() method that returns true when the value of the strings are the same.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

this is due to the string immutability
when you are adding the object to the list then everytime you create a new dog object
then that new object is added to the list
when you are checking for the

then actually a new dog is being created and we have not added it to the list
hence this gives the output as false

when dealing with the strings
every String literal is put in the String pool
that is even if we create the new String object the string literal used for that is from the string pool
hence when you are checking for

the String literal "sahil" is already in the pool and also we have added it to the list in the statement


and when we check whether it exists in the list, then this "sahil" referenced by s1 is checked and the result is true
hope this helps

happy preparation


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Prasad Kharkar wrote:
when dealing with the strings
every String literal is put in the String pool
that is even if we create the new String object the string literal used for that is from the string pool


All strings that are compile time constants are placed into the string pool -- by code generated from the compiler. However, using the new operator on a compile time constant does not generate a compile time constant. Hence, the new string created with the new operator is *not* in the string pool (unless, of course, it is done programmatically via the intern() method).

Prasad Kharkar wrote:
hence when you are checking for

the String literal "sahil" is already in the pool and also we have added it to the list in the statement


and when we check whether it exists in the list, then this "sahil" referenced by s1 is checked and the result is true


This returns true as explained in my previous post. And while "sahil" is in the string pool, the object passed to the contains method is not -- and is a different reference from the object placed previously in the list.

Henry
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Thanks Henry Wong , I got you !!! Thanks a lot !!!

I would appreciate if you could give me some clue how to determine that something is compile time constant or not.

I mean , it is written in book that while using switch statement the argument to switch must be compile time constant.

eg:-
int x=2 \\ this is compile time constant.

eg2:- int x;
x=2;

\\ Now x is not compile time constant.

could you please tell me the clear cut distinction to determine the compile time constants.

Thanks
Cheers!!!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Henry Wong wrote:

All strings that are compile time constants are placed into the string pool -- by code generated from the compiler. However, using the new operator on a compile time constant does not generate a compile time constant. Hence, the new string created with the new operator is *not* in the string pool (unless, of course, it is done programmatically via the intern() method).


Henry


If I create a String as,

String newS = new String("JAVA").

I think, the String Literal JAVA will be in the String Literal Pool, and the normal object heap! And the reference variable newS will refer the String object in the heap, not in the String Literal Pool!

Please Confirm Please, Thanks in Advanced!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Rally wrote:Thanks Henry Wong , I got you !!! Thanks a lot !!!

I would appreciate if you could give me some clue how to determine that something is compile time constant or not.

I mean , it is written in book that while using switch statement the argument to switch must be compile time constant.

eg:-
int x=2 \\ this is compile time constant.

eg2:- int x;
x=2;

\\ Now x is not compile time constant.

could you please tell me the clear cut distinction to determine the compile time constants.

Thanks
Cheers!!!


There are two things in Constants, Compile time Constants and just Constants!

With the final keyword, It's a constant, but not a compile time constant.

Compile time constants, known the value in the compile time, Example



Hope you understood!
 
GeeCON Prague 2014
 
subject: Array List Problem