Java data types are classified into the underlying data type, the reference data type, and the two categories.
The underlying data types are 8 kinds: byte, short, int, long, float, double, char, Boolean
Reference data types include 3: Object (Class), Interface (interface), array (array)
Graphic:
Java Data type diagram:
┏ numeric ━┳━ integer type: byte short int long
┏ basic Data type ━━┫ ┗━ floating-point type: float double
┃ ┣ character type: Char
Data type ╋ ┗ Boolean: Boolean
┃ ┏ Classes (Class)
┗ Reference data Type ━━╋ Interface (interface)
┗ Arrays (Array)
Different storage locations:
stacks and heaps are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.
The Java heap is a run-time data area in which objects allocate space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.
the advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle.
Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define both:
int a = 3;
int b = 3;
The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3.
at this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B.
It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference variable.
string is a special wrapper class data. Can be used:
string str = new String ("abc");
String str = "abc";
In two forms, the first is to create new objects with new (), which is stored in the heap. A new object is created each time the call is made.
The second is to create an object reference to the string class in the stack str, and then find whether there is no "ABC" in the stack, if not, put "ABC" into the stack, and make str point to "ABC", if there is already "ABC" directly to the "ABC" Str.
Use the equals () method when comparing values within a class, and when testing two wrapper classes for reference to the same object, use = =, the following example illustrates the above theory.
String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2);//true
you can see that str1 and str2 are pointing to the same object.
string str1 =new string ("abc");
string str2 =new string ("abc");
System.out.println (STR1==STR2);//False
the new method is to generate different objects. Each time one is generated.
so the second way to create multiple "abc" strings, there is only one object in memory. This writing is advantageous and saves memory space. At the same time it can improve the speed of the program to some extent, because the JVM will automatically determine whether it is necessary to create new objects based on the actual data in the stack. In the case of string str = new String ("abc"), the code creates a new object in the heap, regardless of whether the string value is equal or not, and it is necessary to create a new object, thereby aggravating the burden of the program.
on the other hand, it is important to note that when you define a class using a format such as String str = "ABC", you always want to assume, of course, that the object that created the string class is Str. Worry about traps! The object may not have been created! Instead, it might just point to an object that was previously created. Only through the new () method can you guarantee that a new object is created each time.
because of the immutable nature of the string class, you should consider using the StringBuffer class to improve program efficiency when a string variable needs to change its value frequently.
The difference between the two:
One, from a conceptual perspective
Basic data type: variable name points to a specific value
Reference data type: The variable name points to the memory address of the stored data object, that is, the variable name points to the hash value
Second, from the memory construction aspect
Basic data type: The variable is immediately allocated to his memory space after the declaration.
Reference data type: This type of variable declaration does not allocate memory, only a memory address is stored.
Third, from the aspect of use
Basic data type: You need to assign a specific value when you use the "= =" number when judging
Reference data type: can be assigned null when used, using the Equals method when judging
Types of Java data types and where to store them