Java based learning: the difference 4.Java basic data types and == and the equals method

Reprinted Source: https://www.cnblogs.com/Latiny/p/8099581.html (Daniel's interpretation, reading plainly)

The basic difference between Java data types and == and the equals method

1 basic data types

byte : the Java smallest data type in memory representing one byte (8 bit), in the range -128 to 127, default value of 0

Short : short integer, two bytes (16 bit), the range from -32768 to 32717, a default value of 0

int : integer, for storing integer 4 bytes in memory, the range -2147483648 to 2147483647, the default value of 0

Long : Long, 8 bytes -2 ^ 63 to 2 ^ 63-1, default values in memory 0L

float : float, 4 bytes in memory, for storing a decimal point number (float and double difference is only 6 ~ Type Valid decimal 7), the default value 0

Double : double precision floating point, for digital storage with a decimal point, 8 bytes in memory, a default value of 0

 

char : character, for storing a single character, the memory 2 bytes, in the range 0 to 65535. The default value is empty

Boolean : Boolean type, 1 byte for judging true or false (only two values, i.e. to true, false), the default value of false

 

2 reference types

Java language itself in C ++ structure (struct) is not supported or in combination (Union) data type, its complex data type is generally constructed by a class or interface class provides binding data and methods manner, while for the external program information hiding. 3 types of reference type: class, interface, array;

Class (Class): All classes, whether it is Java itself already exists, then the user or created;

Objec categories: it is the parent class of all classes, each class implementation of this class; all classes can be defined by Object;

String: String class represents character strings, Java

Interface (interface):  The system comes with user-created or

Array (array):  The system comes with user-created or

 

3 Java data types stored in the memory of principle

Java type of data storage principle to a local variable or variables to distinguish members according to their local variables and member variables to distinguish the position defined variables:

Local variables: variable method;

Member variables: the outer class methods;

 

Local variable data type to store principle:

Storage principle (1) basic data types: all simple data types concept of "references" does not exist, all the basic data types, the value of the data itself is stored directly on the stack memory is stored in the memory stack space inside, Java is a language which eight kinds of data types such storage model;

Storage principle (2) reference types: Reference types inherit from the Object class (also a reference type) are in accordance with the Java memory model inside the storage object for data storage, the use of Java memory heap and stack memory to perform this type of data storage Briefly, "reference" (stored object in memory heap address) is stored on the stack memory ordered, and the value itself is stored in the object memory heap;

The basic difference between the data and reference types of basic data types are mainly allocated on the stack, and reference types are allocated on the heap

for example

We analyze the difference between == and the equals () of

First, define two array of String objects

String[] a={"latiny1","latiny2"};
String[] b={"latiny1","latiny2"};

然后比较
if(a==b)
{
System.out.println("a==b");
}
else
{
System.out.println("a!=b");
}

Program outputs a! = B

Reasons: a and b are not the same address, a == b is the address comparing two variables, the address data a and b are not the same

For local variables, whether it is the basic data type or reference type, they will allocate a block of memory on the stack, the basic types, this region contains the basic types of content; and for reference types, this block area contains a pointer pointing to the real content, a real content being manually allocated on the heap.

 

For members of the variables, whether it is the basic data type or reference type, they are stored in the heap memory area or method; static member variables can be divided into ordinary member variables and member variables, static member variables class belong to the class, the class can be directly access data stored on your area; ordinary members of the variables that belong to the class object, then the object must be declared in order to access through objects stored on the heap.

 

4 basic types of conversion

(1) automatic conversion

All values ​​Java type variables each can be converted, if a certain value of the basic type system of the directly assigned Another basic type of variable, this manner is referred to as automatic conversion.

you e = 1;

double d =e;

The above code does not complain, the system will automatically convert.

(2) cast

When the range of a large value or a small range of variables assigned to another variable, needs to be cast.

  double d =1.5;

  you e = d;

  The code above will get an error, the value assigned to the variable a wide range of small-scale, need to cast, so as to compile, and a wide range of values ​​lose precision.

  you e = (you) d;

 

== with equals 5

  Determining whether two variables are equal, there are two ways: one is to use == operator, the other is using the equals method.

  ①  Comparison of java basic types:

  Comparison with only basic types, "==", can not be "the equals", where "==" is a value comparison of two basic types;

  ②  Comparison of packaging: Here take Integer, Character exemplified

Character a = new Character('A');
        Character b = new Character('A');
        
        System.out.println(a == b);
        System.out.println(a.equals(b));
        
        Integer i1 = new Integer(10);
        Integer i2 = new Integer(10);
        
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));

  Output: 

false
true
false
true

  Here "==" to compare the memory address of the object, new address two different objects are stored is not the same, here's "equals" comparison value is, why there is a comparison of the value of it, equals in the rewrite of the method equals. Attach Source:

public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

 

  ③ Comparison of String:

  "==" to compare the memory address, "equals" comparison of the value

        String s1 = "latiny";
        String s2 = "latiny";
        
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));

        String s3 = new String("latiny");
        String s4 = new String("latiny");
        
        System.out.println(s3 == s4);
        System.out.println(s3.equals(s4));

  Output:

true
true
false
true

  Additional information: the difference between Java String and new String () of

  Stack area and stored reference basic types, can not store an object, the object and the heap memory area. == is a comparison address, equals () comparison content.

(1)  String str1 = "abcd" implementation process : First, the stack area to create str reference, then the String pool (independent of the stack and heap exist, storage is not variable) to find that the content they point to "abcd" object, string pool, if not, create one, then point to the object str string pool, if any, directly str1 point to "abcd" "; if later define a string variable str2 =" abcd ", then directly to str2 String pool reference point to the already existing "abcd", not re-create the object; if str1 been assigned (str1 = "abc"), the str1 will no longer point "abcd", but refers to the re-String pool " abc ", if this time is defined string str3 =" abc ", performed str1 == str3 operation, the return value is true, because they are the same value, the same address, but if the content is" abc "is performed string str1 + connecting str1 = str1 + "d"; str1 points at this time in the stack is the new content is "abcd" objects, i.e. at this time str1 == str2, the return value of false, because the address is not the same.

Note: If a String str5 = "abc" + "d", for str1 == str5 operation, the return value is true. This is because two or more string constant sum, "+" will be optimized in the pre-compile time, corresponding to the two or more automated synthesizer string constants a string constant; and the string + the operation is essentially a new StringBulider object append operation, after calling toString splice () returns a String object.

Reference: https://blog.csdn.net/u010775025/article/details/86507090?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

(2) String str3 = new String ( "abcd") implementation process : directly create objects on the heap. If Subsequently, String str4 = new String ( "abcd "), str4 not point to objects before, but to re-create an object and point to it, so if at this time str3 == str4 return value is false, since both address of the object is not the same, if it is str3.equals (str4), returns true, because the same content.

 

  ④ comparison:

  Here "==" to compare the memory address, "equals" comparison also address equals method does not override the equals method of the Object class is called.

         Person p1 = new Person(1001, "latiny1");
         Person p2 = new Person(1001, "latiny1");
         
         System.out.println(p1 == p2);
         System.out.println(p1.equals(p2));

  Output:

false
false

Published 33 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41623154/article/details/104819476