[Study notes] "Java programming ideas" Chapters 1 through 7

Chapter One Introduction to Object

Summary of the whole book.

Skimming.

Chapter II Everything is an object

  1. Create a reference to an object.
  2. Security approach: create a reference at the same time it will be initialized.
  3. Object storage place:
    1) Register : This is the fastest storage area where it is located as different from other storage areas - internal processor.
    2) the stack (the stack is the stack) : in the general RAM, but can be directly supported from the processor where the stack pointer. The stack pointer is moved downward, allocating new memory; if moved upward, to release that memory. This is a fast and effective method for allocating memory, only to register. When you create a program, Java systems must know the precise life cycle in the stack to store all the items, in order to move up and down the stack pointer. Although some Java data stored in the stack - in particular, object references, but not Java objects stored therein.
    3) reactor : a common memory pool (also located in RAM), for storing all Java objects, the stack is different from the heap benefits: the compiler does not need to know the data stored in the heap survive long.
    4) Constant storage: constant value usually stored directly in the program code, it is safe to do so, because they will never be changed.
  4. Autoboxing: automatically convert a raw data type into a package type
  5. Automatic unboxing: automatically package type data into a raw data type
  6. 8 the basic data types:

     

     Wherein the size of the storage space occupied by the boolean type not specified, it is defined to be able to take only the literal true or false.

  7. Java objects and primitive data types do not have the same life cycle. When you create a Java object using new, it can survive outside the scope.
    For example:
    {
      String s = new new String ( "");
    }
    referenced in scope s end disappears. However, s point of String objects still continue to occupy memory space. In this short code, we can not access this object after this scope because it only references have been beyond the scope of scope.
  8. Java has a garbage collector, to monitor all the objects created with new, and which objects are no longer referenced identified. Then release the memory space of these objects.
  9. Even create multiple objects that contain static variable, then this variable is only a storage space. This may be a reference to locate the object by object, it can also be directly referenced by the class name.
  10. Class Integer Example:

    Integer i1 = 10;//this is autoboxing
    Integer i2 = 10;
    Integer i3 = 20;
    Integer i4 = new Integer(10);
    Integer i5 = new Integer(10);
    Integer i6 = new Integer(20);
        
    System.out.println(i1 == i2);//true    (1)
    System.out.println(i3 == i1 + i2);//true    (2)
    System.out.println(i1 == i4);//false    (3)
    System.out.println(i4 == i5);//false    (4)
    System.out.println(i6 == i4 + i5);//true    (5)
    System.out.println(20 == i4 + i5);//true    (6)

     "+" Operator does not apply to an Integer object, i4 and i5 first automatic unboxing to give 20, and then also automatically unpack i6 int value 20 is equal to

  11. String class, for example:
    Intern () method looks at whether there is a string constant pool of the same content, if there are references to the string is returned, otherwise add your own string into the constant pool
    String s1 = "hello";
    String s2 = "hello";
    String s3 = "hel" + "lo";
    String s4 = "hel" + new String("lo");
    String s5 = new String("hello");
    String s6 = s5.intern();
    String s7 = "h";
    String s8 = "ello";
    String s9 = s7 + s8;
    
    System.out.println(s1 == s2);//true    (1)
    System.out.println(s1 == s3);//true    (2)
    System.out.println(s1 == s4);//false    (3)
    System.out.println (s1 == s9); // false (4)
    System.out.println(s4 == s5);//false    (5)
    System.out.println(s1 == s6);//true    (6)
    • s1 and s2 are constant pool points to the same memory space, is equal to
    • S3 sub-strings are in constant pool; string concatenation is optimized during compilation, equal
    • Substring composition s4 is generated by creating a new object, the runtime memory space allocated unknown, unequal
    • s7 and s8, although string literal, splicing into S9, s7 and s8 are used as two variables, where memory space unexpected, not equal
    • s4 and s5 are stored in the stack, different addresses are not equal
    • s6 by intern () method, the string "hello" add constant pool, already exists and constant pool "hello" string, so the direct return address, so s1 and s6 point to the same address, is equal to

Chapter III Operator

  1. Equal to and not equal for all of the basic data types, while the other is not available for comparison operators boolean.
  2. == and! = Comparison is a reference to an object, but to compare the contents of the object equals method to use, you can use basic types == directly. But if you write your own class, but also to cover the job equals method in this class, because the default behavior of equals () is to compare references.
  3. If you want to get the result of rounding, you need java.lang.Math in the round () method, no additional import.
  4. Java does not require the sizeof () operator, because all the data types of all sizes in the machine are the same.
  5. String string = "Hello";
    string += "world";//string = "Hello world"
    

      string + = "world" does not change the string pointed objects, such that only a string pointing to an object of type String Further, the original string constant "Hello" is also present in the memory, and is not changed

  6. When an object is operated, the operation is actually a reference to the object; if "will assign one object to another object" is actually a "reference" assignment from one place to another
  7. In the Java language, the original data type when parameters are passed by value, and the packaging type parameters are passed by reference passed
    eight primitive data types are passed by value, all other data types are used in reference to transmission, since these eight basic data types of packaging are immutable class, passed by reference and will not change its value

    Integer a = 1;
    Integer b = a;
    b++;
    System.out.println("a = "+ a + "b = " + b);//a = 1, b = 2
    

      Since the Integer class is immutable class, the method does not provide a method to change its value; After the execution b ++, creates a new value of 2 is assigned to the Integer b

  8. Involving Java language byte, short, and when the type of char conversion, first of all these types of variable values ​​will cast int type, int type then calculated to give an int.

Chapter IV control the flow of execution

  1. Tags: as label1:
    the Continue label1 and interrupt internal and external iteration iteration, go directly to label1 place, then continued iteration.
    break label1 also breaks all iterations, to label1, but it does not re-enter iteration.
  2. foreach syntax is mainly used for arrays and containers:
    int f[] = new int[10];
    for (int x : f)
        System.out.println(x);

Chapter V initialization and cleanup

  1. Constructor is a special class name and method name of an identical object is automatically created when called, to perform initialization, the constructor is a special type of method, because it does not return value
  2. Each overloaded method must have a unique list of parameter type. The return value of the method to distinguish overloaded methods is not feasible.
  3. this keyword can only use internal methods, means "that object to invoke a method" for references
  4. You can call a constructor with this, but you can not call two at the same time, and you must call the constructor on the most starting position
  5. If you call another method inside the same class method, you do not use this, you can call directly.
  6. static method is not this way.
  7. Each base class members will have a data type of the initial value, object reference is initialized to a default null, it may be initialized and configured by other means, but does not affect the default initialization process.

  8. Static initialization only once in a generation when the first object of this class or first visit to a static data members of this class, after which static objects are not initialized again
  9. The initialization sequence is first static objects (if they are not because the previous object creation process is initialized), followed by "non-static objects."
  10. Array has an inherent member length, is read-only, immutable
    Arrays.toString () method will produce a printable version of a one-dimensional array

  11. finalize () method
    works: Once the garbage collector is ready to release the memory occupied by objects, it will first call finalize method, and when the next garbage collection action occurs, will really recovered memory occupied by the object.
    and finalize difference destructor:
    the c ++, the object will be destroyed, and the java object has not always be garbage collected, or in other words, the object may not be garbage or garbage collection is not equal to "destructor . "
    Before you no longer need an object, if you must perform some action, then you have to do it yourself, you must create a common method to perform cleanup work. For example, suppose an object in the process of creating their own will draw on the screen, if not explicitly erased from the screen, it may never be cleaned up. If you add some kind of erasing functionality finalize method, when garbage collection, finalize invoked, the image will be erased. If garbage collection does not happen, the image would have been preserved.

    Will finalize it should not as a general clean-up methods, the real purpose is to finalize the release by means other than those created some objects allocated storage space occupied by the memory for the object, such as free as a function of c / c ++, so the need to finalize the call it with native methods.
  12. System.gc () is used to force the end of the operation.
  13. Local variables must be manually initialized class variables do not need.

Chapter VI Access Control

  1. If you do not provide any access modifier, it means that it is "packet access."

Chapter VII multiplexing class

  1. Both methods reuse code:
    1) only generates an object existing classes in the new class, since the new class is an object class of the conventional composition, so this method is called composition (has a).
    2) According to the prior class types to create a new class, an existing class without changing the form, the form of an existing class and add new code, this method is called inheritance (is a).
  2. You must call the base class constructor statement explicitly using the super keyword.
  3. Inherited initialization sequence:
    parent class static variable ---> --- parent class static block of code> subclass static variables ---> --- subclass static block of code> parent non-static variable ---> parent non-static block ---> parent class constructor ---> subclass non-static variable ---> subclass non-static block ---> subclass Constructors
  4. Why is it called the transition up?
    Because upward transition from a more specialized type of conversion to the more common type, so it is always safe. That is, the derived class is a superset of the base class. His methods may contain more than the base class, but he must have at least the base class contained. During the upward transition, the class interface only thing that may occur is missing method, instead of getting them. This is why the compiler "never made it clear that restructuring" or in case of "never designated a special mark" still allow upward transition of reasons.
  5. final: Data / Method / Class
    final data : 1. a compile-time constant never changes; 2 is a value initialized at runtime, and you do not want it to be changed.
    For basic data types, final value does not change so; for object references, final make constant reference, once the reference is initialized to point to an object, then it will not be able to point to another object. However, the object itself can be modified.
    pulic static final: can be used outside of the package, only one, is a constant.
    Final static type with substantially constant initial value (compile-time constants) all capital letters of
    final parameters: java allows declaratively specified as final parameter in the parameter list, which means that the parameters can not be modified reference points in the process Object.
    final Methods: Two reasons. 1) The method locked to prevent any inheriting class to modify his meaning; 2) in the past because of efficiency, in the most recent version of the java virtual machine can optimize the efficiency, eliminating the need to optimize the final method.
    Private methods the class are designated as being implicitly final.
    final class: do not intend to extend this class, do not want him to have subclasses. Method final class are implicitly designated as final.
  6. Compositions generally be part of an existing type as the underlying implementation of new types of multiplexing, the multiplexing is an interface inheritance. Although object-oriented programming inheritance strongly emphasized, but at the beginning of a design, a combination of general priority, only when necessary to use in determining inheritance. Because the combination of flexibility.

Guess you like

Origin www.cnblogs.com/mcq1999/p/12026549.html
Recommended