CSE ucsd

Lecture 2 Introduction to Java

  • Machine language -->Assembly language --> High-level language

A computer can execute the code in machine language.

A program written in a high-level language is called a source code/program, which the computer cannot understand, so the translation can be done using another programming tool called an interpreter or a compiler.(解释器和编译器)

interpreter: 逐行读取代码,翻译code,慢
compiler: 一次性翻译成机器语言,在执行,快

Java is an object-oriented programming language.
Java API contains predefined classes and interfaces for developing Java programs.
Every statement must end with a semicolon.
A block in enclosed inside braces.

在java中,compiler: javac
interpreter: java

source code --> compiler(javac) --> generate class(translate to java bytecode) --> interpreter(java) (using java virtual machine) --> executed

print 的用法:
print()----不换行
println()---换行

comments的几种方法:
// 仅限一行
/..../
/**...*/

identifiers:标识符(命名的名字)
must start with a letter, underscore(_) or a dollar sign $
不以数字开头,不以关键字命名

Named constants: 名字quanbudaxie

Programming error types:
Syntax error(Compile error) : mistype... didn't compile well (语法错误)
Runtime error: 非语法错误,是compile之后运行过程中的错误,eg out of bounds...
Logic error: produce incorrect result

一旦申明了datatype,就不能改变

Lecture 3 Numbers and Mathematics

Numerical data types: double > float > long > int > short > byte

long : -2^63 to 2^63-1 64 bit
int: -2^31 to 2^31-1 32 bit
short: -2^15 to 2^15-1 16 bit
byte: -128 to 127 8-bit

Numeric operators: + - * / %(remainder)
Augmented assignment operators: += -= *= /= %= 增强赋值运算符

double are more accurate than float
0.3333333333333 double
0.333334 float

如果float number涉及到了计算,所储存的数据都不是精确的

整数/整数,自动断截小数部分,不会rounded

++var先自身增加,再赋值
var++ 先赋值,在自身增加

Conversion rules:
隐式转换:如果计算中出现范围最大的数据,其他所有数据类型自动转换成出现最大范围的数据类型
强制转换: (int)3.9 >>>3

Operator precedence: 运算符优先级
()var++ var--
++var --var
隐式转换 强制转换 type casting

  • / %

= < <=
== !=
^
&&
| |
增强赋值运算符

同级运算符从左往右运算
赋值运算符(Assignment operators以等号为核心): 从右往左

Math.sin(Math.PI)---return double
Math.round(x) -----return integer
Math.rint(x) ------return double
Math.ceil(x)-------return double
Math.floor(x)-------return double

Lecture 4 Characters and Strings

ASCII Unicode
‘0’~'9' : 48-57 \u0030 to \u0039
'A' ~ 'Z' : 65-90 \u0041 to \u005A
'a' ~ 'z' : 97-122 \u0061 to \u007A

int i = (int)'a';
char c = (char)97;

String type is a predefined class

String一系列methods
.trim()---get rid of blank spaces on both sides
.charAt(index)---return the specific character at index
.concat(s1)-----return the new string that concatenate with s1--------String s3 = s1.concat(s2);
其余的methods见notesheet

instance methods: referenceVariable.methodName(args);
static methods: className.methodName(args);

Lecture 5 Selections

conditional operator: y = (boolean-expression) ? expression1 : expression 2;

logic operator : ^ eg. 5^3 = 6 相同为0 false,不同为1 true

switch(switch_expression){
case value1: statement1;
break;
case value2: statement2;
break;
default: statements_for _default;
}

if-else / switch /

There's no guarantee that 1-0.1-0.1-0.1==0.7 is true because floating-point(float+double) numbers are approximate.

In java, the word true is a Boolean literal.

else 语句 matches the most recent if 语句

For the && operator, the right operand is not evaluated if the left operand is evaluated as false.

如果switch语句里each case没有break,那么将继续运行(穿透)直到break出现

switch/if(可能) control variable cannot be double.

Assignment operators including augmented assignment operators are right-associative.

&& has higher precedence than | |

Lecture 6 Methods

define methods:
public static int/char max(int num1, int num2){}
public static double name(double num1, double num2){}

Pass by value值传递:modify num1 doesn't modify x

instant methods: referencevariable. methodName(args)
static methods: ClassName.methodName(args)

Overloading methods: methods with the same name as long as their parameter lists are different(参数列表不同)

ambiguous invocation : compile error : 同时有两个方法可以调用,但是compiler分不清用哪个

local variable: variable defined inside a method

Method abstraction: 有method header,但是不知道method body

stepwise refinement/Bottom-up implementation/Top-down implementation(stub)

A method signature consists of method name and parameter list.

Lecture 7 Loops and Recursion

while / do..while / for / recursion

sentinel value 哨点

do..while: execute the loop body first, then checks the loop continuation condition

Do not use floating-point values for equality checking in a loop control(不要用double用于条件判断)

break : immediately terminate the loop
continue: end the current iteration

recursion 计算!!
stack overflow栈溢出: 没有restriction(base cases) 限制就会出现栈溢出

for( ; ; ); >>>true

做循环题目的时候把每一次循环的return写出来,最后再看答案是什么

for(i=2 ; i<10 ; i++){ ... } 先判断,再执行语句,再 i++ ,再判断,再执行语句,再 i++

recursive method run faster than non-recursive methods

Lecture 8 Arrays

array的三种写法:
int[] i = {1,2};
int[] a = new int[5];
int[] i = new int[]{1,2} ---------------不能同时规定数组长度和初始化列表
前面的int可以改成double,

default value:0----data
false---boolean
0------arr[i]
indexed variable

for each loops:用来遍历
for(double value : mylist){
soutp(value);
}

The assignment statement doesn't copy the contents, only copies the reference value------引用赋值

anonymous array
eg. printArray ( new int[] { 1,2,3,4,5 } ) ;

Changing the value of the local parameter inside the method doesn't affect the value of the variable outside the method.

Heap: JMV stores the array in the area of memory called the heap, which is used for dynamic memory allocation.

Two-dimensional arrays
int[][] arr = new int[rows][columns]
int columns = arr[0].length;
int rows =arr.length;

两个array相同(list1 = list2),地址值就是一样的, 不管做任何改变list1和list2都是一样的,对list1改变也是对list2做改变

default value for array都是0

对于方法里面的形式参数
值传递:基本数据类型:方法外的变量值不会改变
地址值(reference value):因为传递的地址值,方法可以直接修改array里的内容

https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150524922-2094958921.jpg)

(https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150557561-18493061.jpg)

(https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150638496-956031378.jpg)

(https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150604459-1533275330.jpg)

(https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150509666-76919633.jpg)

(https://img2024.cnblogs.com/blog/3659684/202602/3659684-20260212150627901-596482069.jpg)

5cf76e7b1354c3d18fe193addd897aff

c534ffe8445b927c48acae8e2e01aac8

Lecture 9 Objects and classes

Unifies Modeling Language (UML)

constructors must have the same name as the class itself (constructors don't have a return type)

如果没有手动写constructors的话,会自动生成default constructor(no-arg)
不是所有class都有constructor的

静态方法 静态变量要用类名. 调用

Circle x = new Circle();-----x指向的是一个reference

default value:
boolean : false
numeric: 0
object: null

The data field has default values
但是main方法里的variable必须要有初始值 because Java assigns no default value to a local variable inside a method

a reference variable references to an abject(是储存的地址值,然后对应去找object)

anonymous object匿名对象:直接new,不赋值给变量

pass by value: 先看参数类型是primitive还是reference
如果是primitive:方法里对参数做任何赋值/运算,原变量不变

如果reference: 传递的是reference(地址值),看方法内操作:若是复值新对象/数组,只改变引用副本,不改变原来的;如果调用对象set方法/修改数组元素,改变内部状态,原对象改变

Lecture 10 Objects and classes part2

instance 和 static 的区别
实例方法可以访问实例方法+实例变量+静态方法+静态变量(什么都可以)
静态方法可以访问静态方法+静态变量,关于任何实例的都无法访问(只可以访问静态相关的)

visibility:public>protected>default>private

immutable class requirements:
all data fields must be private
cannot be any mutator methods for data fields(setter)
no accessor methods can return(getter)

"this" must be the first statement in the constructor
this(1.0)------used to invoke another constructor

每次新create一下constructor变量都会变成default value,除非是静态变量

cannot use the private constructor to create an object

use the private modifier to encapsulate data field

Adv of 封装:it changes the implementation without changing a class's contract and causes no consequential changes to other code

代码块局部变量{ }:就是莫名其妙出现的一个大括号里面写了点代码,代码块里的局部变量在运行玩{ }之后就消失了

Lecture 11 Object-Oriented Thinking and Introduction to Generics

class abstraction means to separate class implementation form the use of the class
the user of the class does not need to know how the class is implemented

class relationships:
association:faculty teaching a course 相关联 Faculty & Course
aggregation:owner(aggregating class)-subject(aggregated object) relationship has-a aggregating has an aggregated
composition: 属于更强的aggregation, 有一个独属于的关系存在
inheritance:

primitive data type values as objects
primitive data type is not an object, but it can be wrapped in an object using a java wrapper class: Boolean Integer Short Byte Float Double

the wrapper classes don't have no-arg constructors and constructors are deprecated; we use static valueOf methods instead
eg. Integer wrapper class里面都有以下方法:
Integer intObject = Integer.valueOf(Integer);
soutp(intObject) >>>eg.100--------------为什么也能打印出100?因为自动在打印的括号里调用 .toString()方法了
int a = intObject.intValue();
soutp(a) >>>eg.100
int b = parseInt(string);---string转成int的方法
toString()转成string

Numeric wrapper class constants:
eg. For Float and Double, MIN_VALUE represents the minimum positive float and double values

Wrapper class valueOf method:
these methods create a new object initialized to the value represented by the specific corresponding primitive data type value or string representing the numeric value
Double doubleObject = Double.valueOf(12.4);
Double doubleObject = Double.valueOf("12.4");

Each numeric wrapper class 都有parseInt/parseDouble...之类的方法,把string转变成primitive type

valueOf (自动)
primitive value------> wrapper object
<------
intValue(自动)

A String object is immutable.

做有关array和arraylist的题目时注意index导致的runtime error

Lecture 12 Inheritance 继承

superclass & subclass

keyword:extends

UML标志

keyword: super
if the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked

super的两种用法:

  1. super()调用父类的no-arg constructor来初始化
  2. super.方法名()调用父类方法

this的两种方法:

  1. this(arguments)---会按照给的参数自动匹配本类的constructor,一定是构造器不是方法

both this and super must be the first statement in the constructor

Constructor chaining: constructing an instance of a class invokes all the superclasses' constructors along the inheritance chain

@Override: using the same signature(方法名+参数), and return type same or subtype of the overridden method's return type

Overriding(重写) vs overload(重载)
overriden methods: same signature, in different classes related by inheritance
overloaded methods: same name, but different parameter lists, in same class/different classes related by inheritance

a private method cannot be overridden.
如果在子类里的method是private在父类里,那么这是两个完全不相干的方法

if a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden

toString() 方法return a string representation of the object

a subclass is a subset of a superclass----------错!!!

无论子类构造器是有参还是无参,只要没有写super(...),java编译器都会自动在构造期第一行插入super(),去调用父类的无参构造器(一定只是super())

非常有用的一个例子:https://img2024.cnblogs.com/blog/3659684/202603/3659684-20260314105224163-1368243431.jpg =707x946)

做这类选择题一定要注意要一层一层invoke上去,考虑清楚class会不会自动生成no-arg参数构造和super(), 脑子要想清楚

constructor cannot be static, because you use a constructor to create a specific instance

super.super没有这种语法!!!!

Lecture 13 Polymorphism

dynamic binding动态绑定
当declared type相同时,java会根据运行时对象的actual type来选择要执行的方法版本

casting objects
upcasting 小变大:
downcasting

instanceof

a subclass can increase accessibility, cannot decrease accessibility of a method

final modifier

判断:
dynamic binding can apply to static methods-----错!!!

keywords are all in lowercase

casting类型转换不改变对象本身(只改变declared type,actual type不变)

Object类的equals方法默认也是比较地址值,其他的equals重写方法是比较内容

Encapsulation means that data fields should be declared private.

Inheritance means that a class can extend another class.

Composition means that a class contains a data field that references another object.

Lecture 14 Exceptions and Test File I/O

Exceptions are runtime errors caused by your program and external circumstances
System.exit(1)-----------一个用来终止JVM的方法

exceptions类型分类:
cbe13846031f7a51203c9e79ccb48f6b

declaring throwing and catching exceptions
method2() throws Exception{--------------declaring
if(error occurs){
throw new Exception();-----------throwing
}
}

method1{
try{
method2()
}catch(Exception e){}--------------catching
}

File file = new File(filePath);
Scanner fileScanner = new Scanner(file);---------为什么要写这一步,因为File里没有读写方法
String line = fileScanner.next();
Scanner lineScanner = new Scanner(line);
lineScanner.nextDouble();
lineScanner.next();.......

PrintWriter output = new PrntWriter(file);
output.print(".....")

a method must declare to throw checked exceptions

catch exception时要先catch小的再catch大的(先runtimeException再Exception)

throw后直接跳到catch里,后续的代码不执行

finally的必须执行

如果要在finally写close(),那么output/fileScanner/lineScanner不能只是局部变量

try-catch 中一旦exception被捕捉到,后续的代码就不执行了
exception可以被多个test捕捉到,但catch里面的exception类得从小到大写
final是必须执行的,不管有没有handle error。如果handle了error,final后面的语句继续执行,如果没有handle,那就不执行

Lecture 15 Abstract
对于object来说,只有存在继承关系,才可以强制转换/隐式转换

用abstract修饰的class不能直接用来创建对象,是专门用来被继承、被扩展,是一种“模版”

只要含有抽象方法的一定是抽象类
但抽象类里不一定全都得是抽象方法
继承抽象类的子类必须重写所有抽象方法,但其不一定得是抽象类

抽象类constructors用protected修饰

an abstract class cannot be created using the new operator

abstract class can be used as a data type

动态绑定:编译时看declared type,运行时看actual type

Lecture 16 interface

抽象类和接口的区别:
抽象和接口都不可以实例化;
抽象类和接口都可以有concrete method;
抽象类和接口都可以有fields,只是接口里的fields都是常量,用public static final修饰
A class can extend only one abstract class, while it can implement multiple interfaces.

The subclass method must have the same name, return type, and parameters as the superclass method.---------正确

byte: -128-127

String join用法:String res = String.join(" ", words);

2c330b5283fcff5228decb2c13f4ddeb
fef2bf4a8ec610be3d49767d137b367c
8c2c8e81b7be8a65df38b49c0de90ec7
8deb8e95763e99f01a9954ed04170c6c
9a6da70362faa9e76301909f0a2c17bd
802437cf3d0be1e771884d51d2a8ee90
c5b176dfd59d08e14de84fcccd23e0f7
99d563b9859b13655d721d20d3ba039a
34ad0eab6e7c9fb39da08f08c8b215bf
a31120e4b2a3431626db0c815bb0b740
03a07efff0b061a800ff09ae258d3eef
037014422f7000f83d4f308acbe2befc
7127a9ac8f27f28334cb54dcdfad87e8
dad46d3ae7ad5f2e798011514da2a77d
bcf4187d887161e9254fb1b06b91bfc2
97bc07b1dda667a184dd83d77531ae6d
4cdc8762d9821ad959e90b9696e4227a
c3fb8e69010a73e812583e5dbdf67688
5d869b5eda8f0ccd3e8007583deb5e44
48e8d52db6340d3c900b75152ad95d2e
473155f1b0143d9d018badb979269b01
782eb6f16c2f3b2ebd645d78883c1413
fe44dcc4eb0fea62ad78da57bc535c62
4651a6ab58c85c8673f2ada001af1be2
371a0105fa556bf44782d62ee5aef152
61851c935c3eeb5100c4de74c0f4fde3
0a3aac2232bff14c8f8037f464301fb3
aba47192877da85ea84a4c0b6ca7c804
51197971835d0b7a3c5118d5ceb8769e

专业术语: semicolon;reserved words;comments;blocks;braces{ }; parentheses ( ); brackets [ ] ; double slashes// ; quotation marks" " ;

posted @ 2026-02-05 07:14  junewang35  阅读(10)  评论(0)    收藏  举报