Aotle

你所看到的惊鸿,都曾被平庸磨练

考试96分, 仅供考试参考, 并不代表Java的知识体系.

基础考点

  1. 命名规则

    类使用大驼峰命名, 方法一般使用小驼峰.
    首字母只能用 ‘_’ ‘$’ 以及字母, 其余部分可以用字母数字下划线和$运算符
    区分大小写

  2. 数据类型及其转换
    image-20210107193030149

    序号 数据类型 大小/位 封装类 默认值(零值) 可表示数据范围
    1 byte(字节) 8-bit Byte (byte)0 -128~127
    2 short(短整数) 16-bit Short (short)0 -32768~32767
    3 int(整数) 32-bit Integer 0 -2147483648~2147483647
    4 long(长整数) 64-bit Long 0L -9223372036854775808~9223372036854775807
    5 float(单精度) 32-bit Float 0.0F 1.4E-45~3.4028235E38
    6 double(双精度) 64-bit Double 0.0D 4.9E-324~1.7976931348623157E308
    7 boolean but its “size” isn’t something that’s precisely defined.反正我一般把它当作1bit Boolean false true或false
    8 char(字符) 16-bit Character ‘\u0000’(对应数字0,输出为空) 0~65535
    • 自动类型转换: 低精度可以向高精度自动转换
      byte->short,char-> int -> long -> float -> double
      运算过程中, 不同数据类型先转化成同一个数据类型, 然后进行转换
    操作数1类型 操作数2类型 转换后的类型
    byte、short、char int int
    byte、short、char、int long long
    byte、short、char、int 、long float float
    byte、short、char、int 、long、float double double
    • 强制类型转换: int a = (int)32.1
      注意:Java浮点数默认是double类型而不是float
  3. 运算符(应该没必要考吧, 谭浩强太反人类了)

    优先级 运算符 结合性
    1 ()、[]、{} 从左向右
    2 !、+、-、~、++、– 从右向左
    3 *、/、% 从左向右
    4 +、- 从左向右
    5 «、»、>>> 从左向右
    6 <、<=、>、>=、instanceof 从左向右
    7 ==、!= 从左向右
    8 & 从左向右
    9 ^ 从左向右
    10 | 从左向右
    11 && 从左向右
    12 || 从左向右
    13 ?: 从右向左
    14 =、+=、-=、*=、/=、&=、|=、^=、~=、«=、»=、>>>= 从右向左
  4. 数组(字符数组)
    初始化int[] array = new int[10];int[] array = {1 ,2, 3};
    array.length得到的是数组的容量.

  5. 类的定义

    1
    2
    3
    4
    5
    6
    public class Dog{
    public static void main(String[] args) {
    Dog dog = new Dog();
    System.out.println("hello");
    }
    }

代码分析考点

  1. this关键字用法:

    https://www.cnblogs.com/fuao2000/p/12456094.html

  2. 内部类

    https://www.cnblogs.com/fuao2000/p/14266735.html

  3. 输入输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    Scanner scanner = new Scanner(System.in); // 创建Scanner对象
    String name = scanner.nextLine(); // 读取一行输入并获取字符串
    int age = scanner.nextInt(); // 读取一行输入并获取整数
    double a = scanner.nextDouble(); //读取一行double数
    String name = scanner.next(); //读字符串, 以空格分隔
    byte b = scanner.nextByte();
    short s = scanner.nextShort();
    long l = scanner.nextLong();
    public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("123.txt")));
    BufferedReader br = new BufferedReader(new FileReader(new File("123.txt")));
    //其实还有很多其他的方式读取文件, 但是考试只要会这一种就可以了.
    String str = null;
    while((str = br.readline()) != null){
    System.out.print(str);
    }
    br.close();
    }
    //写文件
    //https://juejin.cn/post/6844903807063687181
    BufferedWriter writer = new BufferedWriter(new FileWriter("123.txt"));
    writer.write("hello,world");
    writer.close();
  4. 访问权限修饰符

    修饰符 当前类 同一包内 子孙类(同一包) 子孙类(不同包) 其他包
    public Y Y Y Y Y
    protected Y Y Y Y N
    default Y Y Y N N
    private Y N N N N

    protected: https://www.runoob.com/w3cnote/java-protected-keyword-detailed-explanation.html

  5. 接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public interface tuxing{
    public static final int a = 10;
    public void eat();
    public void travel();
    }
    public class dog implements tuxing{
    public void eat(){
    System.out.println("Mammal eats");
    }

    public void travel(){
    System.out.println("Mammal travels");
    }
    }
  6. 子类父类对象转换
    子类转父类, 怎么转都可以.

    java中子类强转父类,实际上依然是子类;

    该引用只能调用父类中定义的方法和变量;

    如果子类中重写了父类中的一个方法,那么在调用这个方法的时候,将会调用子类中的这个方法;

    但是变量重名的时候会使用父类的field, 也就是所谓的隐藏.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    // A是父类,B是子类
    A a = new A();
    a.setName("a1");
    a.sayA(); // sayA

    B b = new B();
    b.setName("b1");
    b.setSex(true);
    b.sayA(); // sayA from B
    b.sayB(); // sayB

    // Java中的对象进行类型提升,依然保持其原有的类型。
    A a2 = (A) b; // 子类强转父类,其实仍然是子类
    System.out.println(a2.toString()); // B [name=b1, age=0, sex=true]
    // 该引用只能调用父类中定义的方法和变量;
    // a2.sayB(); // The method sayB() is undefined for the type A 报错
    // 如果子类中重写了父类中的一个方法,那么在调用这个方法的时候,将会调用子类中的这个方法;
    a2.sayA(); // sayA from B a2其实是B,调用的是B方法

    只有父类对象本身就是用子类new出来的时候, 才可以在将来被强制转换为子类对象.

    1
    2
    3
    4
    5
    6
    // B b2 = (B) a; // atest.A cannot be cast to atest.B a是A,转不成B
    // 只有父类对象本身就是用子类new出来的时候, 才可以在将来被强制转换为子类对象.
    B b2 = (B) a2; // a2其实是B,可以转成B
    System.out.println(b2.toString()); // B [name=b1, sex=true]
    b2.sayA(); // sayA from B
    b2.sayB(); // sayB

    父类转子类, 只有父类对象本身就是用子类new出来的时候, 才可以在将来被强制转换为子类对象.

  7. 继承,重载

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    abstract class Person {
    public abstract void run();
    }

    public class Student extends Person {
    @Override
    public void run() {
    System.out.println("Student.run");
    }
    //重载
    public void run(String s) {
    System.out.println(s);
    }
    }
  8. 异常处理
    考试要求看懂就行了, 也就是长脑子都能看懂

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // 捕获异常
    try {
    file = new FileInputStream(fileName);
    x = (byte) file.read();
    } catch(FileNotFoundException f) { // Not valid!
    f.printStackTrace();
    return -1;
    } catch(IOException i) {
    i.printStackTrace();
    return -1;
    }
    finally{
    file.close();
    }
    // 抛出异常
    public class className
    {
    public void deposit(double amount) throws RemoteException {//告诉调用者可能抛出,需要捕获
    // Method implementation
    // 明确的在方法抛出这个异常
    throw new RemoteException();
    }
    //Remainder of class definition
    }

程序题考点

  1. 排序算法
    一般这个考试只可能是冒泡, 选择, 插入, 快排.所以记这四个就够了.

    https://www.cnblogs.com/guoyaohua/p/8600214.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    /**
    * 冒泡排序
    *
    * @param array
    * @return
    */
    public static int[] bubbleSort(int[] array) {
    if (array.length == 0)
    return array;
    for (int i = 0; i < array.length; i++)
    for (int j = 0; j < array.length - 1 - i; j++)
    if (array[j + 1] < array[j]) {
    int temp = array[j + 1];
    array[j + 1] = array[j];
    array[j] = temp;
    }
    return array;
    }
    /**
    * 选择排序
    * @param array
    * @return
    */
    public static int[] selectionSort(int[] array) {
    if (array.length == 0)
    return array;
    for (int i = 0; i < array.length; i++) {
    int minIndex = i;
    for (int j = i; j < array.length; j++) {
    if (array[j] < array[minIndex]) //找到最小的数
    minIndex = j; //将最小数的索引保存
    }
    int temp = array[minIndex];
    array[minIndex] = array[i];
    array[i] = temp;
    }
    return array;
    }
    /**
    * 插入排序
    * @param array
    * @return
    */
    public static int[] insertionSort(int[] array) {
    if (array.length == 0)
    return array;
    int current;
    for (int i = 0; i < array.length - 1; i++) {
    current = array[i + 1];
    int preIndex = i;
    while (preIndex >= 0 && current < array[preIndex]) {
    array[preIndex + 1] = array[preIndex];
    preIndex--;
    }
    array[preIndex + 1] = current;
    }
    return array;
    }
    /**
    * 快速排序方法
    * @param array
    * @param start
    * @param end
    * @return
    */
    public static int[] QuickSort(int[] array, int start, int end) {
    if (array.length < 1 || start < 0 || end >= array.length || start > end) return null;
    int smallIndex = partition(array, start, end);
    if (smallIndex > start)
    QuickSort(array, start, smallIndex - 1);
    if (smallIndex < end)
    QuickSort(array, smallIndex + 1, end);
    return array;
    }
    /**
    * 快速排序算法——partition
    * @param array
    * @param start
    * @param end
    * @return
    */
    public static int partition(int[] array, int start, int end) {
    int pivot = (int) (start + Math.random() * (end - start + 1));
    int smallIndex = start - 1;
    swap(array, pivot, end);
    for (int i = start; i <= end; i++)
    if (array[i] <= array[end]) {
    smallIndex++;
    if (i > smallIndex)
    swap(array, i, smallIndex);
    }
    return smallIndex;
    }

    /**
    * 交换数组内两个元素
    * @param array
    * @param i
    * @param j
    */
    public static void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
  2. 线程实现输入一串数字求max,min,avg

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class XianCheng {

    public static void main(String[] args) {
    Thread thread = new Thread() {
    public int sum = 0;

    public void run() {
    Scanner scanner = new Scanner(System.in);
    int[] temp = new int[10];
    for (int i = 0; i < temp.length; i++) {
    temp[i] = scanner.nextInt();
    sum += temp[i];
    }
    System.out.println(sum / 10);
    Arrays.sort(temp);
    System.out.println(temp[0]);
    System.out.println(temp[9]);
    }
    };
    thread.start();
    }
    }
  3. 继承实现长方形,三角形,圆形求周长面积

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    abstract class tuxing {
    public tuxing(int a, int b) {
    chang = a;
    kuan = b;
    }

    public int chang;
    public int kuan;

    public abstract int mianji();
    public abstract int zhouchang();
    }

    class juxing extends tuxing {
    public juxing(int a, int b) {
    super(a, b);
    }

    public int mianji() {
    return chang * kuan;
    }

    public int zhouchang() {
    return chang * 2 + kuan * 2;
    }
    }

    public class Base {

    public static void main(String[] args) {
    juxing juxing1 = new juxing(10, 2);
    int mianji = juxing1.mianji();
    int zhouchang = juxing1.zhouchang();
    System.out.println(zhouchang);
    System.out.println(mianji);
    }
    }
  4. 键盘输入两个数字,加减乘除,求最大公因数最小公倍数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class Main{
    public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int m = scanner.nextInt();
    int n = scanner.nextInt();
    int a = m;
    int b = n;
    //求最大公因数最小公倍数.
    if (m < n) {// 保证m>n,若m<n,则进行数据交换
    int temp = m;
    m = n;
    n = temp;
    }
    while (m % n != 0) {// 在余数不能为0时,进行循环
    int temp = m % n;
    m = n;
    n = temp;
    }
    int gongyueshu = n;// 返回最大公约数
    int gongbeishu = a*b/n; //最大公倍数
    }
    }

其他

image-20210115194605124

image-20210115194645581

剩下的是我自己原来随便写的, 不用看

基础语法

不多说了,主要说一下容易错的几个点。

局部变量在使用前必须给定初值,否则编译出错,类成员变量无此要求。

image-20210107193030149

注意Java中局部变量的作用域在它的代码块中。

类静态成员常量只能在定义时初始化

方法中的常量(局部常量)可以在定义时初始化,也可以先定义,后初始化

常量只能赋值一次(初始化)

整形默认是int,浮点型默认时double

科学计数法:2.<尾数> E <阶码>

-1.234567E+12

尾数必须有,但小数部分可无

阶码必须有,必须是整数

基数是10

Infinity,-Infinity,NaN

instanceof用来确定一对象是否是某一指定类的对象父类也返回true

&& 和 || 短路,&|不断路

>>右移,左边填充符号位

<<左移,右边填充0

>>>无符号右移,左边填充0

运算符优先级。。但愿不会出谭浩强的题目

自动类型转换:低精度转高精度

强制类型转换:int a=345;byte b = (byte)a;//可能导致精度丢失

int常量赋给合适精度的变量不需要强制转换,但是int变量赋值给精度低的变量就需要强制类型转换了

Arrays.sort(x);//排序

Arrays.binarySearch(x,1) //二分查找,

System.arraycopy(x,0,y,0,length) //拷贝

Arrays.fill(x,7);//使用特定值填充

OOP

final static 修饰 必须在定义的时候赋值

final 变量 可以在构造函数中赋值

final 修饰类, 不能被继承, 修饰方法, 不能被覆写, 一般final类和private方法都默认是final的

static 静态成员变量为所有子类和父类共享

static 可以被子类覆写, 两个变量相互独立, 互不影响

普通变量 也可以被子类覆写, 两个变量相互独立, 不影响, 也就是隐藏机制

子类覆写的变量或方法不可以降低成员的访问权限

子类调用父类构造方法使用super()并且必须放在第一句

如果子类没有显示的调用父类的构造方法, 则默认在子类构造方法前调用父类的无参构造方法, 如果父类没有super()就会报错.

构造方法调用顺序

  • 首先调用父类的构造方法, 这个过程会被反复递归调用, 直到调用完全部父类的构造方法.
  • 对成员变量进行初始化赋值,
  • 最后执行该类的构造方法

静态代码块先于代码块和属性初始化,代码块先于构造方法。 静态属性初始化和静态代码块哪个在前面哪个先。 同样属性初始化和代码块哪个在前面哪个先。 静态属性初始化先于普通属性初始化。

abstract 修饰抽象类/抽象方法

接口

接口中的变量都是 public static final的

接口中的方法都是public abstract , 可以省略不写.

内部类

https://www.cnblogs.com/fuao2000/p/14266735.html

异常处理

  1. 错误error
  2. 异常exception
    • 运行时异常(程序本身的错误, 程序员可以修改程序来避免)
    • 非运行时异常(可以由编译器在编译的时候检测到的, 发生在方法执行过程中的异常, 必须捕获或者抛出这种异常)

util and io包

String

1
2
3
4
5
6
7
8
9
10
11
substring();//返回字串

length()

charAt()

tocharArray()

toUpperCase()

toLowerCase()

StringBuffer //线程安全

StringBuilder //非线程同步的类, 速度比StringBuffer快

1
2
3
4
5
6
7
append()

insert()

setCharAt()

delete()

Calendar

1
2
3
4
//获取
Calendar c = Calendar.getInstance();
set()
gettime()

DateFormat//格式化日期

流的概念:

分为输入流, 输出流, 字符流, 字节流, 节点流, 过滤流

InputStream 字节输入流的超类
OutputStream 字节输出流的超类
Reader 字符输入流的超类
Writer 字符输出流的超类
File 文件类
RandomAccessFile 随机访问文件类

多线程

https://www.cnblogs.com/fuao2000/p/14272827.html

网络编程

暂时不写了.

反射

https://www.cnblogs.com/fuao2000/p/14181645.html

注解

https://www.cnblogs.com/fuao2000/p/14169874.html

评论