JAVA-Basic knowledge

Source: Internet
Author: User
Tags finally block modifiers

1. Exception:

1) Classification:

A. Check for exceptions: At compile-time check the program there are some exceptions that programmers cannot foresee, such as opening a nonexistent file, the programmer does not know whether the file exists. Major check-up exceptions include:

ClassNotFoundException: The program cannot find the corresponding class when attempting to load the (import) class;

Clonenotsupportedexception: The Cloneable interface cannot be implemented when invoking the Clone method of the object class.

Todo:add some more exception types here

B. Operational exceptions: Exceptions in programmer code, such as an index that accesses an array does not exist, a sort index is out of range, NULL is used where the object needs to be used, and so on. can be ignored at compile time.

Todo:add Runtime exception types here

2) Catching exceptions

try {

Program code

} catch (Exceptionname E1) {

Catch Block

} catch (Exceptionname E2) {

Catch Block

} finally {

Program code

}

Tips:

A. Catch captures multiple types of exception, executes the code of a catch block when the first is satisfied, and then, when not satisfied, determines whether the following catch condition is met.

B. Finally block whether or not the try is always executed, you can run some cleanup finishing work in finally.

3) Throws/throw

Throws an inspection exception that can be caught at the last specified method in the method signature;

Throw throws an exception that can be either newly instantiated or just captured.

2. Modifiers

1) Access modifiers

A. Default: Visible within the same package.

B. Private: Visible within the same class.

C. Shared (public): All classes are visible.

D. protected (Protected): visible to classes and all subclasses within the same package.

Todo:add a chart to show the access right of each types

2) non-access modifiers

A. Static variable: Independent of object, only one copy, also known as class variable;

|-static method: Independent of object, cannot use non-static variable of class, get data from parameter list.

B. Final-final variable: The display is initialized once and cannot point to a different reference, but the data can be changed;

|-Final method: can be quilt class inheritance, not quilt class modification;

|-Final class: Cannot be inherited by any attribute.

C. Abstract-abstract class: You cannot instantiate an object, and in order to augment it in the future, you can include abstract methods and cost abstractions. If a class contains an abstract method, it must be declared as an abstract class.

|-Abstract method: Without any method of implementation, the subclass of the inheriting abstract class must implement all the abstract methods of the parent class, or else declare it as an abstract class. Abstract classes can contain no abstract methods.

D. Synchronized

E. Transient

F. Volatile

3. Operator-related

automatic promotion of type: short + int = int!-> short;

= = and equals:

= =: Two basic types are equal, and if they are reference types, the memory is compared.

equals: Compares the values of two independent objects, meaning that the values may be the same even if their memory addresses are different.

What ' s more:

At the time of assignment, if it is the basic type, it is to open a space for the new variable in memory, and write the value of the original variable, so that the modification of the original variable will not affect the new variable; if it is a reference type (class object), it is to point the memory address to the new reference, and the modification of the original variable

4. the original data type and its encapsulation

For example int and integer, the default value for int is NULL for the default value of 0,integer, so that the integer can differentiate between assigning a value of 0 and not being used. The integer provides operations that are related to integers, and the integer max_value and min_value represent the maximum and minimum values for an integral type.

5. Rounding Correlation:

Ceil:up to Integer

Floor:down to Integer

Round = Floor (x + 0.5)

6. Overrides and Overloads

Completely different concepts, the same point is just the same as the method name. Different points are mainly embodied in the parameter list, the return value, and the other rewrite is inherited under the concept.

Overriding: Subclasses override the parent class's methods, and the parameters and return values are exactly the same, with fewer exceptions than the parent class, and greater access rights. Private cannot be overridden.

Overloading: A method with the same name that has multiple different parameter lists in the same class cannot be overloaded with access, return type, or exception thrown.

The constructor (Constructor) cannot be inherited, so it cannot be overridden and can be overloaded.

7. Inheritance of interfaces and abstract classes

Interfaces can inherit interfaces (implements);

An abstract class can implement an interface, or it can inherit concrete, and can have a static main method.

The only difference between an abstract class and a concrete class is that an abstract class cannot be instantiated, and an abstract method is allowed.

8. Object-oriented features

Encapsulation, inheritance, abstraction, and polymorphism

Package: Cohesion Poly, low coupling. Objects are the most basic units of encapsulation, the properties of an object are defined by variables, behaviors are defined by methods, and methods can access variables in the same object. Principle: The methods and data that operate on the same thing are placed in the same class. The e.g people circled on the blackboard. This method should belong to people, Blackboard and Circle which of the three objects? The answer is circle. Circle This action is the operation of the ring, people just call this method only.

abstraction: extracts similarities and commonalities of things and then sorts them into a class. Abstractions include two aspects of behavioral abstraction (methods) and State abstraction (variables). Abstraction is good for stripping off non-caring or unimportant traits, while preserving only the things that matter most and what you need to focus on today.

inheritance: developed on the basis of the implemented classes, can add new content, or modify the original content, improve the reusability and extensibility of the code.

Polymorphic : defines a reference variable and emits a method that does not specify a particular class, and the execution of this method is determined only when it is run. This allows the reference variable to be bound to different classes without modifying the source code, thus invoking different methods, enhancing flexibility and extensibility.

The mechanism for implementing polymorphism is that a reference variable of a parent class or interface definition can point to a subclass or an instance object of a specific implementation class, so that, at run time, the instance object that the reference variable points to is determined, and the method of the object is called, not the method in the reference variable type.

9. Abstract class and interface:

An abstract class cannot create an instance object. Classes that contain abstract methods must be abstract classes, and methods in abstract classes do not have to be abstract. Abstract methods in abstract classes are implemented specifically in subclasses, and subclasses must be abstract if there are no abstract methods implemented in the subclass. An interface is a special case of an abstract class, and all methods in an interface must be abstract, and the default type of the method is public abstract, and the default type for all variables is public static final.

The difference between the two:

1> abstract class can have a construction method, interface cannot have;

2> Abstract classes can have ordinary member variables, the member variables of the interface are public static final type;

3> Abstract classes can have non-abstract ordinary methods, public and protected type, interface methods are abstract;

4> abstract class can have static methods, interface can not;

5> abstract classes can have static member variables, interfaces can be but only public static final type;

6> A class can inherit multiple interfaces, but can inherit only one abstract class.

In terms of applications, interfaces are used to define communication contracts between modules in terms of system architecture design. Abstract classes can implement code reuse in terms of code implementations.

Inner class and Static Nested class

An inner class is a class that is defined inside a class. Static member variables are not allowed in inner classes, and methods in external classes can be accessed directly. An inner class can be defined outside the method of an external class or defined in the method body of an external class. An inner class that is defined outside a method of an outer class, similar to a member variable of an external class, can have four types of access, and when creating an instance object, you first create an object of the outer class, and then create an object of the inner class. An inner class in the method body of an outer class, similar to a local variable of the method, must be declared before it can be used, not accessed by another class, but passed to another class. The inner class can access local variables in the method body, but must add the final modifier.

Before the inner class defined by the method is static, it becomes the static Nested class, and there is no difference between the static class, only when programming references. The static nested class does not rely on an instance variable of an external class and can access the static variables of the outer class. The external class can directly access the name of the static nested class, and the static nested class can also directly access the static member variable of the external class without prefixing it.

String,stringbuffer,stringbuilder.

String is not the most basic data type, and the most basic data type is boolean,byte,char,short,int,long,float,double. When the string object is not modifiable, after executing s = s + "World", the original string object pointed to by S is not modified, only s points to the newly generated string object, and the original object is still in memory. Therefore, if you are dealing with variable string objects, you can use StringBuffer to implement them. String has the Equals method, StringBuffer not. String is the final class and is not inheritable. Both StringBuilder and StringBuffer are modifiable strings, except that StringBuilder is not thread-safe and cannot be accessed synchronously. String has the length () method, and the array has the length property. Javac the "a" + "B" + "C" + "D" directly to generate a string at compile time

Try,finally,return.

If there is no return in the return,finally in the try, then the execution time of the finally statement is returned after the execution of the return. If both the try and finally have return, the return of the return,try that executes finally does not take effect.

final,finally,finalize.

The final modifier is used to declare properties (properties are not mutable), methods (methods are not overwritten), classes (classes are not inheritable). The local variables to be accessed by the inner class must be declared final. Finally is a part of the exception handling statement structure that is always executed. Finalize is a method of the object class that, when executed by the garbage collector, calls the method of the reclaimed class, and can override this method to provide some way to recycle resources, such as closing a file.

14. Exceptions

1> General Exception (common exception): The software determines the anomalies that can occur, which are foreseeable, need to be treated with try-catch, or throws to the previous level of the method.

2> Program exception (runtime exception): Runtime Exception, is compiled through but at run time to check out the exception, such as Nullpointerexception,arrayindexoutofboundsexception, Classcastexceptoin and so on, are programmers can not meet, but need to modify the exception.

JAVA-Basic knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.