2.1 Manipulating objects with handles
Everything in Java is "considered" as an object. The way the object is manipulated is through a handle to the object (also known as a reference or pointer). But the handle does not necessarily point to the object, and the action handle gets an error (run-time)
For example: String s;s.length ();
The handle s,s is created here and does not point to the object. If you send a message to s at this point, you will get an error. The compiler prompts "The local variable s may not have been initialized"
string s = new String ("hello,world!") ; S.length ();
The handle is created here S,s points to an object (the object is located in a heap area) and sends a message to s normal.
2.2 All objects must be created
When you create a handle, it is generally also pointed to an object. The object is created in the New keyword.
string s = new String ("hello,world!");
2.2.1 Where data is saved
① registers, located inside the processor. is assigned by the compiler as needed.
The ② stack, which resides in the normal Ram area, is second only to register. The object handle is saved in the stack.
③ Heap, a general-purpose memory pool (in RAM). The flexibility is higher than the stack, but the performance is low (allocating more storage space within the heap) and Java objects are saved in this area.
④ static storage, located in RAM. The static keyword indicates that a particular element of an object is static. However, the Java object itself will never be placed in a static store.
The ⑤ constant is stored in read-only storage (ROM). Save constant value
⑥ non-RAM storage, streaming objects, or fixed objects.
2.2.2 Main types
Also known as the basic type, mainly includes: boolean,char,byte,short,int,long,float,doule,void
For these types, instead of creating a variable from new, you create an "automatic" variable that is not a handle, which holds a specific value. On the stack.
The main type has its own wrapper class
Basic data Types
Main type |
Size |
Minimum value |
Maximum Value |
Wrapper type |
Boolean |
1 |
|
|
Boolean |
Char |
16 |
0 |
2^16-1 |
Character |
Byte |
8 |
-128 |
127 |
Byte |
Short |
16 |
-2^15 |
2^15-1 |
Short |
Int |
32 |
-2^31 |
2^31-1 |
Integer |
Long |
64 |
-2^63 |
2^63-1 |
Long |
Float |
32 |
IEEE754 |
IEEE754 |
Float |
Double |
64 |
IEEE754 |
IEEE754 |
Double |
Java Programming thought Fourth Edition--Everything is an object