Http://www.cnblogs.com/tianzijiaozi/diary/2017/08/10/7340813.html
Everything is Object Java
1. Create a new object with the keyword new
★string s=new String ("asdf");
What does this process do?
⑴ Request Storage Space
⑵ Call constructor to initialize
⑶ returns a first address (Java is referred to as a reference)
2. Where are they stored?
★ Register (fastest)
★ Stack (FAST)
★ Heap (flexible, but relatively slow)
★ Constant Storage (stored directly in the code)
★ Non-RAM storage (external storage)
Register: The fastest, not directly control, write the program when the presence of the Register
Stack: In general RAM (random access memory), but through the stack pointer can be directly from the processor to get support, if the stack pointer moves downward, the allocation of new
memory, if moving upward, The memory is freed. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java system must know the exact life cycle of storing
all items within the stack to move the stack pointer up or down.
Heap: A common memory pool (also located in RAM) that holds all Java objects. The heap is different from the stack: The heap does not need to know how long the stored data survives in the heap
. Because of this, there is great flexibility in allocating storage space in the heap. When an object is needed, it is only necessary to write a simple line of code with new, and when this line of code is executed
, the storage allocation is automatically made in the heap. The disadvantage is that storage allocation and cleanup with heaps is more time-consuming than using stacks.
Constant Storage: Constant values are generally placed directly inside the program, so it is safe to do so because they can never be changed. In the case of embedded, the constants themselves may be separated from the program
and placed in ROM
non-RAM storage: If the data is completely outside the program, it can be unaffected by any program. such as stream objects and persisted objects
A notable feature of the Java language is the introduction of a garbage collection mechanism, which makes it possible for C + + programmers to solve the most troublesome memory management problems, which allows Java programmers to write
It is no longer necessary to consider memory management in a program. Because there is a garbage collection mechanism, objects in Java no longer have a "scope" concept, only references to objects are "
With the domain. Garbage collection can effectively prevent memory leaks and effectively use free memory.
The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Find useless letters
(2) Reclaim the memory space occupied by the useless object, so that the space can be reused by the program.
1. Java File Comment Header (class, function)
2. Package Declaration and Reference Rule one: package name should be unique Rule two: package name should usually be lowercase ascii letter rule three: package name
is generally the reverse of the company's domain name, often used to have the domain name suffix has cn, com, edu, gov, mil, net, org rule four: the part behind the domain name can be used to life
Named
3. Declarations and references to classes
Rule one: The first letter of each word is capitalized
Rule two: Other identifiers: Style is similar to the class name, except the first letter lowercase
4. Code line length rules: Generally speaking, Java code line length should be less than 80 characters.
5. Good-style code.
2. Code organization
⑴ Each Java file has a public class (main class) and several non-public classes (auxiliary classes)
★ When compiling Java files, each class will have an output file (. class file)
⑵ libraries (class library)
★ consists of a number of Java files and more. class files
If we want these files to belong to a group, we have to use the package.
1, the meaning of the permission
Public: Common access rights
Private: Privileged Access
Protected: Protecting access rights
Default: Package access rights
★ The first two have exactly the same meaning as in C + +
and protected:= Subclass + package access rights
If no access modifier is provided, it means that it is a package access permission
2. Default: Package access rights
★ can be accessed by all classes in the same package.
★ Because classes in the same package can access each other by default, the classes that need to work together are usually placed in a package for ease of programming and management.
★ The second basic principle of access control: class consumers generally do not have access to the package, preventing the class's consumers from accessing and modifying the classes in the package. 、
There are two types of access permissions for the ★java class
Public permissions
Package access rights
(But the inner class can be private or protected)
The following are the effects of various access rights
2, how to clean in Java?
Answer: Garbage collector, the garbage collector only knows to release memory that is allocated through the new keyword.
★ Question 1: Is there memory that is not allocated by new?
If it does not exist: the above paragraph should be changed to garbage collector to reclaim all memory that is no longer needed
If it exists: But in Java everything is an object, and the object must be allocated memory through the New keyword
Answer: There is indeed memory that is not allocated by new (using local methods to invoke non-Java code, such as the malloc function of c)
★ Question 2: Does the garbage collector know how to release this special memory?
The answer is obvious: I don't know.
So how is this special memory released? Java allows you to define a Finalize () method in a class that can be implemented by invoking the method.
In Java, after we have finished new objects, the garbage collector is responsible for reclaiming the memory resources occupied by useless objects.
The Java Virtual machine takes an adaptive garbage collection technique.
Stop-copy: Pauses the program first (it is not in background recycle mode), and then copies all the surviving objects from the current heap to the other heap, with no replicated objects
The objects that are copied into the new heap are in a compact arrangement.
Tag-sweep: Iterate through all references to find more surviving objects. When a surviving object is not found, the object is flagged, and the process does not clean up any
Like. Garbage is not cleaned until all the marks have been completed.
During a Java virtual machine run, if all objects are stable and the garbage collector becomes less efficient, it switches to "tag-sweep"; consent, Java Virtual Opportunity Tracking "
Mark-Sweep "effect, if there is a lot of debris in the heap space, it will switch to" stop-copy ";
2. Specify initialization
★java Allow: You can assign an initial value to a class's data member while defining it
Advantages: Simple and intuitive
Cons: Each object has the same initial value
void public class InitialValues2
{int i;//auto-Initialize
int j = 999; ...//auto-initialize first, then specify initialization
}
Note: C + + does not allow this and must all be initialized by constructors.
1, the first basic principle of data member initialization
★ can be initialized with a constructor
The order of execution is automatic initialization, specifying initialization, and finally invoking the constructor for initialization.
The order in which variables are defined is the order in which they are initialized.
★ The second basic principle of initialization
The order of initialization is the first static object, and then the non-static object
1. Basic Knowledge about arrays
The size of the ⑸ array (very different from C + +)
★ The size of the array can be determined at run time, but it is not recommended
such as: int[] A; A = new Int[rand.netint (20)];
The type of the ⑹ array element can be an object reference, which is called an array of references
★ Note that the reference array is initialized, otherwise a null reference (NULL) is used in the program.
2, variable parameter list.
★ Apply to the number of parameters or unknown types of occasions.
public class Newvarargs {//variable argument list syntax
static void PrintArray (Object...args) {
for (Object Obj:agrs)//foreach syntax
System.out.println (obj+ "");
System.out.println ();
}
public static void Main (string[] args) {
PrintArray (New Integer, New Float (3.14), New Double (11.11));
PrintArray (47,3.14f,11.11);
PrintArray ("One", "one", "one", "three");
PrintArray (New A (), New B () New C ());
..... }
}
Problem:
1. Create a class that contains an int field and a char field that are not initialized and print their values to verify that Java performs the default initialization.
2. Write a program that shows that no matter how many objects you create for a particular class, there is only one instance of a specific static field in the class.
For:
1. public class Excercise8_9 {
public static void Main (String args[]) {
A c = new A ();
System.out.println (C.tostring ());
}
}
Class A {
int A;
Char b;
Public String toString () {
Return "a=" +a+ "b=" +B;
}
}
2.public class Excercise8_9 {
public static void Main (String args[]) {
A = new A ();
A b= new A ();
A.print ();
B.print ();
A c= new A ();
A.print ();
B.print ();
C.print ();
}
}
Class A {
static int A;
A ()
{
a++;
}
public void print ()
{
System.out.println (a);
}
}
Problem:
1. Create a class that contains a string field that is initialized when it is defined, and another string field initialized by the constructor. What is the difference between these two approaches?
2. Write a class with two (overloaded) constructors, and in the first constructor, call the second constructor with this one.
3. Create an array of String objects and assign a string to each element. Use the For loop to print the array.
For:
1, classa{
Private String str1;
Privatestring str2;
A ()
{
Stringstr2=new String ("test");
}
}
2, classa{
int i;
A () {
This (0);
}
A (INTJ)
{
I=j;
}
}
3. publicstatic void Main (String args[]) {
String[]test = {"What", "is", "You", "doing", "Now"};
for (int j = 0; J < Test.length; J + +) {
System.out.println (Test[j]);
}
}
Problem:
1. Create a class in a package that creates an instance of the class outside the package where the class is located.
2. Create a class with public,private,protected and package access fields and method members, create an object of the class, and see what type of compilation information you get when you try to invoke members of all classes. Note that all classes in the same directory are part of the default package.
For:
1.
Packageexcercise1;
importexcercise2.*;
Publicclass Excercise1 {
publicstatic void Main (string[] args)
{
Excercise1 a=new excercise1 ();
}
}
Packageexcercise2;
Publicclass Excercise2 {
Public Excercise2 () {
System.out.println ("Hello");
}
}
2.
Smooth access to the protected and public methods, package visible methods, but the private content compiler error,
Java Platoon training 1