What is the difference between (1) == and equals that?

Java data types are divided into:

         Basic data types

         Complex data type (data type of package is the basic type of reference compound data types also belong)

Basic data types:

      byte,short,int,long,float,double,char,boolean 

Complex data types:

       Wrapper class: Byte, Short, Integer, Long, Float, Double, Character, Boolean

       对象:var obj ={"name":"xiaoming"}

       数组: var objarr = ["1","2","3"]

       Function: function f () {}

Turn: https: //blog.csdn.net/qq_35001600/article/details/82889515 

1) comparison between the basic data types and == comparison value, and address complex data type == when comparing two objects of value are the same, so unless it is with a new object, the result is true, otherwise false.

2) equals () Since the java classes inherit all the Object base class that defines an Object class equals (), the method initializes the memory address comparison target.

But override this method in some class, such as packaging String, Date, etc., the equals () comparing the values ​​of two variables.

·String

String objects created two ways to compare their equals and == have different effects

String s1=”sss”;

String s2=new String(“sss”);

System.out.println(s1.==s2);

System.out.println(s1.equals(s2));

Output: false

              true

The research results will need to look at the mechanics of creating String objects.

   

String s1 = "sss"; this process of creating in-memory representation is to create a "sss" literal constant pool in the method area, then after this address pointed by the stack created by the variable this way, it is as long as the value is true == between variables;

The String s2 = new String ( "sss"); this process is to create a concrete way in the heap to create a new object, and it is false when the value of the same variable == comparison, but as long as the same value, then .equals () or to true.

 

String There is also a special place is the result of splicing using the + and get consistent if the value of a variable value, two variables does not mean the same address, if splicing is a literal constant pool address points to the same value, two consistent variable address. But if the stitching of a variable is the equivalent of a new object address points to the heap, so even if the values ​​match, the address is not the same.

There is a final String intern () method.

 

The role of this method is that if there is the same value and the string constant pool checking, if there is, the constant value is returned, if the constant value is not created, and returns. Therefore, consistent values ​​and refer to the same variable constant pool address value () obtained by the same value and intern

For String simple summary:

① as long as no new target value generally equal to the same address

As a ②String immutable value, the operation of the string will create a new object.

Concerned about whether the new string object mosaic ③

 

 

 

· Packaging

When the "==" Comparative packaging and basic type, packaging automatically unpacking basic data types and compare. The literal address point constant pool, the same value to the same address.

Two objects were the same type of packaging equals () comparison, equals () will first compare the type, if they are the same type of comparison values, both at the same time set up to true

No two basic types equals () comparison method

Packing class called equals (), but the parameters are the basic types, this time will carry out automatic packing, into its basic types of packaging, first determine the type, and then determine the value, when both are true is true

Integer, for example in order to discuss the case about the direct assignment of two variables result == and equals () value:

Example: Integer i1 = 123;

Integer i2=123;

System.out.println(i1==i2);

Output: true

Because Integer object at compile automatically unpack (This feature after jdk1.5).

Example: Integer i1 = 200;

Integer i2=200;

System.out.println(i1==i2);

The output is: false

The demerit is very magical, and obviously the same as the previous example why the results are not the same. This is because the unpacking process automatically Integer: Integer i1 = 200 calls at compile-called valueOf () static method

 

 

It can be seen, cache is an array, the predetermined integer in the range [-127,128]. Beyond this value will create a new object in the heap memory, two objects of different addresses.

For Integer briefly summarize:

① anyway Integer variables are not equal and the newly created object.

②Integer automatically at compile time when unpacking == comparison.

 

Guess you like

Origin www.cnblogs.com/baimh/p/11220708.html