Ali P8 Architect Java core knowledge finishing point: Java basic principle + spring + + algorithm microService

Java foundation

5.1.1. JAVA abnormal handling and classification

5.1.1.1. The concept

If a method can not complete the tasks in the normal way, you can exit the method by another route. under these circumstances

It will throw an object that encapsulates the error message. At this point, this method will return immediately quit without any value. In addition, calls

Other code this method also can not continue, exception handling mechanism to execute the code to the exception handler.

5.1.1.2. Abnormal classification

Throwable Java language is all wrong or abnormal superclass. The next layer is divided into Exception and Error

Error

1. Error category refers to internal errors and resource depletion system java runtime error. Applications will not throw objects in that class. in case

There has been such a mistake, except notify the user, and the rest is try to make the program safe termination.

Exception(RuntimeException、CheckedException

2. Exception there are two branches, one is RuntimeException abnormal operation, is a

CheckedException。

RuntimeException such as: NullPointerException, ClassCastException; a check exception

IOException CheckedException, such as I / O errors due to, SQLException. RuntimeException is

Superclass those exceptions might be thrown during the normal operation of the Java Virtual Machine. If RuntimeException occurs, then a

Given programmer error .13 / 04/2018 Page 102 of 283

Abnormalities CheckedException : General external error, such anomalies occur at compile time, Java compiler will be strong

System program to catch such exceptions, will appear asking you to put this program may be abnormal try catch, a type of abnormal

Generally includes several aspects:

  1. Trying to read the data in the end of the file
  2. Trying to open a malformed URL
  3. Attempts to find the class object based on a given string, and the string representation of this class does not exist

5.1.1.3. Abnormal handling

The problem is not encountered specific treatment, but continued thrown to the caller (throw, throws)

An exception is thrown, there are three forms, one throw, a throws, there is a system to automatically throw an exception.

public static void main( String[] args )
{
	String s = "abc";
	if ( s.equals( "abc" ) )
	{
		throw new NumberFormatException();
	} else {
		System.out.println( s );
	}
}
int div( int a, int b ) throws Exception
{
	return(a / b);
}

try catch catch the exception targeted approach

. 5.1.1.4 Throw and throws difference:

Different locations

1. throws used in the function, is followed by an exception class with a plurality; and throw used within a function, is followed by an exception object.

Different functions:

2. throws used to declare an exception, let the caller know that the function issues that may arise can be given in advance of treatment; throw throw objects specific problems, perform a throw, the function has ended, jump to the call who will target specific issues thrown to the caller. That is a throw statement exist independently, following Do not define other statements, because unreachable.

3. throws represents a possibility of abnormal, not necessarily these anomalies occur; throw an exception is thrown, throw it must perform some kind of thrown exception object. 13/04/2018 Page 103 of 283

4. Both are negative treatment of abnormal, or just throw might throw an exception, but not to deal with the abnormal function, real handle exceptions invoked by the upper layer function processing.

5.1.2.JAVA reflection

5.1.2.1. Dynamic languages

Dynamic language, refers to the program can change its structure at runtime: new functions can be introduced, existing functions can be deleted and other structural changes. Such as common JavaScript is a dynamic language, in addition to Ruby, Python and other dynamic languages ​​also belong, and C, C ++ is not part of the dynamic languages. JAVA reflection angle from said semi-dynamic language.

5.1.2.2. Concept of reflection (the operating state of all known class attributes and methods)

在 Java 中的反射机制是指在运行状态中,对于任意一个类都能够知道这个类所有的属性和方法;并且对于任意一个对象,都能够调用它的任意一个方法;这种动态获取信息以及动态调用对象方法的功能成为 Java 语言的反射机制。

5.1.2.3. 反射的应用场合

编译时类型和运行时类型

在 Java 程序中许多对象在运行是都会出现两种类型:编译时类型和运行时类型。 编译时的类型由声明对象时实用的类型来决定,运行时的类型由实际赋值给对象的类型决定 。如:

Person p=new Student();

其中编译时类型为 Person,运行时类型为 Student。13/04/2018 Page 104 of 283

的编译时类型无法获取具体方法

程序在运行时还可能接收到外部传入的对象,该对象的编译时类型为 Object,但是程序有需要调用该对象的运行时类型的方法。为了解决这些问题,程序需要在运行时发现对象和类的真实信息。

然而,如果编译时根本无法预知该对象和类属于哪些类,程序只能依靠运行时信息来发现该对象和类的真实信息,此时就必须使用到反射了。

5.1.2.4. Java 反射 API

反射 API 用来生成 JVM 中的类、接口或则对象的信息。

  1. Class 类:反射的核心类,可以获取类的属性,方法等信息。
  2. Field 类:Java.lang.reflec 包中的类,表示类的成员变量,可以用来获取和设置类之中的属性值。
  3. Method 类: Java.lang.reflec 包中的类,表示类的方法,它可以用来获取类中的方法信息或者执行方法。
  4. Constructor 类: Java.lang.reflec 包中的类,表示类的构造方法。

5.1.2.5. 反射使用步骤(获取 Class 对象、调用对象方法)

  1. 获取想要操作的类的 Class 对象,他是反射的核心,通过 Class 对象我们可以任意调用类的方法。
  2. 调用 Class 类中的方法,既就是反射的使用阶段。
  3. 使用反射 API 来操作这些信息。

5.1.2.6. 获取 Class 对象的 3 种方法

调用某个对象的 getClass()方法

Person p=new Person();

Class clazz=p.getClass();

调用某个类的 class 属性来获取该类对应的 Class 对象

Class clazz=Person.class;

使用 Class 类中的 forName()静态方法(最安全/性能最好)

Class clazz=Class.forName(“类的全路径”); (最常用)

当我们获得了想要操作的类的 Class 对象后,可以通过 Class 类中的方法获取并查看该类中的方法和属性。

//获取 Person 类的 Class 对象
Class clazz=Class.forName(“reflection.Person”);13/04/2018 Page 105 of 283

//获取 Person 类的所有方法信息
Method[] method=clazz.getDeclaredMethods(); for(Method m:method){ System.out.println(m.toString()); }

//获取 Person 类的所有成员属性信息
Field[] field=clazz.getDeclaredFields(); for(Field f:field){ System.out.println(f.toString()); }

//获取 Person 类的所有构造方法信息
Constructor[] constructor=clazz.getDeclaredConstructors(); for(Constructor c:constructor){ System.out.println(c.toString()); }

5.1.2.7. 创建对象的两种方法

Class 对象的 newInstance()

1. 使用 Class 对象的 newInstance()方法来创建该 Class 对象对应类的实例,但是这种方法要求该 Class 对象对应的类有默认的空构造器。

调用 Constructor 对象的 newInstance()

2. 先使用 Class 对象获取指定的 Constructor 对象,再调用 Constructor 对象的 newInstance()方法来创建 Class 对象对应类的实例,通过这种方法可以选定构造方法创建实例。

//获取 Person 类的 Class 对象
Class clazz=Class.forName(“reflection.Person”);

//使用.newInstane 方法创建对象
Person p=(Person) clazz.newInstance();

//获取构造方法并创建对象
Constructor c=clazz.getDeclaredConstructor(String.class,String.class,int.class);

//创建对象并设置属性
13/04/2018 Page 106 of 283 Person p1=(Person) c.newInstance(“李四”,“男”,20);

5.1.3. JAVA 注解

5.1.4. JAVA 内部类

5.1.5. JAVA 泛型

5.1.6. JAVA 序列化(创建可复用的 Java 对象)

5.1.7. JAVA 复制

以上这些都是Java基础中的核心知识点整理,还有跟多的Java架构核心详细解析包含(spring原理,JVM,多线程并发,微服务,设计模式,数据结构与算法等等)经过阿里P8架构师韩飞龙老师花费半年时间的整理,已形成为一份PDF文档资料,相信看完后收获会颇丰,对于Java架构的核心知识点理解会更加的透彻。这份资料现在已经分享到粉丝社群。需要可下方评论区留言。根据需求,下篇文章分享。

Guess you like

Origin blog.csdn.net/weixin_45136579/article/details/91445185