Equations in Java

Source: Internet
Author: User
Comparing the equality of the original type is different from comparing two objects. If the value 5 is stored in two different int variables, comparing whether the two variables are equal will result in a Boolean value true:

public class TestIntComparison {  public static void main(String[] args) {  int x = 5, y = 5;  System.out.println(    "x == y yields " + (x == y));  }}

Testintcomparison generates the following output:

D:/>java TestIntComparisonx == y yields true   

Equal operators compare their values in the original type. When used for an object, the reference of the object is compared with the actual content of the object. You may ask, "do all these references point to the same object? "For clarity, see another version of dog that only contains tags and age:

  class Dog {  int tag;  int age;  public void setTag(int t) {tag=t;}  public void setAge(int a) {age=a;}}   

If two dogs have the same content, they are not equal when the = Operator is used. The output of the following code segment indicates that A and B are not equal when "=" is used:

Dog a = new Dog();a.setTag(23129);a.setAge(7);Dog b = new Dog();b.setTag(23129);b.setAge(7);if ( a==b ) {  System.out.println("a is equal to b");}else {  System.out.println("a is not equal to b");}   

So how should we compare the values of two objects instead of comparing their references? The Java (TM) programming language has a convention that equals () is used to define equal object values. The equals () method is defined in the class object. If it is not overloaded in its subclass, it is used by default. To compare the values of dog a and B, you should rewrite the comparison section above:

if ( a.equals(b) ) {  System.out.println("a is equals() to b");}else {  System.out.println("a is not equals() to b");}   

In the above Code, if the equals () method is not overloaded in the dog, the two dogs are still not equal. Because object. Equals () is actually simulating the = Operator function. The equals () Definition in dog is very understandable:

 class Dog {  int tag;  int age;  public void setTag(int t) {tag=t;}  public void setAge(int a) {age=a;}  public boolean equals(Object o) {    Dog d = (Dog)o;    if ( tag==d.tag && age==d.age ) {      return true;    }    return false;  }}   

Why is the parameter type of equals () object rather than dog? Because you are reloading the method equals () of the parent class object, you must mark it with the same method. However, the parameter we want to pass in is another dog, so in order to be able to access the parameter field, we need to convert its type to dog.

However, since equals () is defined in dog, you must check whether the input object is a dog because someone may use the following code:

 fido.equals("blort");   

The string "blort" is also an object, so it matches the equals () tag in dog. The correct method of equals () is:

 public boolean equals(Object o) {  if ( o instanceof Dog ) {    Dog d = (Dog)o;    if ( tag==d.tag && age==d.age ) {      return true;    }  }  // false if not Dog or contents mismatched  return false;}   

The instanceof operator asks whether o is an instance of a dog (including a subclass of dog.

String comparison introduces the last problem of object comparison, that is

"ABC" = "def"

Is the expression true or false? Is false, because they are essentially different objects (obviously, their content is different ). However, the following expression

"ABC" = "ABC"

Is it true or false? Unfortunately, this is determined by the compiler. If the compiler optimizes two references to "ABC" into one object instead of two objects, the expression value is true. However, if the compiler does not perform this optimization, the expression value should be false!

If you really want to determine whether two strings are physically the same object, use the equals () method:

boolean b = "abc".equals("def"); // falseboolean c = "abc".equals("abc"); // true   
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.