Objective
Java is based on C + +, but Java is a more purely object-oriented programming language.
Both C + + and Java are mixed/hybrid languages. The hybrid language allows a variety of programming styles.
Manipulating objects with references
Each programming language has its own way of manipulating elements in memory.
- Manipulating elements directly
- Use some kind of indirect representation based on special syntax (pointers in C and C + +)
- Manipulating objects with references (Java)
In Java, everything is treated as an object. The manipulated identifier is actually a reference to the object.
- Analogy for remote control (reference) to manipulate the TV (object)
- Just grasp the remote control, you can keep the connection with the TV
- Change the channel or reduce the volume, the actual operation is the remote control (reference), and then the remote control to control the TV (object)
- Walk around the room and control the TV with a remote control (reference) instead of a TV (object)
- Remote control can exist independently even without a TV
1 //create only references instead of objects2 String S1;3 //Create a reference and initialize it at the same time4 //features of the Java language: strings can be initialized with quoted text5String s2 = "ABCD";6 //a more general method of initialization7 //creates a reference and is associated with a new object8String s3 =NewString ("ABCD");
All objects must be created by us where to store
When the program runs, the placement of objects, the allocation of memory is what we must understand.
There are five different places to store your data:
1. Register
2. Stacks
3. Heap
4. Constant Storage
5. non-RAM storage
Register : In a very limited number of registers, is the fastest storage area, the register will be allocated according to demand, can not directly control, and can not feel the register in the program any signs of existence
stack : In general RAM (random access memory), through the stack pointer can get direct support from the processor, the stack pointer moves down, then allocate new memory, move up, then release those memory (this is a fast and efficient allocation of storage methods, After register) when creating a program, the Java system must know the exact life cycle of all items stored in the stack to move the stack pointer up or down. ---> limits the flexibility of the program, so some Java data, such as object references, are stored on the stack, but Java objects are not stored in the stack
Heap : A common memory pool (located in the Ram area). Used to store all Java objects. The compiler does not need to know how long the stored data will survive in the heap (flexibility, but the time allocated and cleaned up is longer).
constant Storage : stored inside the program code.
non-RAM storage : Data is completely outside of the program and is not controlled by any program, and can exist when the program is not running. A persisted object , such as a stream object .
Exception: Basic type
The new object is stored in the heap, so creating an object with new-especially small, simple variables -is often not very effective.
For these types, Java takes the creation of a variable without new and creates an automatic variable that is not a reference (storing the value directly and storing it in the stack).
The Java language is a fixed amount of storage space for each base type, unlike other languages that vary with the hardware architecture of the machine. (Higher portability)
1 /**2 * The base type creates not a reference, but an automatic variable that stores the value directly and stores it in the stack. 3 * Basic types have corresponding wrapper classes that can create a non-basic object in the heap to represent the corresponding base type. 4 */5 //Basic Type6 inti = 5;7 //Package Type8Integer integer =NewInteger (i);9 //Java SE5 Automatic packagingTenInteger Integer1 = 4; One //Reverse Conversions A intI1 = Integer1;
Reprint: http://www.cnblogs.com/JohnTsai/p/4606365.html
Java Programming thought learning (a) everything is an object