Question

The class Object defines an equal() method to compare objects. Specify the advantages and disadvantages of...

The class Object defines an equal() method to compare objects.

Specify the advantages and disadvantages of using this method

and suggest an alternative for equality.

Illustrate with code examples.

(Please elaborate on the advantages and disadvantages; also please include your sources and references) Thanks a lot!

Homework Answers

Answer #1

Consider this code:

String x = "Hello World";
String y = "Hello " + "World";
if (x == y)
System.out.println("The strings are ==");
if (x.equals(y))
System.out.println("The strings are equal");

The first will not print, but the second one will. This is because the two Strings are not the same object, even though they contain the same content.

The .equals() method, on the other hand, is smart enough to check the contents. It sees that both contain "Hello World" so it returns true.

If you did this:

String x = "Hello World";
String y = x;
if (x == y)
System.out.println("The strings are ==");

then it would print, because x and y would actually be the same object.

Here's the details as to why this is.

Deep down, if you have a primitive variable (like int), the value is stored in the variable. If you have int j = 8; then 8 is stored in the j variable. And if you then have int k = 8; then k will also have 8 stored in it. So if you use == on them, they will show as equal.

But if you have an object (like a String) then what is actually stored in the variable is a memory location which contains the rest of the object. So if you have String x = "Hello World"; then x does not *really* contain "Hello World" - it contains something like 0x13F70000, which is a memory location which then contains "Hello World". Then if you have String y = "Hello " + "World"; it's going to create a new memory location and also put "Hello World" in it. But now y is maybe 0x13F70040. It contains a different value than the x variable, so == will see them as not equal.

(And if you're wondering "well, why doesn't Java just share that memory location so both variables can use it" - sometimes it does, for Strings. That's known as string interning. So if you have String x = "Hello World"; and String y = "Hello World"; then the compiler might recognize that they are the same and cause them to use the same memory location, and == might return true.)

Alternative:

Using compare() method

In Java for locale specific comparison, one should use Collator class which is in java.text package. The one most important feature of Collator class is the ability to define our own custom comparison rules.

References- https://www.geeksforgeeks.org/java-equals-compareto-equalsignorecase-and-compare/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects...
create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects to the same array. Then, because the different object types are stored in the same array, you can easily manipulate them by using a for loop. 1. Open a new file, and then enter the following first few lines of the VehicleDatabase program: import javax.swing.*; public class VehicleDatabase { public static void main(String[] args) { 2. Create the following array of five Vehicle references...
Write a Python class Matrix which defines a two-by-two matrix with float entries as a Python...
Write a Python class Matrix which defines a two-by-two matrix with float entries as a Python object. The class Matrix has to be able to display the object on the screen when the print function is called, and it should include methods determinant(), trace(), inverse(), characteristic_polynomial(), and matrix_product(). Furthermore, a user should be able to multiply the matrix by a constant and be able to add and subtract two matrices using the usual symbols + and -. Use the following...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
Python recursive design question: Background: The class vote creates vote objects. A vote is either a...
Python recursive design question: Background: The class vote creates vote objects. A vote is either a blue vote, a red vote, or a purple. This information is stored in the "value" attribute. The function Poll returns a list of random votes. In the code below, some_poll is a list of 32 random votes. some_poll = Poll(32) # ----------------------------------------------------- import random # ----------------------------------------------------- # # Return a list of n random votes # # ----------------------------------------------------- def Poll(n=16): # # A random...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
1.Establishing the virtual Management: As known, managing virtual staff requires a different method or approach than...
1.Establishing the virtual Management: As known, managing virtual staff requires a different method or approach than managing local staff. Due to that reason, Golden Scent has developed a strategic plan to successfully manage its virtual staff in the USA. Identify the suitable manager. to make sure our work will proceed as we planned, Golden Scent willrecruit a virtual manager with the essential skills and knowledge required to manage virtual employees. Find the skilled people to work with. Since not everyone...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included Download Lab04.zip for all of the additional supporting files that you will need to compile and run. Extract the files into your working directory You can find the rules of blackjack all over the Internet. To get an idea of what you are trying to accomplish in this lab, I’ll demonstrate the final solution. The dealer stands on all 17s. Doubling after splitting is...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...