Shinkai005

V1

2023/02/21阅读:9主题:极简黑

# 【格拉365天不摆烂行动】progSDJava双周复习

【格拉365天不摆烂行动】progSDJava双周复习.md

内容包含~

  • progSDJava PPT
  • progSDJava quiz
  • // progSDJava lab
  • // progSDJava tapp

上次考试英语很吃亏~ 花了很多时间~ 所以提前看看单词~

太多了...写了快20个小时了...

PPT part

java basic

Variables in Java

  • Unlike Python, Java is statically typed , So types of all variables must be declared

Local variables:

  • int i;

  • Class fields:

  • class C { boolean b; }

Method parameter and return values

  • public float getValue (long l) { … }

Static typing in practice

int i; 
i = 5
i = 5.0;
i = “hello”

报错 i 从double 变成 int 会精度丢失

String s = “hello”;
int s;

不能重复声明, 同一个变量

List of Java primitive types

image-20221020101616886
image-20221020101616886

几个注意点:

  • float范围比long大
  • 默认整型是int,默认浮点是double.
  • char 必须单字符
  • 空char会报错
  • byte范围-128-127

String

  • Technically, Java strings are of type java.lang.String which is an object (not primitive)

String是一个对象不是基本类型

  • 直接用"" 就可以创建String. ""这个相当于字符串字面量~
  • String is immutable 不可变得

Java对象存在缓存池中, 被不同用户共享, 如果可以改变, 一个改变就全变了~

immute 这个单词废除了~

  • 可以把String当基本类型用, 但是你要记得他不是~不可变的引用类型~

Useful operators for primitive types

  • Assignment = (single equals sign)
int i = 5;
  • Equality comparison == (two equals signs),!= (not equal to)
if (i == 5) { … }
if (j != 5) { … }
  • Relational operators <, >, <=, >=

  • Boolean combinations && (and), || (or)

Type conversions类型转换

  • To store a value of type t**1 in a variable of type t**2 , the value must be converted to t2before** the assignment occurs

  • 就是先转换了变量类型,再声明(存储相应的值)

Implicit (widening) conversions隐式转换

  • Precision may be lost, but the magnitude of the numeric value is preserved
  • 精度可能丢失, 但是数值大小会保存下来
int i = 5000;
long l = i;
double d = l;
// 可以用精度大的去储存小的, 反过来就直接报错
// 不可以直接改变改变原变量值类型, 直接就报错~

byte/short/char -> int -> long->float->double

Explicit (narrowing) conversions –casting 显式转换-强制转换

  • 强制大转小

  • These narrowing conversions must be made explicit in source code using casting: specify the target type in round brackets
  • 如下用法
int i = 1025;

byte b = (byte)i;

// b now has value 1

How does narrowing work?

double to float: loss of precision, plus …

  • Out-of-range values become infinite
  • Some non-zero values may become zero

非0 可能变0

超出范围可能变

 double d = 1.12312312312311E40d; // Infinity
         float f = (float) d;
        System.out.println(f);
double d = 1.0E-90d; // 0.0
         float f = (float) d;
        System.out.println(f);

double/float to long/int/short/byte/char

  • Round-to-zero
  • Out-of-range becomes infinity
double d = 1.0E-90d; // 0
         int f = (int) d;
        System.out.println(f);

Integer type to “smaller” integer type (e.g., long to int, int to byte, …)

  • Discard all but n lowest-order bits
  • In practice: value mod max

String 和原始类型转换

Converting from String

Use parseXXX methods of primitive wrapper classes

String str = "42"// 0.0
        int i = Integer.parseInt("42"); //42
        System.out.println(i);

E.g., int i = Integer.parseInt(“42”);

Converting to String:

\1. Just concatenate (+) with an existing String value

\2. Use String.valueOf(…)

        int i = 42;

        String str = i + "";

        System.out.println(str); // 42
    int i = 42;

        String str = String.valueOf(i);

        System.out.println(str); // 42


valueOf 保留原始值 转换为想要的类型. 参数有 i/s

A note on integer division

Function of division operator “/” depends on type of the two arguments

  • If both are integers (int, long, short, byte, char), then it does integer division

  • If either is floating-point (float, double), then it does floating point division

        int i = 100;
        double y = 25.0;
        System.out.println(i/y);//4.0

    int i = 105;
        int y = 25;
        System.out.println(i/y);//4

一般整数除法会只取整数部分

Blocks and scope

Block of statements is enclosed in curly braces { }

Usually all statements in a block have same indentation level

主要说作用域~


class VariableTest{
    public static void main(String[] agrs){
        int i = 5;
        int j = 10;
        {
//            int i = 20; // 报错, 已经在main中定义i, 也就是局部不能和上一层重名
            j = 3;
            int k = i + j;
            {
//                int j = 1; // 确实不能重名
            }
            System.out.println(i);
            System.out.println(j);
            System.out.println(k);
        }
        System.out.println(i);
        System.out.println(j);
//        System.out.println(k); //k在局部, 外面找不到
    }
}

Syntax is straightforward:语法很直接的意思

Ifstatements in Java

class VariableTest{
    public static void main(String[] agrs){
        int i = 5;
        int j = 10;
        if(i != 0){
            System.out.println("i"+i);
        }else{
            System.out.println("j"+j);
        }
    }
}

用动态语言的注意下 if括号里只能是布尔值,没隐式转换

The Java for loop

  • Initialisation

  • Termination condition check

  • Variable update

  • 循环变量只是循环的一个局部变量, 每次更新.

for (int i = 0; i < 10; i++) {
doSomething();
}

The while loop

  • while的变量 在循环前后可用~
int i = 0;
while (i<10) {
doSomething();
i++; }

Advanced loop control flow高阶循环控制

  • for (…; …; i<10)

  • while (!valid)

  • continue

  • break

switch statements

和if statement区别

  • Evaluates any boolean expression

  • Conditionally executes one of two statements or blocks

  • 计算布尔表达式, 有条件的执行两个语句

switch statements

  • Evaluates an integer or String expression

    Integer: byte, short, int, long, char, enum

  • Conditionally executes one or more of a list of case blocks

  • 执行多个, 不光可以执行布尔表达式

When to choose one or the other? Depends on (1) readability, (2) the nature of the expression being evaluated (complex tests require if)

Details of how switch works

执行顺序

  • 计算表达式
  • 执行所有符合的case 标签
  • 直到遇到break结束
// 单词原元音辅音单词数
class Test{
    public static void main(String[] agrs){
        String s = "nice";
        int vs = 0, cs = 0;
        for (int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case 'a':
                case 'e':
// …
                    vs++;
                    break;
                case 'b':
                case 'c':
// …
                    cs++;
                    break;
                default:
                    System.out.println("other");
            } }
        System.out.println("Vowels:" + vs + "Cononants:" + cs);
    }

Declaring a method in Java: Syntax

方法声明有六个组件(按顺序):

  1. 访问修饰符(零个或多个-稍后详细说明)
  2. 返回类型(如果不返回值则为空)
  3. 方法名(通常以动词开头)
  4. 括号中的参数列表-用逗号分隔的输入参数列表, 前面是数据类型,用parens括起来。无参数-空的parens。
  5. 一个例外列表(稍后详细介绍)
  6. 方法体,用大括号{}括起来

3和4合起来叫做Method signature

public double calculateAnswer(double wingSpan, int numberOfEngines,double length, double grossTons) {
//do the calculation here
}

Calling a method

double result = calculateAnswer(1041001000);

返回值为空不需要用变量存储

More on methods

  • 多重return是有可能的

  • 只要它们具有不同的参数列表,就可以有两个名称相同的方法(这称为重载)。

Declaring an array

Each array has …

  • A type – the type of the individual elements in the array

  • A dimension – the dimensionality

Variable declaration (method parameter, local variable, class field): String[] args

(String args[] also valid – hang-over from C language style)

两种方式都对~其实还可以更骚

initialize an array

两种方式

  • new 然后给 size
  • 另一种是shortcut syntax
int[] values = new int[10];

默认值

0 for numeric types // 整型浮点型有点区别

Null for object types

int[][] arr = new int[4][];
        arr[1] = new int[]{123};
//        arr[1] = {1,2,3};//报错 非法表达
        int[] arr1 = {1,2,3};

1d的可以

但是2d的行不能直接声明

2d可以直接声明

*(Nice feature: you can put a comma at the end of the list)*

This syntax is only useful if you are **declaring** and **initializing** an array **at the** **same time**

最后这句话很重要~

for-each

class Test {
    public static void main(String[] args) {
        String[] fruits ={"apple","banana","orange"};
        for (String fruit:fruits
             ) {
            System.out.println(fruit);

        }
        }
    }

对象和继承

  • Characteristics of objects (real-world or software)
  • State
  • Behaviour
  • Classes are types
  • Objects are instances of types

这部分看我另一个笔记写的会好一些~ 这块没什么代码只写了知识点

类声明

  1. Use the class keyword
  2. Give the class a name (use CamelCase)
  3. Specify class body inside curly brackets
  4. Fields (properties)
  5. Methods (behaviours)
  • fields, 成员变量, 属性 一个意思~

  • 属性是有默认值的 可以不赋值

  • 构造函数不能写返回值~ void也不行 默认是对象

  • 构造函数里不写this其实也可~ 写类名.属性也行

属性和局部变量区别~ 见另一个笔记

  • An instance variable is accessible in all methods of a class 传进去的实例变量每个方法都能访问得到~
  • 构造器和类名同名
  • 没有返回值
  • this关键字指向当前被创建的对象(....ppt真是啥都敢写,虽然知道是想一点一点补充)
  • 无参构造函数,甚至不写构造函数,属性是默认值~
image-20221021133303090
image-20221021133303090

这道题的解决办法是设置 get 和 set. 然后把字段改成私有~

静态字段

静态字段是和类绑定, 不会被实例化;

只有一个变量, n个实例对象也只有一个变量

静态方法, 类方法

一样不会被实例化~

拿js理解就是写在原型上的 不是在构造函数上~

关于子类的字段

  1. 继承父类字段, 私有不继承~
  2. 尽量别使用同名字段, 不推荐,会隐藏父类字段
  3. 声明超类中没有的字段

关于子类方法

  1. 继承父类方法
  2. Override
  3. 隐藏父类静态方法
  4. 声明超类里没有的方法

多态

字面意思~ 就是实例化后 存在多种实例方法~ a.speak() b.speak()

几个方法

toString()

默认是~ 打印类和存储地址

几个测试没考但是记一下的点

  • private static int NEXT_ID = 0;
  • Subclass is a specialised version of the superclass
  • 子类不继承超类的构造函数~ 但是可以用super(args[]) 调用他们

没时间啦~

把quiz放几个

JAVA基础

image-20221021141718056
image-20221021141718056

强制转换不考虑会不会精读丢失~

  • 右侧强制转换 // 只有 char, 浮点 和整型可以, 布尔 String不可以

  • 然后左侧存储条件是必须比右侧范围大~ short/char/byte->int->long>float>double

image-20221021142115213
image-20221021142115213

左侧可以随便放~右侧不行

二维数组一样

image-20221021142139449
image-20221021142139449

这个自己模拟一下就好~

1

12

123

image-20221021142216972
image-20221021142216972

这个第一行没错~ 第二行存储错误~ 存的是对象类型~扔了个基本类型进去primitive

case

  • two case constants in the same switch statement can have identical values

  • 不能一样

image-20221021142325718
image-20221021142325718
image-20221021142343838
image-20221021142343838

编译错误和运行错误最简单的就是IDEA可以直接发现的就是编译错误~ 运行错误一般就是数组过界问题等对象内部问题

局部变量

个人遇到的~

image-20221021142705194
image-20221021142705194

奇葩写法直接编译报错

image-20221021142721156
image-20221021142721156

字符串+是拼接// 其他报错

image-20221021142857504
image-20221021142857504

整数范围内的计算 都是取整~ 1/2是0

Java对象

What is the correct term for the design approach that allows methods with related effects, but different signatures, to have the same name?

  • 学术名词: 相关效果, 不同署名, 但是有相同名字

signature这里是拥有者名字

name 是方法名

类似a.speak() b.speak() 多态~

image-20221021143142246
image-20221021143142246
image-20221021143210148
image-20221021143210148

构造函数不能加返回值

这里还有个点: 可以在构造函数前写访问修饰符,但是不能写返回值类型

- END -

分类:

后端

标签:

后端

作者介绍

Shinkai005
V1

公众号:深海笔记Shinkai