Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt with equals()

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following code:

public class test {
public static void main (String[]args) {
String s1 = new String ("true");
Boolean b1 = new Boolean (true);
if (s1.equals(b1)) {
System.out.println("Equal");
}
}
}
options:
A The program runs and prints nothing.
B The program runs and prints "Equal."
C.An error at line 5 causes compilation to fail.
D.The program runs but aborts with an exception.


Ans: A

Doubt: Why,and also can we use equals for any class across any hierarchy?Here we are using equals() on Boolean and String class, similarly can we use for any two non-connected classes?
So should it not give a compile-time error?

pls help
regards,
gitesh

[ August 28, 2007: Message edited by: Gitesh Ramchandani ]
[ August 29, 2007: Message edited by: Gitesh Ramchandani ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not get what you meant by "A" . The comparison will not result in equal . They are not equal as the objects being compared belong to different classes . In this case the equals method in the String Class is being invoked . This will result false as the instanceof check fails.

From the String class in jdk 1.5_06
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your questions is not clear enough, you should list down the list of possible answers.

You can always use equals() for any objects because equals() takes Object and every class in java extends Object. Whether the equals() will return true or false is another different story and you shouldn't rely on the default implementation of the equals() method.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Freedy and Alok your replies are contradicting.

Meanwhile I have edited the post with options, pls help..

Gitesh
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Freddy says: The type of the parameter of the equals() method is Object. There is no compile-time error, because String and Boolean are both subclasses of Object. You can call the equals() method on any kind of object with any other kind of object as a parameter. There is nothing that restricts this so that you would get a compiler error.

Two objects of different types which are not related through an inheritance relationship, such as a String and a Boolean, are always different objects, so equals() will return false.

The equals() method of class String, which is called in the code above, most likely contains a check like this (if you really want you can look it up, the source code for the String class is available in src.zip in your JDK installation directory):
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everybody, doubt cleared.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic