Thinking in Java [Java programming mechanism] learning notes -- variables and objects
The book says "You manipulate all objects with references". In Java, the object exists in heap, and all operations on the object are represented by reference in the stack.
In Java, Object Instantiation operations are performed usingNewBut there are also some situations such as string S = "ABC"; also created an object, and S is a reference to the string object "ABC.
One thing to note about the string object is that it is immutable objects. That is to say, modifications to the string will only create a new string and will not modify the original string object.
Why immutable objects?
Immutable objects are only useful in concurrent applications. Since they cannot change state, they cannot be upted by thread interference or observed in an inconsistent state.
There is also a class of variables in Java. They are not created using new, they are not referenced but a real variable. They are called primitive data type.
The following table defines these variables in Java:
If you want to create them in the heap and use them to reference them, you can use their corresponding class to create them.
The following table lists the default values of each variable:
| Data Type |
Default Value (for fields) |
| Byte |
0 |
| Short |
0 |
| Int |
0 |
| Long |
0l |
| Float |
0.0f |
| Double |
0.0d |
| Char |
'U000000 ′ |
| Boolean |
False |
| Object (any object) |
Null |
| String (is a object) |
Null |
| Array (is a object) |
Null |
Uninstantiated variables are a warning in C ++ and an error is reported in Java.