java foundation - "JAVA programming language and data structure," notes

Recently almost the java language learning part of it again, here marking the contents of the book before recording it, consolidate the foundation.

A, classes and objects

1. java Default: Data Type field reference is null, a default value in the Value field of the data type is 0, the default boolean type data field is false, the default value of char type data field "\ u0000".

  Wherein, Java does not assign a default value to a local variable in the method .

2. The visibility modifiers: private modifier can only be applied to the members of the class, and can be used on public members of the class or classes. Use modifiers public and private will be on the local variable causes a compilation error.

3. Variable Scope: Scope instance variables and static variables is the entire class.

4.this statement: java specified, the statement in the constructor this should appear before any other executable statements.

5. packaging: java in consideration of the performance of the basic data types not used as an object. For convenience, java.lang package which provides various basic data corresponding to the type of packaging.

 No packaging constructor with no arguments, can use the basic data types or values ​​represented by a numeric string constructed packaging. Such as: new Double (4.2) or new Double ( "4.2").

 The conversion process between the primitive values ​​corresponding wrapper class called "packing" or "unpacking."

6.String class: String variable is stored String objects immutable reference, value is a string of String objects in storage. However, the convention, the term is often used to represent a string variable value String, String, and the string objects.

7. The interconversion between the numerical or character string: Double.parseDouble ( "5.44"); String.valueOf (5.44).

8. constructor chain: in any case, an instance of a class structure , along the constructor will call the parent class inherits all chains, this process continues until the last is called a constructor along the inheritance hierarchy until.

Here super statement can be understood, if not in the subclass constructor super statement, the compiler will automatically in the first line of the "super statement to add a row."

So, when designing a class is inherited, it is best to provide a no-argument constructor method to avoid this situation: the subclass Apple does not explicitly define a constructor method when instantiating'll call your own default no-argument Constructors before no-argument constructor calls the parent class Fruit, but because the definition of the parent class constructor have parameters, the compiler will not provide a default constructor with no arguments, call fails, the compiler will go wrong. code show as below:

1 public class Apple extends Fruit {
2 }
3 
4 class Fruit {
5     public Fruit(String name) {
6          System.out.println("Fruit's constrctor is invoked");
7    }  
8 }

In addition, super statement can not only call the constructor of the parent class, ordinary parent class can be called.

Note that, only the implicit call subclass constructor (here three cases can be:.. A display is given with a constructor parameter, b show the constructor with no arguments are given, C has not given construction method. when the compiler is given constructor with no arguments) default,

I.e. sub class is instantiated, the constructor will be called chain; constructing explicit call subclass (e.g., super statement), it does not call the constructor with no arguments parent class.

9. The method of rewriting: present in succession, i.e., subclass rewrite the parent class. Between overridden method must have the same signature (function name and parameter list must be exactly the same), but returns may be the same or compatible type,

   Return type subtype return type parent process to be rewritten that is compatible with the method of rewriting subclass. java syntax provided rewrite marked to avoid errors.

public class Circle extends GeometricObject {
 // other methods

    @override
    public String toString(){
         return super.toString() + "\nradius is " + radius;
    }
}

 Among them, static methods can not be overridden. Because if the static methods of the parent class is redefined in a subclass, then the parent class static method will be hidden. But you can still use (the name of the parent class static method name) called.

10. The method overloading: function names must be identical, but different parameter list.

    All java classes inherit from the Object class, and Object has a toString () method.

11. Polymorphic: since sub-class "is greater than" parent class, if A is the parent of B, then A contains B, in Java can be used to refer to a variable of type A object B, then the compiler will automatically B does not belong to the portions a negligible.

   Polymorphism means that a parent can refer to a variable of type Object subtype. That is, when a new object instance, the declared type of the parent type, but the actual type is a subtype.

  Dynamic binding: The reference variable method is called by its real type determines which specific method calls. JVM has to be a subclass of the root class (Object Class) looking for from this method, once you have found a realization, we stop searching, you can call.

 But it should be noted that, although the specific method calls which is determined by the actual type of the variable, the JVM dynamic binding method at runtime. However, the reference variable declaration type determines the compiler which method to match compile time .

12. explicit conversions: The parent class explicit reference variable into corresponding subclass reference type, causing the compiler to understand the intent of the code.

   The purpose is to avoid explicit conversion compilation errors occur, because the declaration reference variable type determines which method match at compile time, and the parent types are possible ways subtype called does not exist.

 Further, the object member access operator has higher precedence than cast operator precedence, embodied in the code as follows:

((Circle)object).getArea();

When the value of the basic types of explicit type conversion, the statement will return a new value and the original value of the variable unchanged; but explicit reference variable is converted and does not create a new object:

. 1  int Age = 45 ;
 2  byte Newage = ( byte ) Age; // Newage a new value will be created. 
. 3  
. 4 Object o = new new Circle ();
 . 5 Circle c = (Circle) 0;   // reference variables o and c refer to the same object. It does not create any new objects

13. equals method and == Comparison operators: comparison operator for comparing two values ​​of the underlying data types are equal or two reference variable target address is the same, i.e. whether it is pointing to the same object.

     Object class equals the default method is implemented:

1 public boolean equals(Object obj) {
2       return (this == obj);
3 }

   however. equals method are rewritten in many types of JAVA API for comparing the contents of two objects are equal. Such as java.lang.String and java.util.Date.

  Since the function of rewriting the signature should be identical, overwrite the equals method of Object to be received parameter types, namely:

1 @Override
2 public boolean equals(Object o) {
3    if (o instanceof Circle)
4          return radius == ((Circle)o).radius;
5    else
6         return false;
7 }

14. The visibility modifiers

 Modifiers The same class In the same packaging Different subclasses packet Different classes package Feizi
 public 1 1 1 1
 protected 1 1 1 0
 default 1 1 0 0
 private 1 0 0 0

 

 

 

 

 

 

 

15. Constructor Summary:

Examples of constructed class constructors, properties, and methods different from the class, sub-class does not inherit the parent class constructor , they can only be called from explicit constructor subclass by super statement.

Of course, the constructor may call the constructor overloads or parent class constructor , the call must be in the constructor of the first statement appears.

Without this call, the compiler will super () Default constructor about the first statement to call the parent class constructor with no arguments .

Second, exception handling

1. floating-point division by zero is no exception.

2. The fundamental advantage of exception handling is that the method is called in detecting errors from processing errors caller separated in.

3.java abnormality in the class is divided into three types: system error, exception and runtime exceptions.

   JVM system error is thrown, he expressed Error class.

   Exception class is represented by an abnormal, including class ClassNotFoundException, IOException class and the like.

  When the abnormal operation RuntimeException class represents, it describes error codes. As ArithmeticException class, NullPointerException class, IndexOutOfBoundsException class, IllegalArgumentException class and the like.

  In which the runtime system errors and exceptions and their subclasses are called exempt abnormal , all other exceptions are called will be seized exception. java requires all must abnormality detection shall declare the method and using the first try catch block processing thereof .

  Because any code are exempt abnormalities may occur, for they all java is not mandatory.

 Note that, if there is no declaration will review the exception in the parent class method, then its time to rewrite its subclasses can no longer unusual statement.

4. exception trap: various exception classes can be derived from a common parent class, then a catch block may capture a parent class of the exception object, it also captures the exception object that all subclasses of the parent class.

  Therefore, the order of importance of the catch block, if the parent class exception catch block in front of the catch block subclass abnormal, in which case the catch block subclass would be empty, it will cause a compiler error.

5. chained exception: the exception is thrown upwardly continue captured the catch block. code show as below:

. 1  the catch (Exception EX) {
 2      the throw  new new ( "New info from the method1", EX) Exception // here two throws exception 
3 }

6. custom exception class, the subject will be making the best abnormality, so that the compiler can be forced to catch them.

Simplify code catch block 7. try-with-resourse statement to avoid errors at the same time.

Three abstract classes and interfaces

1. class contains abstract methods must be abstract class, but an abstract class can not all abstract methods. Abstract class constructor is defined as protected. Abstract method is non-static.

 Even if the parent class is a subclass of the concrete, this subclass may be abstract. An abstract class can not be instantiated.

2. When one kind of an interface similar to a class of structures, contains only constants and abstract methods, static methods and methods as well as the default .

   All data fields interface are public static final modification; Interface methods are public abstract modification.

   These modifiers java allows writing interface code is omitted, but a subclass must implement a method defined as public.

In addition, java8 began, and the introduction of a default interface method public static method. Such as:

. 1  public  interface A {
 2     // default interface methods 
. 3     public  default  void doSomething () {
 . 4         System.out.println ( "the Do something" );
 . 5     }
 . 6  
. 7     // interface public static method 
. 8     public  static  int the getValue () {
 . 9         return 0 ;
 10     }
 . 11 }

3. Object class clone method of automatically all data fields shallow copy .

4. A class inherits only a parent, but may implement one or more interfaces; an interface can inherit one or more interfaces, but not inherited class.

  variable Construction method method
 Abstract class Unlimited Constructor chain, not new Unlimited
 interface All variables must be public static final No constructor, not new Abstract example method may comprise the public, and the public default method public static methods

 

 

 

 

Third, Generics

 

Fourth, multi-threaded

 

Guess you like

Origin www.cnblogs.com/miaoxiaonao/p/10795280.html
Recommended