二刷Java官方教程

第二次阅读Java官方教程,有些不一样的理解。内容比较散乱,个人意识流笔记。

Java官方笔记14流

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Processing Data in Memory The Stream API is probably the second most important feature added to Java SE 8, after the lambda expressions. In a nutshell, the Stream API is about providing an implementation of the well known map-filter-reduce algorithm to the JDK. map-filter-reduce: List<Sale> sales = ...; // this is the list of all the sales int amountSoldInMarch = 0; for (Sale sale: sales) { if (sale.getDate().getMonth() == Month.MARCH) { amountSoldInMarch += sale.getAmount(); } } System.out.println("Amount sold in March: " + amountSoldInMarch); map:通过get取值,将部分字段映射到新数据(select字段) ...

Java官方笔记13集合

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Storing Data The Collections Framework is the most widely used API of the JDK. 集合不是数据类型,它是JDK的API,可以用来存储数据等,相当于数据结构。 the Collections Framework is a set of interfaces that models different way of storing data in different types of containers. Then the Framework provides at least one implementation for each interface. There are two main categories of interfaces in the Collections Framework: collections and maps.(所以我猜想Python也是基于这样的考虑,设计了List和Dict,并作为Python基本数据类型,实际编码中要用到的也主要是这两类) That makes two main categories, Collection and Map, a subcategory, Queue, and a side category, Iterator. ...

Java官方笔记12异常

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Exception Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. the checked exception 比如,java.io.FileNotFoundException the error 比如,java.io.IOError the runtime exception 比如,NullPointerException error和runtime exception又叫做unchecked exception Catching and Handling Exceptions try { code } catch and finally blocks . . . try { } catch (ExceptionType name) { } catch (ExceptionType name) { } 使用|catch多个: ...

Java官方笔记11包

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Packages Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this section simply as classes and interfaces. 创建 //in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . } 文件顶部第一行,每个文件只能有1个package关键字。 ...

Java官方笔记10注解

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 注解 注解的作用: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. Runtime processing — Some annotations are available to be examined at runtime. 注解格式,使用@: @Entity 带key-value: @Author( name = "Benjamin Franklin", date = "3/27/2003" ) class MyClass { ... } 只有1个key时可以省略key: ...

Java官方笔记9Lambda表达式

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Lambda Expression 有了Lambda Expression,就不用再写anonymous classes。 写Lambda,首先要找到它的类型。 There is a restriction on the type of a lambda expression: it has to be a functional interface. 函数接口,只有1个抽象方法的接口: @FunctionalInterface public interface Runnable { public abstract void run(); } 默认方法do not count,所以下面这个也是函数接口: @FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { // the body of this method has been removed } } 静态方法也do not count,所以下面这个还是函数接口: ...

Java官方笔记8泛型

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 泛型 为什么需要泛型?generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. 说白了就像Python动态语言的变量,是动态的,可以指向任意类型。 泛型有个好处是不需要类型转换: List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); List<String> list = new ArrayList<String>(); list.add("hello"); String s = list.get(0); // no cast 这个例子的泛型,是指List的实现使用了泛型<T>,从而给使用带来了好处。 定义 class name<T1, T2, ..., Tn> { /* ... */ } 比如定义class List<T>,使用List<String> 将以下代码: public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; } } 改为泛型实现: ...

Java官方笔记7接口

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 接口 接口只能包含:constants, method signatures(abstract), default methods, static methods, and nested types 方法体只存在于:default methods and static methods 接口不能实例化,只能被类实现,或者被其他接口继承(接口可以多继承)。 定义接口: public interface OperateCar { // constant declarations, if any // method signatures // An enum with values RIGHT, LEFT int turn(Direction direction, double radius, double startSpeed, double endSpeed); int changeLanes(Direction direction, double startSpeed, double endSpeed); int signalTurn(Direction direction, boolean signalOn); int getRadarFront(double distanceToCar, double speedOfCar); int getRadarRear(double distanceToCar, double speedOfCar); ...... // more method signatures } 类实现接口: ...

Java官方笔记6继承

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 继承 Java只有单继承,最顶级的父类是Object。 子类会继承父类的fields和methods,而不会继承constructors,因为constructors不属于methods,但是子类可以通过super调用父类的constructor。 子类继承父类的范围是:public、protected、package-private 隐式转换,子类转父类(只有1个爸爸): Object obj = new MountainBike(); 显示转换,父类转子类(有多个子女,所以要明确指定): MountainBike myBike = (MountainBike)obj; 类有field,而接口没有,所以在多继承时就有问题:如果多个类有相同的field,那么子类将不知道用哪一个,而接口不存在这个问题。Java不支持继承多个类,但是可以实现多个接口。 重载 子类的方法跟父类有完全相同的签名和返回类型(也可以是子类),将会覆盖父类方法Override。 如果子类定义了1个static方法,跟父类完全相同,那么父类方法会被隐藏Hide。 Override和Hide是不同的: public class Animal { public static void testClassMethod() { System.out.println("The static method in Animal"); } public void testInstanceMethod() { System.out.println("The instance method in Animal"); } } public class Cat extends Animal { public static void testClassMethod() { System.out.println("The static method in Cat"); } public void testInstanceMethod() { System.out.println("The instance method in Cat"); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; // 隐式转换 Animal.testClassMethod(); // 调的父类 myAnimal.testInstanceMethod(); // 父类对象引用,还是调的子类 } } The static method in Animal The instance method in Cat Override只会调子类方法,而Hide取决于调用方是父还是子,比如这里的myCat,隐式转换为父类Animal后,会调父类的static方法,而调的实例方法却是子类的。(如果不是隐式转换,而是直接给父类实例化,那肯定还是调父类方法) ...