Basic concepts of 1.Java programming
1.1 JVM
ClassLoader (class loader): Each Java file corresponds to a Java class, which is gradually loaded into memory according to the program. (generally extclassloader load Java Extension API is the class in/lib/ext, Appclassloader is used to load the Classpath settings on the user machine in the directory of classes).
ClassLoader loading process: When the program runs, the JVM starts, runs Bootstrap ClassLoader (starts the ClassLoader), The ClassLoader load Java Core API (Extclassloader and Appclassloader are also loaded at this time), calling Extclassloader load extension API, Finally Appclassloader loads the class defined under the Classpath directory. ----The most basic loading process for a program
Within the JVM, uniform encoding is Unicode, which can only occur at the boundary, at the junction of the JVM and OS, and also in the various input and output (or Reader,writer classes) where it works. (so byte-oriented and character-oriented are different)
1.2 i++
i++ is self-increment after the program is finished, ++i is the self-increment before the program starts
i=0;i=i++ + ++i=0+2=2
1.3 Type Conversions
Boolean type numeric character type
Low-level turn advanced auto-conversion (if the char type goes to advanced type, automatically goes to ASCII code)
Coercion type conversion: (type) variable name
Packaging type over-conversion
String and other type conversions (ToString () to String)
1.4 Program Structure
ASSERT (expression) assertion
1.5 operator
& && (and the previous & difference is as long as the front false does not need to be judged)
1.6 Exceptions
An unhealthy behavior that occurs when a Java program is running, which is called a run-time error. Error (generated and thrown by Java Virtual Machine 5) and exception
Final (variable value unchanged or object reference unchanged), Finall, Finalize (run at most once)
1.7 Reflection----refers to the ability of a program to access, detect, and modify its own state or behavior.
Reflection allows program code to access the internal information of classes loaded into the JVM, rather than the class collaboration code selected in the source code, when writing and executing. It is important to note that the cost of reflection is high if used improperly.
Class Reflection Relectiion in Java is one of the features of Java programming language development, which allows a running Java program to examine itself and to manipulate the internal properties of the program directly.
2 Java Memory Management
2.1 Garbage Collection
allocating memory and freeing memory are often caused by a "memory leak", so Java automatically allocates memory when it creates an object, and automatically frees memory if the object reference does not exist.
The garbage collector technology monitors the Java program to run. Java uses a series of soft pointers to track the individual references of an object and maps these soft pointers to objects as references in an object table. With soft pointers, Java's garbage collector can run in the background with a separate thread and examine each object in turn. (Tag objects, remove objects, move objects, or inspect objects)
In the Java language, there are only two criteria for determining whether a piece of memory space conforms to the garbage collector cell phone Standard: (1) NULL null values have been assigned to the object and no calls have been made, (2) A new value has been assigned to the object, that is, the memory space has been reassigned.
(Reminder: A piece of memory space that complies with the garbage collector's collection criteria does not mean that the memory space will be reclaimed by the garbage collection.) )
Garbage collection note several points:
1) Do not attempt to assume garbage collection time, unknown.
2) Java provides some ways to deal with garbage collection, but also provides a way to enforce garbage collection-call System.GC (), but the Fong Fong is also an indeterminate method. (There is no guarantee that the call will be able to start and recycle, just make a request to the JVM, everything is unknown)
3) Select the appropriate garbage collector. (The JVM default option is generally used, but,,)
4) Memory leak problem ( memory object is clearly not needed, still retains the memory and its access Way (reference), the object is life cycle, some long, some short, if the long life cycle of the object holds a short life cycle of the reference, it is likely to have a memory leak ). Good programming habits and rigorous change to attitude.
In Java programs, we typically use new to allocate memory to objects that are on the heap.
Here's an example of borrowing someone:
Public class Simple { object object; Public void method1 () { new Object (); // ... Other code } }
After the Method1 method call ends, the object is not disposed, until the simple end is released.
Solution:
Public class Simple { object object; Public void method1 () { new Object (); // ... Other code object=null // Add one step } }
5) Release references to useless objects as early as possible. For operations that frequently request memory and free memory, it is best to use finalize enforcement or write your own Finalize method, which does not necessarily apply to the System.GC () method. Before the JVM garbage collector collects an object, it is generally required that the programmer call the appropriate method to free the resource, but without explicitly releasing the resource, Java provides a default mechanism to terminate the object to release the resource, the Finalize () method.
2.2 Memory Management
The memory management of Java is the allocation and release of objects. In Java, programmers request memory for each object through the keyword new (except for the base type), and all objects allocate space in the heap. Object deallocation is determined and executed by the GC. GC monitors the running status of each object (request, reference, referenced, assignment, etc.)
Memory leaks Two features: 1) object can be reached, that is, the existence of the path and its connection 2) object is useless. (Memory leak Reason: 1. Global collection.) 2. Cache 3. ClassLoader)
2.3 Clone
3. Passing and referencing
3. 1 Passing and passing references
In Java, variables fall into two categories. (a discussion of the Java value-passing reference)
1) for the base data type (int float long double boolean char byte), a copy of the value is passed.
2) For all object type variables, Java is a copy of the reference. The essence of a reference copy is to copy a pointer to an address. (This differs from C + + in that when a parameter is a reference type in C + +, the actual reference is passed instead of the copy)
The string type is also an object type, so it must be a reference copy. It is a non-mutable class, and there is no difference between using a value or passing a reference.
For the basic type, the value of the transfer is to copy the copies of themselves, even if their own copy of the change itself will not change.
For an object type, it passes a copy reference, pointing to its own address instead of a copy of the actual value. Because the object type is placed in the heap, the speed is slower relative to the base type, on the other hand the object type itself is larger, and if you choose to re-copy the value waste memory and slow. (Interesting analogy: copying keys instead of duplicating warehouses)-------------------some books, such as Thinking in Java, refer to "both basic and object types as values." They use reference replicas as a value.
Reference passing Example 1:
1 Public classTest {2 Public Static voidMain (string[] args) {3StringBuffer str=NewStringBuffer ("Hello");4 Test (str);5 System.out.println (str);6 }7 Public Static voidTest (StringBuffer s) {8S=s.append (", world!");9 }Ten}
Output: hello,world!
Another example 2:
1 Public classTest {2 Public Static voidMain (string[] args) {3String str= "Hello";4 Test (str);5 System.out.println (str);6 }7 Public Static voidTest (String s) {8S= "world!"; The system automatically generates a new string object set to "World" and then assigns a reference to this object to Str.9 }Ten}
Output: Hello
The Stringl class is final, so you cannot modify and inherit the class, and when the function ends in Example 2, the S function disappears.
Example 1 StringBuffer is to produce a piece of memory space, related to add and delete operations are in it, still in the same memory address.
String StringBuffer Stringbuilder
1) variable, non-variable
The string class uses character arrays to hold strings because there is a "final" modifier, so you know that a string object is immutable. StringBuffer and StringBuilder are variable)
----String-----Private Final char value[];
-----StringBuffer and StringBuilder-----char[] value;
2) is thread safe
The objects in string are immutable and can be understood as constants, apparently thread-safe .
Abstractstringbuilder is the common parent of StringBuilder and StringBuffer, and defines some basic operations for strings, such as expandcapacity, append, insert, indexof, and other public methods.
StringBuffer is thread-safe because it adds a synchronous lock to a method or a synchronous lock on a method that is called.
StringBuilder does not have a synchronous lock on the method, so it is non-thread safe .
If the program is not multithreaded, then using StringBuilder is more efficient than stringbuffer.
Similarly, the nature of the array pass-through value is also a copy of the address value.
3.2. Static variables and private variables
Variables defined in the class will default to an initial value. Boolean type default initial value is False
The main function cannot access a non-static variable or method
3.3. Input/output stream
Large file reads: New BufferedReader (New InputStreamReader ("filename"))
Write file: FileOutputStream =new fileoutstream ("filename"), Out.write ("String to write". GetBytes ()); Out.close ();
Java IO operations have two ways of byte-oriented and character-oriented character.
Byte-oriented operations operate on data in 8-bit binary units and do not need to convert data, and the classes of these operations are subclasses of InputStream and Ouputstream.
Character-oriented operations need to convert the binary to a character when read in characters, and the characters need to be converted to binary when writing, and these classes are subclasses of reader and writer.
3.4 Serialization
Java Programming <5>