2021-04-15-Fabric1.4 multi-channel experiment soulmate

2023-01-23   ES  

First we write a piece of code to compare two objects

public class test { 
 Public Static void main (string [] args) { 
 String name2 = new string ("tom"); // Create name1 assignment TOM 
 String name2 = new string ("tom"); // Create name2 assignment TOM 
 System.out.println (name1 == name2); // Use "==" to determine whether the two objects are the same 
 System.out.println (name1.equals (name2)); // Use the equals method to determine whether the same 

We all know that when “==” to determine the two reference type objects, it directly judges the address of the object. That is to say, different objects are judged by “==”. Equals are different, the output here is

Obviously the EQUALS method is not just a look at the address, so how does it execute it?

Public Boolean Equals (Object Anobject) {{ 
         if (this == AnObject) {// "==" to determine whether the address is the same 
             Return true; 
         if (AnObject Instanceof String) {// If the comparison object is String 
             String anotherString = (String) AnObject; // Because the anObject parameter points to the name2 object 
                                                     // name2 is the string type object, so you can transform 

             int n = value.length; 
             // Because Value is a global variable, it is essentially this.value.length 
             // name1 call equals, this is name1, name1 is global variable, this.value.length is the character of name1. 
             if (n == Anotherstring.value.length) { 
             // Anothertring is pointing to name2, anotherString.value.Length represents how many characters have. 
             // Two string is the same, the length must be the same, so n == Anothertring.value.length is very important 
                 char v1 [] = value; // array consisting of name1 character 
                 char v2 [] = Anothertring.value; // array consisting of name2 character 
                 int i = 0; 
                 While (n-! = 0) {// Comparison of characters one by one 
                     if (v1 [i]! = v2 [i]) // As long as one comparison fails, two string must be different 
                         Return false; 
                     i ++; 
             // This code actually creates a string that is exactly the same as the comparison object, and the characters are compared with the characters 
             // The elephant is compared. If all the same, the two objects are the same, otherwise different 
                 Return true; 
         Return false; 

By analyzing, we can find that the Equals method actually uses “==” to first judge the address. When the address is different, they will not immediately think that the two objects are different. Instead, when the object is string type type Compare their characters one by one, so we can use the EQUALS method at the same time when judging a few sentences to avoid errors brought by “==” judgment.


Related Posts

Transform, Translate, Translaton, Animation Animation

tinyxml analysis

vSCode related shortcut keys A

SpringBoot+MyBatisplus Multi -Dent Source Configuration, Main Study Read the Writing and Write Complete Explanation

2021-04-15-Fabric1.4 multi-channel experiment soulmate

Random Posts

Tomcat configuration https

Android booting the bottom layer optimization

Socket server Bind Bind Return Error: Invalid Argument

Unity extended Editor menu: Provide a unified interface to allow the planning to regulate the values of related scripts in Prefab Steven

FreertOS’s understanding of the priority macro