Although Java is based on C + +, Java is a more purely object-oriented language.
2.1 Manipulating objects with references
Each programming language has its own way of manipulating elements in memory. Sometimes programmers need to be aware of what type of data they are going to manipulate. is to manipulate the elements directly, or to use some kind of indirect representation based on special syntax (such as pointers in C and C + +).
Java is very simple, because everything is an object, we can use a single syntax to operate. Using the remote control to operate the TV mode, with reference to manipulate the object. Any object can be manipulated with a reference.
2.2 All objects must be created by you
After you have created a reference, you want it to be associated with an object. Often implemented with new:
string s = new string ("Hello");
Java has built in a lot of out-of-the-box types, but it's important that we create types ourselves and then create new objects.
Where is the 2.2.1 stored?
How are the objects placed when the program is run? In particular, how is memory allocated? It's a great help for us to know this. There are five places where data can be stored:
1) Register: Located inside the processor, is the fastest storage area, but the number is limited, on demand. It cannot be controlled or sensed in the program, and C and C + + allows the compiler to suggest how the registers are allocated.
2) Stack: in RAM, direct support from the processor can be obtained from the stack pointer. Speed is second only to register. When a Java program is created, it must know the exact life cycle of all elements in the stack to move the pointer up and down, which limits the flexibility of the program. So Java puts the reference on the stack, and the object exists in the heap.
3) Heap: Universal memory pool, also in RAM, holds all Java objects. The compiler does not need to know the life cycle of the data in the heap. So it is very handy and flexible to use new to automatically store allocations in the heap, but the flexible back is also a cost: the heap of storage and allocation takes more time than the stack.
4) Constant storage: constants are stored directly in the code, which is safe and will not be changed.
5) Non-RAM storage: If the data is completely outside of the program, it can be controlled, and the program can exist without running. Common flow and persistence objects. The trick is to turn objects into things that can be stored in other mediums and can be converted into RAM when needed.
2.2.2 Exceptions: Basic types
Basic types because it is relatively simple to compare "small", it is often not efficient to create it in the heap with new, so the basic types are more efficient when they are stored in the stack space directly according to the size of their respective types. The size of each type of Java does not change across platforms, which enhances portability.
Boolean--
BYTE 1 byte (8bits) -27~27-1
Short 2 bytes (16bits) -215~215-1
int 4 bytes (32bits) -231~231-1
Long 8 bytes (64bits) -263~263-1
Float 4 bytes (32bits)
Double 8 bytes (64bits)
void--
You can use wrapper classes to make them created in the heap.
With BigInteger and BigDecimal two classes can be used for high-precision calculations, they do not have a corresponding basic type.
Arrays in 2.2.3Java
Almost all languages support arrays, and C and C + + use arrays to manipulate memory blocks directly, which is dangerous if access is out of bounds or uninitialized, which can cause unpredictable problems. To improve security, Java ensures that an array is initialized and cannot be accessed outside its scope (at the expense of a small amount of memory overhead and run-of-the-mark checks, which is worthwhile). When the array is created, the reference is set to a specific value of NULL, and if Java discovery attempts to use a or null reference, a run-time error is generated, which avoids the situation.
2.3 Never destroy objects in most programming languages, the life cycle design of variables is very important and how long does it survive? When will it be destroyed? The chaotic life cycle often leads to a lot of bugs. Here's how Java is doing all the cleanup for us, which greatly simplifies the problem.
2.3.1 Scope
The scope determines the visibility and life cycle of the internal variables.
Scope of the 2.3.2 object
The life cycle of objects and primitives in Java is different, and when a reference does not exist, the object is still in memory, waiting for garbage collection.
2.4 Creating a new data type: Class we use the keyword class to define a new type
Class atypename{}
Even if we don't do anything, and the class doesn't behave, we can already use new atypename() to create its object.
2.4.1 fields and methods
Java programs only do 3 things: Define classes, produce objects, and send messages.
After the class definition, you can set 2 types of elements in the class: field (data member), method (member function).
Java Programming Ideas: 2nd Chapter Everything is the object