• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest looking into enhanced for() loops. They're much cleaner.
Becomes...

 
srikant nair
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Produces this output:

So, no, it doesn't default to String.
 
Carey Brown
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So which equals method is being used in the above code
and why?
 
Carey Brown
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79275
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic