Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List Problem

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem :- Why ArrayList contains() method is behaving differently .
Please do explain.




Output:

false
false
true
true
 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic