Question

In: Computer Science

Comparing Objects Vs Comparing Primitive data Types Topic: Discuss how do we compare Strings for equality?...

Comparing Objects Vs Comparing Primitive data Types

Topic:

Discuss how do we compare Strings for equality? Provide coding examples. How String comparison is different than the primitive data type comparison? Please cite any resources you used.

Solutions

Expert Solution

Comparing Objects Vs Comparing Primitive data Types :

In java we can compare object by using equals() method . equals() method is available in object class. It will use for check the equality of two object. If two object will be store in same memory location then both object is equal.

Syntax:   

            Public boolean equals(Object ob);

In this syntax - public is a access modifier and boolean is a return type it means it return either true or false. And equals is a method name. And it takes object as a parameter and return true if object is equal otherwise return false.

Note: Object class is a parent class of all class so it can hold the reference of all class.

   Primitive data types: in java we have to declare all variables to before use because java is statically types programming language. We can compare primitive variable by using == operators.

  

An example of == operator:       

   

Int a=10;

Int b=a;

If(a==b)

{

System.out.println(“True”);// it return true

}

Else

{

System.out.println(“False”);

}

Output- True

How to compare String -   we can compare two string by using equals() method as well as using == operator . we have to override equals() method in our program. equals() method compare content of any string and == operator compare address of the string.

Program for String comparison:

   public class StringComparision {
public static void main(String[] args) {
   String s1="abc";
   String s2="xyz";
   String s3="abc";
   System.out.println(s1==s2);// it return false because address of both string is different
System.out.println(s1.equals(s3));//it return true because content of both string is same
}
}

Output:- false

true

Note: in string if we make object using new keyword then two object is created on in SCP(String constant pool) and second is in heap.

How String comparison is different than the primitive data type comparison:

If we compare primitive variable then it can be done simple by using == operators but if we want to compare Strings then we have to override equals() method in our program. So we can say string comparison is difficult then primitive variable comparison.


Related Solutions

We see that data structures, collections, and abstract data types are closely related concepts. Objects that...
We see that data structures, collections, and abstract data types are closely related concepts. Objects that are contained within these entities are typically called items or elements. From the object-oriented perspective, data structures have both private data and public behaviors. The behaviors define data structures in an abstract, implementation independent manner. Other equivalent terms used for behaviors are operations, methods, public interface, and usage interface. In this discussion, we explore the primary behaviors needed to use data structures. - Remove...
Why do you think Java provides both primitive data types and wrapper classes for them? Why...
Why do you think Java provides both primitive data types and wrapper classes for them? Why not just one or the other?
Who are Open-Panel Model HMO's competitors and how do they compare in market, membership, assets, equality,...
Who are Open-Panel Model HMO's competitors and how do they compare in market, membership, assets, equality, and profit?
Different types of software can be used for data management. Compare Excel and SPSS and discuss...
Different types of software can be used for data management. Compare Excel and SPSS and discuss specific SPSS software features that make it preferable to Excel for data management. Provide examples illustrating when electing to use SPSS could be preferable to Excel and vice versa.
how do we interpret how typical a score is compare to the population based on the...
how do we interpret how typical a score is compare to the population based on the population mean and standard deviation
Compare and contrast the muscular anatomy of annelida vs nematodes. How do each of thse groups...
Compare and contrast the muscular anatomy of annelida vs nematodes. How do each of thse groups use their muscular systems to effect movement/locomotion?
ABC vs. PROCESS COSTING & JOB-ORDER COSTING How do the three compare? What are the advantages,...
ABC vs. PROCESS COSTING & JOB-ORDER COSTING How do the three compare? What are the advantages, disadvantages, and applicability for each?
Discuss how we form opinions of others. How do we attempt to manage and control their...
Discuss how we form opinions of others. How do we attempt to manage and control their opinions of us?
The topic is Corona viruses and COVID-19. What is the problem? How do we know there...
The topic is Corona viruses and COVID-19. What is the problem? How do we know there is a problem (what evidence is there)? What causes the problem? What are the possible future effects of the problem? How will it affect the world? What is being done to solve the problem? Are there laws related to the topic? What can an individual do to help?
Compare a data warehouse used for decision support to an operational database. How do the data...
Compare a data warehouse used for decision support to an operational database. How do the data warehouse and the operational database differ in their purpose, their structure, the technology they use, and the way they are managed? Why would a company need both?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT