Study notes _java programming ideas

Programming ideas

2. Everything is an object

With reference to the operation target 2.1

In java everything is seen as an object, but the actual operation object is a reference object, a reference to the relationship between objects like TV remote control and TV, a reference to a statement without specifying an object, is also possible, but the operation will be reported null pointer , equivalent to buy a remote control, but no television.

2.2 object storage places

  1. Register
    Register cpu located inside the fastest but the capacity is small, so the java register allocation on demand. Not explicitly specify the operation to a register or registers storing objects, we can not even feel the presence of traces of register
  2. Stack
    Stack is located in RAM, the speed second only to register, but when you create a program, the compiler must know the exact lifecycle stored on the stack of all items, this constraint limits for efficiency and generate a stack objects can not be stored, only can store references to objects ^ [here do not understand, you need to read deeper jvm books]
  3. Heap
    Heap also located in RAM to store all objects in java, the benefit is that the compiler does not need all of the exact life cycle, the downside is that because they do not know the exact life cycle, the memory heap and garbage collection need to be more than the stack more time beneficial to have disadvantages .java data these two structures and objects combined to create a suitable jvm memory management.
  4. The constant pool
    storage will not change constants.
  • Exception: basic types
    for some small, simple but common variables individually for them in the heap to create an object, it seems inefficient for these types, java and using c, c ++ the same way: create a not a reference of variables, which values are stored directly, and placed in the stack, instead of using new to create objects, and thus more efficient. these variables are referred to as basic types of variables.
  • Array
    When you create an array object, in fact, is to create a reference to the array, and all references are initialized to a particular value: null

    2.5 Method

    In java, the method is second-class citizens, can not exist separately, each method must depend on the class. To declare a method, you must first declare a class to carry this method ^ [situation seems to have changed in jdk8].
    Ordinary method depends on the object, you must first create an object in order to use the method.
    static method depends on the class, use the class itself can call the method, you do not have to create the object.

    javadoc

Operator 3

Temporarily slightly

4 control the flow of execution

Temporarily slightly

5 Initialization and garbage collection

5.4 this keyword

A class can instantiate multiple objects, each object can call methods in the class, how to know that the method is invoked his class in which class does, in fact, when the object calling the method, will own implicit incoming method, want to use this object in the method, then, this will be played. in other words, this method is invoked on behalf of an object.
excerpt

5.5 Cleanup

^ [Skip, but it is important]

5.6 Variable Initialization

java principle is: all variables before using (object instantiation) should have been properly initialized, so all variables declared when there is a default value, the basis of the type of the variable default value is 0, false, etc. other variables. type is null.
static variables are initialized only once when the class is loaded, he belongs to the class, there is only one. Assuming a random value static variables, many times to create an object class will not get a different static variable.

5.7 initialization sequence

Member variable is assigned before the method is completed. (Method avoids the use of a variable, unassigned completed, causing the error)
1. Static variables
2. Static block
2. The common member variables
3. Common block
3. The constructor ( implicit static)
4. common method
?? When are static methods to load, why not take a non-static member variables
to access static fields or static methods can lead to load the class, the class loader loads all the static modification of things, including fields and methods .
initialization sequence causing said load

5.8 Variable parameters

5.9 Enumeration

to sum up

In C ++, a large number of programming errors stem from incorrect incomplete initialization, java in order to avoid making the same mistakes, use the constructor of this guarantee structure.
Instantiate an object before the constructor before the constructor (variable assignment) and to ensure full initialization.

7 multiplexing class

7.1 portfolio

7.2 Inheritance

Subclass inherits the parent class after, not just the properties and methods of the parent class simply copied into subclasses. When you create a subclass of object that contains a parent object. The parent object and call the parent class constructor parent object is obtained is the same. the only difference is the latter from the outside, the former is packaged within the subclass object.
to achieve these results, we need to call java objects available to us to ensure the success of the initialization structure --- structure device. in other words, the subclass constructor implicitly calls the parent class constructor. If the parent class is not empty argument constructor, you need to call the parent class constructor in the first line of the subclass constructor or the compiler will not pass.
subclasses may be performed in a class of their parent class method overloading, or direct overwrite covered.

7.3 Proxy

7.6protected

Allow access to protected domain subclass inherits the parent class inside the package from other packages of the parent class, play a role in the protection package. Is not inherited, it can not be accessed.

7.7 upcast

Subclass method level is a superset of the parent class, subclass must contain all of the methods of the parent class, so java allow implicit upward transition. Because the method can call the parent class, subclass can certainly call.
Available for domain , private domain of the parent domain or friendly, access will be given explicitly, but subclasses can access upcast compiler is initialized to null.

7.8final

static final variables can be modified to ensure that only one variable and constant in the class.
P147 has a loading order, it is important

  • final basic type variable
    variable value changes are not allowed
  • final reference type variable
    references are not permitted to be redirected to another object, but the object itself can be changed, including arrays, arrays are objects
  • Blank final variable is
    not assigned when you declare a variable, fishes final variable assignment in the constructor, through mass participation of the construction method, which is more flexible.
    In other words, to determine the final variable is initialized when the object is determined. In this way limitations sex is here, can not be modified for static final variable.
  • final parameter
    parameter references are not permitted to be reassigned
  • final method
    all private methods are implicitly added a final, just as he himself visible, it does not exist in the case overridden by subclasses.
    declare a parent class method declaration and declare private methods in a subclass in the same way, does not belong method overrides, just declare a general subclass method.
  • final class
    is not allowed to be inherited, all methods are implicitly added final

More than eight-state

Encapsulation, inheritance, polymorphism
Subclasses can override the method of the parent class and subclass may be automatically referenced parent class upcast characterized in that reference, is called inheritance.
During operation, the reference to the parent class is a subclass can be automatically bound reference to automatic this procedure calls for concrete subclasses of, called dynamic. Note that the multi-state only for ordinary methods, static fields and methods of an object, do not have the polymorphism.
Subclass calling sequence

9 Interface

It requires a combination of design patterns

  • Strategy Mode
  • Adapter mode

    12 abnormal

    The abnormal 12.2java

  • Exception class is
    java abnormality is a class object, he also has a default constructor, and a standard exception class has a parameter of type String parameterized constructor for storing abnormality information.
  • An exception is thrown similarities with the method returns the
    method throws an exception to the normal return method return values have many similarities, such as:
    1. They can interrupt the method continues
    2. will be returned as a normal reference, the same exception is thrown It will throw an exception object reference
  • The difference:
    the process returns to the object "destination", the method returns a value to the calling stack layer method, an exception will be given to the appropriate exception handler, specifically to look at who has the capacity to deal with this anomaly, an exception may be It would call stack across a number of ways to find his handler.

    12.3 catch the exception

    The first exception is a catch phrase can handle his capture, and after the capture end, that is to say, a plurality of juxtaposed catch statement will not be captured as many times as swtich statement, but an exception is caught after processed you can continue to throw

    12.5 Declare your abnormal

    Use the keyword on the method declaration throws up one method to declare your exception if the method is capable of handling one will deal with these exceptions, if not, they should continue to declare the exception upwards.

    12.6 stack trace

    Print exception information by calling e.printStackTrace, information can be directly accessed by getStackTrace, the method returns an array.
    Array is a stack trace elements, each element of the stack represents an index of the element stack is 0 element, which is the last method call (an exception is thrown place), the last element is the bottom of the stack is the first element of a method call.

Guess you like

Origin www.cnblogs.com/hans-kl/p/11141267.html