• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object class  RSS feed

 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,





In this object class equals method is executed .But that is meant for reference comparison
  it.remove statement gets executed


Why is this happening .I am confused



OutPut

[sri, nivas, dande, JLC]
sri
[sri, nivas, dande, JLC]
nivas
[sri, nivas, dande, JLC]
dande
[sri, nivas, dande, JLC]
JLC
[sri, nivas, dande, JLC]
 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry this is the output



[sri, nivas, dande, JLC]
sri
nivas
[nivas, dande, JLC]
dande
[nivas, dande, JLC]
JLC
[nivas, dande, JLC]
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Object referenced by obj is a String, so the String equals is the method being called.

As a tip, when posting code use code tags.
Highlight the code and click the "Code" button.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare the types instead of using Object it works. Type declaration helps the compiler catch some coding errors.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest looking into enhanced for() loops. They're much cleaner.
Becomes...

 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Object referenced by obj is a String, so the String equals is the method being called



How Did it convert to String Class
 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Sri
{
    public static void main( String args[] )
    {
        Collection col = new ArrayList(); // DECLARE TYPE

        col.add( 10 );
        col.add( 39 );
        col.add( 50 );
        col.add( 60);
        
        System.out.println( col );
        Iterator it = col.iterator(); // DECLARE TYPE
        while( it.hasNext() )
        {
            Object obj = it.next(); // DECLARE TYPE
            System.out.println( obj );
            if( obj.equals( 10 ) )
            {
                it.remove();
            }
            else
            {
                System.out.println( col );
            }
        }
    }
}


If you look at this here also equals method is of String class
Is it by default objects are of String Type
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Produces this output:

So, no, it doesn't default to String.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Autoboxing Tutorial
Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

Primitive typeWrapper class
booleanBoolean
byteByte
charCharacter
longLong
doubleDouble
shortShort
floatFloat
intInteger


 
srikant nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So which equals method is being used in the above code
and why?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This creates and adds an Integer object.

Because the Collection now contains this Integer, when you retrieve it into "obj" it is still an Integer, so when you call its equals() method it also converts this 10 into an Integer and then compares them.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikant nair wrote:So which equals method is being used in the above code
and why?
Polymorphism. The version of the instance method called depends on the runtime type of the object, and nothing else. If you create a String, the String version will be used. Remember that polymorphism only applies to instance methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!