This time is more than before the text time, is because, one night after reading Java programming thought literature after the late to take notes.
The following notes are basically reproduced, not original
Chapter II Everything is the object
Directory:
- 2.1 Manipulating objects with references
- 2.2 All objects must be created by you
- 2.3 Never need to destroy objects
- 2.4 Creating a new data type: Class
- 2.5 methods, parameters, and return values
- 2.6 Building a Java program
- 2.7 Your first Java program
- 2.8 Notes and embedded documents
- 2.9 Coding Style
2.1 Manipulating objects with references
Everything is considered an object, the manipulated identifier is actually a "reference" to the object, the remote control (reference) manipulates the TV (object), wants to regulate the television, just through the remote control, and the remote control is independent.
2.2 All objects must be created by you
Once you have created a reference, you want it to be associated with a new object, which is usually accomplished with the new operator.
string s = new String ("Jiancheng");
Five places to store data:
1) Register . This is the fastest storage area because it is located in a different location from the other store-the inside of the processor.
2) stack . is located in Universal RAM (random access memory), but can receive direct support from the processor through the stack pointer.
If the stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. This is a fast and efficient method of allocating storage, second only to registers .
When you create a program, theJava program must know the exact life cycle of all items stored in the stack to move the stack pointer up or down.
3) heap . A common memory pool is also located in the Ram area for storing all Java objects .
the benefit of a heap that differs from the stack is that the compiler does not need to know how long the stored data survives in the heap .
When you need an object, simply write a simple line of code with new.
Storage allocation and cleanup with the heap may take more time than storage allocation with the stack.
4) constant storage . Constants are usually stored directly inside the program code, and it is safe to do so because they will never be changed.
In an embedded system, the constants themselves are isolated from the rest, and in all cases, they can be stored in ROM (read-only memory).
5) non-RAM storage . If the data is completely outside the program, then it is not under any control of the program and can exist if the program is not running.
Two of the basic examples are stream objects and persisted objects .
In a stream object , the object is converted into a byte stream, which is usually sent to another machine.
In persisted objects , objects are stored on disk, so they can remain in their own state even if the program terminates.
The stack is a stack. (important thing to say three times, this is the second time)
If the stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. This sentence has not been understood, because the university study of the data structure, the bottom of the stack is at the base, the top of the stack is at the top, so the stack pointer down, should be released memory, so this sentence is wrong? Of course not!
Virtual memory distribution for a process in Linux:
Figure No. 0 address at the bottom, the higher the memory address of the larger.
Take the 32-bit address operating system as an example, a process can have a virtual memory address range of 0-2^32. It is divided into two parts, one for kernel (kernel virtual memory) and the other for the process itself, that is, the processes virtual memory in the diagram.
The ordinary Java program uses the process virtual memory.
The topmost part of the memory is called the user stack. This is the stack that the topic asks. There's a runtime heap in the middle. Is the heap in the title. their names have little to do with the stack (stack) and heap (heap) in the data structure .
Note that the stack (stack) is growing downward; The heap is grown upward.
Quote this Answer
So, when the stack pointer moves down, it allocates memory and frees memory when it moves up.
9 Basic types:
basic types occur because this type is a small, simple variable that does not need to allocate space on the heap with new (the base type of the exception member variable is on the heap) and is placed directly on the stack, which is more efficient.
The size of the storage space occupied by the Boolean type is not explicitly specified and is defined only to be able to take the literal value true or false.
High-precision numbers:
- BigInteger: Integer that supports arbitrary precision
- BigDecimal: Fixed-point number with arbitrary precision support
Operations are similar to what can be performed on a basic type, but must be implemented in a method invocation instead of an operator.
Arrays in Java:
The array is initialized and cannot be accessed outside of its scope. This range check is at the expense of a small amount of memory overhead on each array and the runtime's subscript check (for security and efficiency).
2.3 Never need to destroy objects
Scope:
------------------------------------------------------------------
Scope of the object:
Java has a large difference in its role and basic type, and new creates a Java object that can survive outside the scope. As long as you need this object, it will persist until the garbage collector recognizes that the object is no longer referenced and frees the object's memory space, eliminating the problem of memory leaks.
2.4 Creating a new data type: Class
Most object-oriented programming languages are accustomed to using the key sub-class to represent "I'm going to tell you what a new type of object looks like."
class /* */ }
2.4.1 fields and methods
All you do in Java is define classes, produce objects of those classes, and send messages to those objects.
Once a class is defined, you can set two types of elements in a class: fields (sometimes referred to as data members ) and methods (sometimes referred to as called member functions ).
A field can be any type of object, can communicate with it by its reference, or it can be one of the basic types. If a field is a reference to an object, you must initialize the reference so that it is associated with an actual object.
Each object has space to store its fields, and normal fields cannot be shared between objects.
class dataonly { int i; Double D; Boolean b;}
How to refer to a member of an object:
After the name of the object reference is followed by a period, and then the member name inside the object:
objectReference.member
Such as:
DataOnly data = new DataOnly();data.i = 47;data.d = 1.1;data.b = false;
Basic member Default value
If a member of a class is a basic data type, Java ensures that it obtains a default value even if it is not initialized.
| Basic Type |
Default Value |
| Boolean |
False |
| Char |
' \u0000 ' (null) |
| Byte |
(byte) 0 |
| Short |
(short) 0 |
| Int |
0 |
| Long |
0L |
| Float |
0.0f |
| Double |
0.0d |
These initial values may not be correct or even illegal for your program. Therefore , it is best to initialize the variables explicitly .
However, the above method of ensuring initialization does not apply to (that is, a field that is not a class).
In a method definition, there are:
int x;
If you forget to assign an initial value , Java will return an error in the compilation, telling you that the variable is not initialized.
2.6 Building a Java program
Static keyword:
The following two scenarios require the use of static
- Just want to allocate a single storage space for a particular domain, without thinking about how many objects to create.
- A method is not associated with any object that contains its class, or it can be called without creating an object.
Class statitest{ static i = 47;//can be directly statitest.i such a call}
Statitest st1 = new Statitest ();
Statitest st2 = new Statitest ();
where st1.i and st2.i point to the same storage space, they have the same value of 47.
Similarly, the preferred way to invoke a static method: ClassName.method() . Such as:
class incrementable { staticvoid increment () { statictest.i+ +; }}
Call method One:
New incrementable (); Sf.increment ();
Call Mode two:
Incrementable.increment ();
2.9 Coding Style
Hump Style:
- class, with uppercase letters in the first letter of each word
- Methods, fields, and objects, the first letter in lowercase, the remaining first letter capitalized
Summarize:
This chapter is easy to understand, the Java language and some of its basic ideas have a general understanding, most of the knowledge point has also been exposed, but easy to forget the details of the place, look no more.
Java Programming thought chapter II