Java Learning Note 18

Source: Internet
Author: User

Objects is a new tool class in Java 7 that provides tools to manipulate objects, most of which are "null pointers" safe.


Objects implements the following methods:



In the source code, how Equals (Object,object) is defined in the Objects tool class:


public static Boolean Equals (object A, object B) {        return (a = = b) | | (A! = null && a.equals (b));    }

The above procedure is not difficult to find when two objects A, B are null, the return is also true, and guarantees that only one of the object A is not NULL


Compare object A To object B for equality. The above method can avoid nullpointerexception anomalies.


OK, let's take a look at the following programs:

Class User {    private String name;    private int age;        Public User (String Name,int age) {    this.name=name;    this.age=age;    }        Protected String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }        @Override public    String toString () {    return "name=" +name+ "\nage=" +age;    }} public class Main {private static user user1;private static user user2;public static void Main (string[] args) {System.out. println (Objects.equals (user1, user2)); System.out.println (User1.equals (User2));}}

Output Result:

True
Exception in thread "main" java.lang.NullPointerException
At Code2. Main.main (main.java:37)


When both User1 and user2 are null, the Equals (Object,object) method that executes objects gets true when the Equals (object) of object is executed.


Throws a NullPointerException exception.


Then let's look at how the Deepequals (object) method is defined in the source code:


   public static Boolean Deepequals (object A, object B) {        if (a = = B)            return true;        else if (a = = NULL | | b = = NULL)            return false;        else            return Arrays.deepequals0 (A, b);    }

In the above program, it can be seen that if two objects are equal, equal returns True, and vice versa, determine if one is null if one of the


Returns false if NULL, whereas the ARRAYS.DEEPEQUALS0 (A, B) method is executed. Let's look at how Arrays.deepequals0 (A, A, b) is real in the source code.


The current:

 Static Boolean DeepEquals0 (object e1, object E2) {assert e1! = NULL;        Boolean eq;        if (E1 instanceof object[] && E2 instanceof object[]) eq = deepequals ((object[]) e1, (object[]) E2);        else if (E1 instanceof byte[] && E2 instanceof byte[]) eq = equals ((byte[]) e1, (byte[]) E2);         else if (E1 instanceof short[] && E2 instanceof short[]) eq = equals ((short[]) e1, (short[]) E2);        else if (E1 instanceof int[] && E2 instanceof int[]) eq = equals ((int[]) e1, (int[]) E2);        else if (E1 instanceof long[] && E2 instanceof long[]) eq = equals ((long[]) e1, (long[]) E2);        else if (E1 instanceof char[] && E2 instanceof char[]) eq = equals ((char[]) e1, (char[]) E2);        else if (E1 instanceof float[] && E2 instanceof float[]) eq = equals ((float[]) e1, (float[]) E2); else if (E1 instanceof double[] && E2 instanceof double[]) eq = equals ((double[)) E1, (double[]) E2); else if (E1 instanceof boolean[] && E2 instanceof boolean[]) eq = equals ((boolean[]) e1, (boolean[]) E2        );        else EQ = e1.equals (E2);    return EQ; }

Let's take a look at the Assert keyword, in Java, where the Assert keyword represents an assertion. When an assertion is turned on (executionJava-ea Assertfoo, default is


Non-open:java assertfoo) The use of assert above is that when the Boolean expression following the Assert is true, the execution continues, if false, the process


The Assertionerror is thrown, and execution is terminated. Assert can also be used in the following format in addition to the above usage:


Assert<boolean expression >: < error message expression >;


Then its function is, when the following Boolean expression is true, continue execution, and vice-versa throws Java.lang. Assertionerror, and output the error message


Interest.


Using assert above ensures that E1 is not NULL, so that an exception is thrown when executed to Eq=e1.equsls (E2), and then seen in the IF statement using the


instanceof the keyword to determine if it is an instance of the same class, and if so, execute the corresponding if statement separately.


If if (E1 instanceof object[] && E2 instanceof object[] is true, the following methods are performed:


public static Boolean Deepequals (object[] A1, object[] A2) {        if (a1 = = A2)            return true;        if (a1 = = NULL | | a2==null)            return false;        int length = A1.length;        if (a2.length! = length)            return false;        for (int i = 0; i < length; i++) {            Object e1 = a1[i];            Object e2 = A2[i];            if (e1 = = E2)                continue;            if (e1 = = null)                return false;            Figure            out whether the elements is equal boolean EQ = DeepEquals0 (e1, E2);            if (!EQ)                return false;        }        return true;    }

In the program, the length of the two object array is judged, equal, and the for loop is passed through = = to determine whether their address


Equal, otherwise the DeepEquals0 (object, Object) method is executed to determine whether the contents of each object are equal in the heap.


Then let's look at the following methods for each of the above if statements:

public static Boolean Equals (Byte[] A, byte[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

public static Boolean Equals (Short[] A, short a2[]) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

public static Boolean Equals (Int[] A, int[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

public static Boolean Equals (Long[] A, long[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

public static Boolean Equals (Char[] A, char[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

public static Boolean Equals (Float[] A, float[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (Float.floattointbits (A[i])!=float.floattointbits (A2[i]))                return false;        return true;    }

public static Boolean Equals (Double[] A, double[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (Double.doubletolongbits (A[i])!=double.doubletolongbits (A2[i]))                return false;        return true;    }

public static Boolean Equals (Boolean[] A, boolean[] A2) {        if (A==A2)            return true;        if (A==null | | a2==null)            return false;        int length = A.length;        if (a2.length! = length)            return false;        for (int i=0; i<length; i++)            if (a[i]! = A2[i])                return false;        return true;    }

Now we can see that all of the above methods have a common point, which is to determine the length of the array and whether each element is the same. That is


The DeepEquals0 method is a deep comparison of the array.


We look at the following procedures:

public class Main {public static void main (string[] args) {int[] i_int1=new int[]{1,2,3,4};int[] i_int2=new int[]{1,2,3,4} ; System.out.println (Objects.deepequals (I_int1, I_int2)); int[] I_int3=new int[]{1,2,3,5};int[] i_int4=new int[]{ 1,2,3,4}; System.out.println (Objects.deepequals (I_int3, I_int4));}}

Through front facingThe results of the above procedure can be obtained quickly by introducing the DeepEquals0 method.

The output is:

True
False






Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43926545 Emotional Control _  

Java Learning Note 18

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.