cool hit counter The difference between equals and == in Java_Intefrankly

The difference between equals and == in Java


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/43313353

We all know that data types in Java can be broadly classified into two categories: basic data types and reference data types. Both equals and == are used to compare data with data to see if they are "equal".

Comparison of basic data types

Comparisons between basic data types are made with "==", which is used to determine if their values are equal.

Example code.

public class Test{
	public static void main(String[] args){
		int i1=9;
		int i2=9;
		boolean b1=true;
		boolean b2=true;	
		char c1='c';
		char c2='c';
		System.out.println(i1==i2);    //The output istrue
		System.out.println(b1==b2);    //The output istrue
		System.out.println(c1==c2);    //The output istrue
	}
}

Comparisons between basic data types do not use equals.

Comparison of referenced data types

Comparisons between reference types can be made both with "==" and with equals.

Use "==" to compare reference objects

When comparing reference objects with "==", you are comparing their addresses, i.e. the values of variables stored on the stack.

public class Test{
        public static void main(String[] args){
                Person p1=new Person("Danny",24);
                Person p2=p1;
                Person p3=new Person("Danny",24);
                System.out.println(p1==p2);  //The output istrue
                System.out.println(p1==p3);  //The output isfalse
        }
}
class Person{
        public String name;
        public int age;
        public Person(String name,int age){
        this.name=name;
        this.age=age;
        }
}

The memory map at this point is.

Both p1 and p2 point to the same object, so the addresses stored in p1 and p2 are the same, and "==" compares exactly the values they store on the stack, so the value of p1==p2 is True; The objects pointed to by p3 and p1 are not the same object, although they are identical, so p1!=p3 (or p1==p3==false).

special circumstances.

For String and Integer, the object is created differently and the result of the comparison with "==" is different, for example.

public class Test{
	public static void main(String[] args){
		String a1="hello";
		String a2="hello";
		String a3=new String("world");
		String a4=new String("world");
		System.out.println(a1==a2);    //The output istrue
		System.out.println(a3==a4);    //The output isfalse
	}
}

Using equals to compare reference types

The equals method is defined in the base class Object, and this method also compares the memory address of the object, i.e., if you replace "p1==p2" with p1.equals(p2) in the above example, the result will be the same.

special circumstances.

If a class doesn't define its own equals method, its default equals method (inherited from the Object class) is to use the == operator, which also compares whether two variables point to the same object, when using equals will give you the same result as using ==.

However, some classes override methods in the Object class, in which case it compares whether the contents of the referred objects are the same. For example.

public class Test{
	public static void main(String[] args){
		String a1="hello";
		String a2="hello";
		String a3=new String("hello");
		String a4=new String("hello");
		System.out.println(a1==a2);    //The output istrue
		System.out.println(a3==a4);    //The output isfalse
		System.out.println(a1.equals(a4));    //The output istrue		
	}
}
class Person{
	public String name;
	public int age;
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
}

The a1 and a4 above obviously don't point to the same object, but they refer to the same content in the object, so the value of a1.equals(a4) is true.


Recommended>>
1、Microsoft Bing international version released in China the introduction of AI focus on English search
2、Node Part 4 Integrating AJAX and Cookies for Login
3、Chinas first public welfare blockchain project Care Chain debuts at the Shanghai Blockchain Innovation Summit with Block Continental providing full technical support
4、This tool lets you blast global cameras in five minutes
5、Issue 80 The Story of Nangong Yuans Personal Advisor Qingfeng below

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号