The main content is from MIT6.031 software construction course and hit software construction course.
There are two types of data in Java, which are divided into basic data types (primitive types) and object data types (objects types). For example, types such as int, Float,boolean, and so on are basic data types, and string, Integer, BigInteger and so on are object data types.
Here are the similarities and differences between the two points:
The base data types are immutable (immutable), while some of the object data types are mutable (mutable), some are not. The base data type is stored in the stack (stack), and the object data type is stored in the heap, which is managed by the Java garbage collection mechanism (garbage collector). Basic data types often do not implement complex representations, whereas object data types can represent complex concepts in the form of generics. The basic data types typically come at a very small cost and object data types are expensive.
The object data type also has a wrapper data class (Boxed primitive) that is encapsulated by the basic data type, such as Boolean, Integer,short,long,float. It is mainly used in collection. Generally do not take the initiative to use the packaging class, because the cost of doing so is very high. Requires the compiler to make an automatic conversion.
For example:
list<integer> list = new arraylist<integer> (); List.add (1); List.add (2);/////////list.add ( Integer.valueof (1)); List.add (Integer.valueof (2));
We usually choose the first form of writing, but this is actually a more expensive way of writing. Since 1 and 2 are not objects at all, the compiler is automatically converted to reduce efficiency. The following wording is a more efficient way of writing.
On the data types in Java