In order to take care of my friends who just started learning Java, I specially found the beginners’ tutorial at that time:

Ps: Help you learn Java:Small white entry to proficient > > > > >

We have time to look at the Java beginners to learn video tutorial, this set of tutorials of the total number of playback more than 100 million, a very shocking number, learning this set of tutorial programmers have 500 thousand +, such a good tutorial is not worth watching?



Is: u u u u

Abnormal: means not normal. An abnormal condition that occurs while a program is running. It’s a problem in the program. The problem is described in terms of object-oriented thinking and encapsulated as objects. Multiple attribute information, such as the cause, name, and description, exists when a problem occurs. When multiple attribute information is present, the most convenient way is to encapsulate the information. Exceptions are objects that Java encapsulates in terms of object-oriented thinking. This makes it easier to operate on problems and to deal with them.

There are many kinds of problems, such as corner mark out of bounds, null pointer, etc. Classify these questions. And these problems have common content such as: each problem has a name, but also the problem description of information, the location of the problem, so you can continue to extract upward. Formed an anomaly system.

Java. Lang. Throwable:

-Chuck: It’s Throwable.

| – Error: Error, in general, not specific code for processing, is usually the JVM, need to modify the program.

| – Exception: abnormal, can be targeted treatment

Whether error or exception, they have a specific subclass to reflect each problem, their subclasses have a common, that is, to the name of the parent class as the suffix name of the subclass.

All classes and objects in this system have a unique characteristic: it is disposable.

May throw sex: is the system of the classes and objects can be throws and throw two key operation.


class ExceptionDemo{

public static void main(String[] args) {

// byte[] buf = new byte[10241024700]; / / Java. Lang. OutOfMemoryError out of memory error

}

}

During development, if a feature is defined and something goes wrong with it, the problem should be flagged when the feature is defined, so that the caller can anticipate what to do when the feature is used.

How do you label it?

The completion is done by using the throws keyword. The format is: throws exception class name, exception class name…

With this notation, the caller, when using the feature, must handle it or the compilation will fail.

There are two ways of processing: 1. Capture; 2. 2. Throw.

For capture: Java targeted blocks of statement processing.

try {

Code that needs to be tested;

}

Catch (exception class variable name){

Exception handling code;

}

fianlly{

Code that is bound to execute;

}


Catch (Exception e) {//e is used to receive Exception objects detected by the try.

System.out.println(“message:”+e.getMessage()); // Get the exception information.

System.out.println(“toString:”+e.toString()); // Get the name of the exception + the information about the exception.

e.printStackTrace(); // Print the exception in the stack; Exception name + Exception information + exception location.

}

Exception handling principle: if a function throws several exceptions, if a function call is to be tried, it needs to be handled by the corresponding catch block.

Special case: If there are more than one catch for a try, the catch block must be placed below it.

The difference between throw and throws keyword:

1. Throw is used to throw an exception object, followed by an exception object. Throw is used inside a function.

Throws an exception class. The name of the exception class can be multiple, separated by commas (,). Throws is used on functions.

In general: if the contents of a function have a throw object and are not handled, then the function must be declared or the compilation will fail. But there are special circumstances.

There are two kinds of anomalies:

1: compile-time checked exceptions, as long as Exception and its subclasses are compile-time checked exceptions.

2: run-time exceptions, where Exception has a special subclass RuntimeException, and a subclass of RuntimeException is RuntimeException, which means that this Exception is not checked at compile time.

The difference between compile-time checked exceptions and runtime exceptions:

If an exception is thrown in a function, the function must be declared, otherwise the compilation fails.

2, the reason for the declaration: the exception needs to be handled by the caller.

3. Runtime exceptions that are thrown inside a function do not need to be declared on the function.

The reason for not declaring: there is no need for the caller to handle, the runtime exception occurs, and the program can no longer be allowed to continue running, so if the call is not allowed to handle, the program is directly stopped, and the code is corrected by the caller.

When do we define exception handling, when do we define try and when do we define throws?

1, if there is an exception inside the function, if there is an exception inside the function, use try.

2. If the function cannot be handled internally, it must be declared and let the caller handle it.

Custom exception: When the development, the project appeared in Java has not been defined in the problem, then we need to establish the idea in accordance with The Java exception, the project in the special problem also carry on the object encapsulation. This exception is called a custom exception.

For division, zero is not allowed as a divisor. Java describes this problem with the ArithmeticException class. For this function, in our project, divisor should not be considered negative in addition to 0. But the negative number part of Java is not for description. So we need to customize this exception.

Steps to customize an exception:

1: Define a subclass that inherits Exception or RuntimeException, making the class disposable.

2: Throws or throws.

The idea of exception conversion: when an exception occurs that the caller cannot handle, it needs to be converted into an exception that the caller can handle.

Try catch finally

1, try, catch, finally

2, try, catch

3, try, finally

In this case, if an exception occurs, it is not handled, but the resource must be shut down, so the try finally collection only closes the resource.

Remember that Finally is useful when the primary user closes the resource. Resources must be shut down regardless of whether an exception occurs.

System.exit(0) ; // Exit the JVM, in which case finally does not execute.

When an exception occurs, there are some new features when the subparent overwrites:

1: When a subclass overrides a method of a parent class, if the parent class method throws an exception, then the subclass method either does not throw an exception or the parent class or a subclass of that exception cannot throw any other exception.

2: If the parent class throws multiple exceptions, then the subclass can only throw a subset of the parent class’s exceptions when overwriting.

Note:

If a subclass has not thrown an exception, then a subclass may not throw an exception. If a subclass overwrites a method that has an exception, only a try can not throw an exception.

Throw a RuntimeException in a subclass’s method if the subclass cannot handle the exception, and the subclass’s method has an effect on the operation of the subclass’s method. You can throw a RuntimeException in the subclass’s method by throwing the exception.

Common exceptions:

1, the abnormal foot the cross-border (IndexOutOfBoundsException) including arrays, strings;

NullPointerException

2. Cast exception: ClassCastException

No NullPointerException for this element

4. Abnormal operation is not supported;

Exceptions should be avoided as much as possible, and if they cannot be avoided, they should be handled in advance. Such as home medicine, such as fire extinguishers.


Packages: Packages are defined using the package keyword.

1: Manages class files by category.

2: Provide multiple namespaces for class files.

If the generated package is not in the current directory, it is best to execute classpath and define the parent directory of the package in the classpath variable.

It is common to define a package name because a package exists to distinguish between classes with the same name. So the package name should be as unique as possible. How do you guarantee uniqueness? You can use the URL domain name to define the package name.

packagepack; // Define a package named pack. Note: Package names are written in lowercase.

//package cn.itcast.pack.demo;

The full name of the class is the package name. The name of the class

Compile command: javac -d location (.current path) Java source file (to automatically generate packages)

A package is a form of encapsulation used to wrap a class. To be accessed by a program other than the package, the class must be public.

Members of a class that are accessed outside the package must also be public.

There are two types of permissions available for package to package access:

1: public

2: protected: a privilege that can only be used by subclasses of different packages.

To summarize the four types of permissions in Java:

Scope public protected Default private

Ok, OK, OK, ok in the same class

Ok ok OK in the same package

A subclass of ok

Ok in different packages

Import – Import: The class name becomes longer and is cumbersome to write. For simplicity, a keyword is used: import, which can be used to import classes in a specified package. Remember: in actual development, the class to which the import, not recommended to use *.

import packa.*; // This simply imports all the classes in packa’s current directory. Contains no subpackages.

import packa.abc.*; // Import all current classes in the subpackage ABC of packa.

If classes with the same name exist in two packages that you import. If this class is used, you must specify the package name in your code.

Common software packages:

Java. lang: Language Is the core Java package. After the Object System String Throwable JDk1.2 version, the classes in this package are automatically imported.

Java.awt: Defines objects that are used for Java graphical interface development.

Javax. swing: Provides all Windows desktop applications including controls, such as: Frame, Dialog, Table, List, etc., is the Java graphical interface library.

Java.net: Objects used for Java network programming aspects are in this package.

IO: input Output Objects used to manipulate data on the device are in this package. For example, reading and writing data to a hard disk.

Java.util: Toolkit for Java, time objects, and collections framework.

Java. applet: Application +let Client Java applet. Server +let — > Servlet server Java applet


Jar: Java compression package, mainly used to store class files, or configuration files, etc.

Syntax: jar – cf package name. jar package directory

Decompress: jar – XVF Package name. Jar

Redirect the list of jar package directories to a file: jar -tf package name. jar >c:\1.txt

So much for object orientation in this lesson, and a preview of the next article: Nine: Multithreading

Congratulations you read an article again, knowledge reserve rises in a straight line, do you have the power that insists to study in the future!! Insist on victory, I insist on the update, you do not insist on a little bit

Ps: No future in sleep ( ̄o ̄). Z z

In order to take care of the friends who just started to learn Java, I specially found the beginners tutorial: to help you learn Java: small white entry to proficient > > > > >

We have time to look at the Java beginners to learn video tutorial, this set of tutorials of the total number of playback more than 100 million, a very shocking number, learning this set of tutorial programmers have 500 thousand +, such a good tutorial is not worth watching?

If you are interested in Java, watch the video above to join the Java programmers!!