In Java, everything is an object. Why are data types divided into basic types and objects?

In Java, everything is an object! Why are data types divided into: basic types and objects? Shouldn't there be only one type - object?

1. First of all, everything in Java is an object! This sentence is correct, because the eight basic types have corresponding wrapper classes (the wrapper class of int is Integer), and the wrapper class is naturally the object. The basic type has always been part of the Java language, which is mainly based on the consideration of program performance. The variables defined by the basic type are stored in the stack. For example int i=5;, Integer j=new Integer(10);j is just a reference to an object, which is stored in the stack, while the actual value is 10 It is placed in the heap, and the read and write speed of the heap is far less than that of the stack. Another is that the variables defined by the basic type are created and destroyed quickly, and the variables defined by the class need to be destroyed by the JVM.
2. In fact, java is not 100% object-oriented programming. For example, basic data types such as int, boolean, etc. are not objects, that is, they cannot be obtained by new, but java has solutions for these basic data types, that is, to A wrapper type, but their wrapper class is an object. For example, int corresponds to Integer, and boolean corresponds to Boolean. They are all used to solve the object-oriented use of basic data types.

[The difference between objects and basic data types]

基本数据类型在栈中进行分配,而对象类型在堆中进行分配。
所有方法的参数都是在传递引用而非本身的值(基本类型例外)。
对象之间的赋值只是传递引用,基本类型之间的赋值是创建新的拷贝。

For the value passing or reference passing, see Passing by reference or passing by value in Java
Note:
① For basic types, "==" and "!=" are comparing values. For objects, "==" and "!=" are comparing whether two references are the same.
②The equals method in the Java language is actually handed over to the developer to override, allowing the developer to define what conditions the two Objects satisfy are equal. So we can't simply say what equals compares. You want to know what the equals method of a class means is to look at the definition.

public static void main(String[] args) {
        Date a = new Date(1496620800000L);//创建对象a,L代表long int 
        Date b = new Date(1496620800000L);//创建对象b
        Date c=a;//将对象a赋值给c,只是传递引用,即对于非基本数据类型变量, = 操作是复制了变量的引用

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        System.out.println("a==b?  "+(a==b));
        System.out.println("a equals b? "+(a.equals(b)));
        System.out.println("c==a?  "+(c==a));

    }

The output is as follows

Mon Jun 05 08:00:00 CST 2017
Mon Jun 05 08:00:00 CST 2017
Mon Jun 05 08:00:00 CST 2017
a==b?  false
a equals b? true
c==a?  true

The simulation diagram is like this
write picture description here

For objects a and b, use to ==judge that the result is not equal, because for objects a and b, "==" is comparing whether the two references are the same, which is obviously not equal.
For objects a and b, use to equals()judge the result as and so on. Looking at the API, you can know that the equals() method in the Date class compares whether the long int values ​​returned by getTime() are equal.

two Date objects are equal if and only if 
the getTime method returns the same long value for both.

Objects a, c, and the ==result of judgment is etc., because for objects a, c, whether the two references are the same, obviously and so on.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476864&siteId=291194637