Java Why don't you need to create objects for basic data types?

  • 2021-07-16 02:37:10
  • OfStack

Java is an object-oriented language, that is, all objects! So why are data types divided into basic types and objects?

There are 8 basic data types in Java boolean , byte , short , char , int , flaot , long , double The basic data type is part 1 of the Java language, but the basic data type is not an object, the basic data type is on the stack, and the object is on the heap. The heap reads and writes far less quickly than the stack. If you use basic data types, it is equivalent to operating on the stack, and the creation and destruction of variables are very fast. On the contrary, if you define variables with classes, you need to operate in the heap, and the creation and destruction speed is slow.

For performance reasons, it is reasonable to do so in order to improve performance. But some places must use objects, and the basic data type is not objects. What should I do? Java provides wrapper classes for each of the basic data types, namely Boolean , Byte Wait. This solves the problem of object-oriented use of basic data types.

At the same time, it is the use of wrapper class that shows that Java is a pure OO language.

Summarize


Related articles: