In Java, we use the = = and. Equals () When comparing two data types, but what is the difference between the two? Let's look at some examples below.
Package Test;public class Test {public static void main (string[] args) {//TODO auto-generated method stubstring A = "1"; S Tring B = "1";//int a = 1;//int B = 1;if (A = = b) {System.out.println ("= =");} if (A.equals (b)) {System.out.println (". Equal ()");}}}
both = = and. Equals () are output, but when we change the data type of AB to String, the. Equals () method will give an error. What is this for?
For the basic data type (Byte,short,char,int,long,float,double,boolean), it is necessary to use = = To determine whether the two variables are equal.
For non-basic data types, the. Equals () and = = can be used essentially to compare the same reference for two objects. Just. Equals () is overridden in these classes by string,integer,date to determine whether the specific storage content is equal, not the memory address.
The difference between = = and. Equals () in Java