Two Methods of the Arrays class in Java: deepEquals and equals

Source: Internet
Author: User

Two Methods of the Arrays class in Java: deepEquals and equals

DeepEquals and equals are two static methods of the Arrays class in Java, but what are the differences between them?

Code 1,

import java.util.Arrays;public class Test {public static void main(String[] args) {String[][] name1 = {{ "G","a","o" },{ "H","u","a","n"},{ "j","i","e"}};String[][] name2 = {{ "G","a","o" },{ "H","u","a","n"},{ "j","i","e"}};System.out.println(Arrays.equals(name1, name2));    // falseSystem.out.println(Arrays.deepEquals(name1, name2));// true}}
Code 2,
import java.util.Arrays;public class Test {public static void main(String[] args) {String[] name1 = {"G","a","o","H","u","a","n","j","i","e"};String[] name2 = {"G","a","o","H","u","a","n","j","i","e"};System.out.println(Arrays.equals(name1, name2));      // trueSystem.out.println(Arrays.deepEquals(name1, name2));  // true}}
Summary:

1. deepEquals is used to determine whether two specified arrays are deep equal to each other. This method applies to nested arrays of any depth.

2. equals is used to determine whether two arrays are equal. If the two arrays contain identical elements in the same order, true is returned. Otherwise, false is returned.

3. By comparing "code 1" and "Code 2", we can conclude that if the two arrays use equals to return true, deepEquals also returns true, that is to say, the comparison results of equals and deepEquals are no different on the premise that both arrays are one-dimensional arrays;

4. If you want to compare multiple objects to an array, you must use the deepEquals method;

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.