Java is based on C ++, but Java is a more pure object-oriented programming language.
C ++ and Java are mixed / hybrid language. Heterozygous language allows multiple programming styles.
Manipulate objects with references
Each programming language has its own way to manipulate the memory element.
Direct manipulation element
Based on indirect representation in some special syntax (C and C ++ pointers)
With reference to manipulate objects (Java)
In Java, everything is treated as objects. Manipulation identifier is actually a reference to an object.
Analogy for the remote control (reference) to manipulate the television (the object)
So long as the remote control, you can stay connected to the TV
Change channels or decrease the volume, the actual manipulation of the remote control (reference), and then by the remote controller to regulate the television (the object)
Walk around the room, as long as the portable remote control (reference) instead of television (the object) TV will be able to Regulation
Even without the TV, remote control can also exist independently
// Create an object reference instead of just
// Create a reference while it initializes
// Java language features: strings can be initialized with quoted text
String s2 = "abcd";
// More generic initialization method
// Create a reference, and with a new object is associated
String s3 = new String ( "abcd");
All objects must be created by us
Where to store
The program runs, placing arrangement, the memory assignment of all we have to understand.
There are five different places to store data:
4. Constant storage
5. Non-RAM storage
Register: Located in a very limited number of registers, is the fastest memory, the register will be allocated according to demand, can not directly control, can not register any signs of the presence felt in the program
Stack: in the general RAM (random access memory) can be obtained by the stack pointer from the processor where direct support, the stack pointer is moved downward, the allocation of new memory, move up, then release that memory (this is a fast and effective storage allocation method, second only to register) to create a program, Java stored within the system must know the exact stack all the life cycle, in order to move up and down the stack pointer. ---> Limits the flexibility of the program, so some Java data (such as object references) stored in the stack, but Java objects are not stored in the stack
Heaps: common pool of memory (RAM area is located). Used to store all java objects. The compiler does not need to know that the data stored on the heap survive long (flexibility, but a longer time allocation and cleanup).
Constant storage: Store in the program code internally.
Non-RAM memory: data survived completely outside the program, without any control of the program, the program is not running may also be present. Such as stream objects, persistent object.
Exception: Basic type
a new object is stored in the heap, and therefore an object created with the new - particularly small and simple variables are often not very effective.
For these types, Java do not take to create a new variable, but not automatically create a variable reference (directly stored value and stored in the stack).
Java language for each share of the basic types of storage space is fixed, unlike other language changes randomly hardware architecture varies. (More portable)
* Create a basic type is not a reference, but an automatic variable, direct store values, and stored in the stack.
* Basic type has a corresponding wrapper class can create a heap of non-essential object that represents the basic types correspond.
int i = 5;
//type of packaging
Integer integer = new Integer (i);
// Java SE5 automatic packaging
Integer integer1 = 4;
// Reverse conversion
int i1 = integer1;