1. Basic debugging:
- visual inspection:假设statement是错误的并检验是否确实是错误的;最好每写几条statement之后就test看看是否有bug
- Inserting debug output statements:插入检测语句测试部分代码是否正常运转
- Hierarchical debugging:代码过长,每一部分都插入测试语句过于繁琐;
- 将代码分成几大块,分别使用debug output statements
- 找到错误的部分,再对其中的代码使用 visual inspection 或者 debug output statements
2. Java documentation for classes
2.1 简介
Java 中有一种叫做 Javadoc 的工具用于解析代码和特定格式的注释来生成文档。这种文档叫做 API 即 application programming interface 应用程序接口。其中只包含 public class members(variable,method)
2.2 Doc comments
这种特定格式注释叫做 Doc comments:which are multi-line comments consisting of all text enclosed between the /** and */ characters. Importantly, Doc comments are distinguished by the opening characters /**, which include two asterisks.
p.s.: Java中基本修饰符的意思,其中一个包中可以有很多个类,包package>类class
Doc comment 中常用的 block tag
p.s.:
@author:只能声明class的作者,不能用于其他任何members
@see:想让读者去看某个东西
代码示例
ElapsedTime.java
/**
* A class representing an elapsed time measurement
* in hours and minutes.
* @author Mary Adams
* @version 1.0
*/
// 以上即是一种 Doc comment,但是可见“//”也可以做普通注释
public class ElapsedTime {
/**
* The hours portion of the time
*/
private int hours;
/**
* The minutes portion of the time
*/
private int minutes;
/**
* Constructor initializing hours to timeHours and
* minutes to timeMins.
* @param timeHours hours portion of time
* @param timeMins minutes portion of time
*/
public ElapsedTime(int timeHours, int timeMins) {
hours = timeHours;
minutes = timeMins;
}
/**
* Default constructor initializing all fields to 0.
*/
public ElapsedTime() {
hours = 0;
minutes = 0;
}
/**
* Prints the time represented by an ElapsedTime
* object in hours and minutes.
*/
public void printTime() {
System.out.print(hours + " hour(s) " + minutes + " minute(s)");
}
/**
* Sets the time to timeHours:timeMins.
* @param timeHours hours portion of time
* @param timeMins minutes portion of time
*/
public void setTime(int timeHours, int timeMins) {
hours = timeHours;
minutes = timeMins;
}
/**
* Returns the total time in minutes.
* @return an int value representing the elapsed time in minutes.
*/
public int getTimeMinutes() {
return ((hours * 60) + minutes);
}
}
TimeDifference.java
import java.util.Scanner;
/**
* This program calculates the difference between two
* user-entered times. This class contains the
* program's main() method and is not meant to be instantiated.
* @author Mary Adams
* @version 1.0
*/
public class TimeDifference {
/**
* Asks for two times, creating an ElapsedTime object for each,
* and uses ElapsedTime's getTimeMinutes() method to properly
* calculate the difference between both times.
* @param args command-line arguments
* @see ElapsedTime#getTimeMinutes()
*/
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int timeDiff; // Stores time difference
int userHours;
int userMins;
ElapsedTime startTime = new ElapsedTime(); // Staring time
ElapsedTime endTime = new ElapsedTime(); // Ending time
// Read starting time in hours and minutes
System.out.print("Enter starting time (hrs mins): ");
userHours = scnr.nextInt();
userMins = scnr.nextInt();
startTime.setTime(userHours, userMins);
// Read ending time in hours and minutes
System.out.print("Enter ending time (hrs mins): ");
userHours = scnr.nextInt();
userMins = scnr.nextInt();
endTime.setTime(userHours, userMins);
// Calculate time difference by converting both times to minutes
timeDiff = endTime.getTimeMinutes() - startTime.getTimeMinutes();
System.out.println("Time difference is " + timeDiff + " minutes");
}
}
3 Common errors: Methods and arrays
3.1 Method signature errors
方法签名就是method的名加后面形参,如:
public static void main(String[] args)
method signature即使
void main(String[] args)
主要检查method的返回值与其目的是否匹配
- array unchanged,使用数组中的内容计算:int, boolean
- array content changed,size unchanged:void,不需要返回值
- array size changed,int[],产生新数组
3.2 Errors with array references
数组变量引用错误
- 变量的值是地址,那么这个变量是指针;
- 数组名是一个指针,存放了第一个element的地址;
- 数组作为函数参数,若使用形参中element值改变,实参element也改变;若只改变形参的指向,实参不改变;
- 实参向形参的转递实际上是在运行内存中声明一系列拥有相同值的变量(其中的值可以是指针)。
3.3 Common error: Unintended side effects
在不必要的情况下改变了数组的顺序,也就告诉我们一般情况下不要改变数组顺序;
4 Perfect size arrays
4.1 定义
int[] highTemps = {93, 80, 62, 75, 74, 89, 97};
正好被填满的没有空余内存的array叫做 Perfect size arrays。
p.s.:
- 虽然数组的最后一个元素的后一个内存存放数组长度,不能用array[某数字]的方法来访问,必须array.length
- 应已经初始化的数组不能整体上二次赋值,可以一个一个改变()
4.2 When to use perfect size arrays
在数组 长度 能够确定的时候使用,数组内容一般不会改变。
5 Oversize array
不知道数组长度需要多大
int[] salesTransactions = new int[1000];
int salesTransactionsSize = 0;
p.s,
- identifier 就是变量名。
- java中的常量用关键字“final”
public static final String SUNDAY = "SUNDAY"
6 Methods with oversize arrays
pass oversize array to a method 时需要pass 两个量
- array reference
- current size
String toString(int[] a, int fromIndex, int toIndex)
优于,因为可以对oversize中某一部分进行操作
String toString(int[] a, int size)
返回一个量 array size(在改变size才需要)
其实这个array 的内存size并没有改变,变化的是有效size
7 Comparing perfect size and oversize array
1.性质区别: perfect size长度确定,全部填满;oversize 有效长度可变,总体不一定填满了
2. method signature
perfect
a) return可以是void,int,in[]
b) parameter 关于array本身的只有arrayRef
oversize
a) return可以是 void,int,int[]
b) parameter:arrayRef以及arraySize(或者from to之索引)
p.s.
- perfect size array的大小是不能被改变的
- method中不能新建oversize array,因为不能同时返回arrayRef和arraySize
- method中能干什么不是你觉得能就能,使用method之前一定要看文档
- oversize数组空的部分是null
8 Objects: Introduction
object: 一系列data,variables和对应的操作
Abstraction / Information hiding/encapsulation:抽象/封装:隐藏底层细节
9 Object and references
Reference(reference variable) are variables of class data type. object/对象又是这种变量的实体。
RunnerInfo lastRun;//声明一个reference variable不会create一个 object
RunnerInfo currRun = new RunnerInfo();//create an object
currRun.setTime(300);//使用class中的method
currRun.setDist(1.5);
System.out.print("Run speed: ");
System.out.println(currRun.getSpeed());
// Assign reference to lastRun
lastRun = currRun;
lastRun.setTime(250);
lastRun.setDist(1.2);
10 Primitive and reference types(wrapper classes)
10.1 definition
primitive type variable: 八种基本数据类型
reference type variable(wrapper classes):用类的方式表示八种数据类型
为什么没有字符串?字符串String已经是 reference type 了。
10.2 Memory storage&Initialization
如果改变wrapper class 的值,会新建内存储存新的值,原先的内存并未消失
初始化
Integer a = 10;//这里的a是一个class
10.3 Relational operators
关系运算符
特殊比较函数
String.equals()真正比较内容,==比较地址;String 本事就是reference type
11 Parameters of reference types
定义:就是将reference type variable作为method的parameter
- 给 reference type variable(已初始化) assign一个新的值,会创建一个新的object,原来的object并未消失,只是reference type variable指向新的object
- Integer object是不能改变的,只能赋予Integer新的object,和上面的原理相同
12 Wrapper class conversions
12.1 Java 强制类型转换:
Java 自动实现的,不是某种method
- Autoboxing:primitive 转化成 wrapper class
- unboxing:wrapper class 转化成 primitive
Scenarios
- 互相赋值;
- 函数转参;
- 算数表达。
12.2 Wrapper class类内函数转化成primitive
12.3 Strings and numeral systems字符串和数字系统
- Ineter object 转化为 十进制String
- Integer object,int variable,int literal转化为十进制String
- 十进制String转化为int value(其他类型也适用)
- 十进制String转化为Integer object(其他类型也适用)
- Integer object,int variable,int literal转化为二进制String
13 ArrayList
和普通的数组array不同,长度可变加一个多一个,只能储存reference type(wrapper class);
Java object 专用的数组
四种基本操作
import java.util.ArrayList//导入,否则无法使用
ArrayList<Integer> valsList = new ArrayList<Integer>();//Initialization
valsList.add(31);//add a object with value 31
valsList.add(1,14);//insert object with value 14 at index 1
System.out.println(valsList.get(1));//get the element with index 1
valsList.set(1,90);//set element with index 1 as 90
valsList.size();//return 3
14 Introduction to memory management
ArrayList 列表:k序查找快,插入删除慢
Linked list 链表: k序查找慢,插入删除快
15 Memory regions: Heap/Stack
四种内存类型
- code:代码本身的储存空间
- static memory:静态变量储存空间
- stack(automatic memory)栈:系统自动分配和释放(declare variable)储存数据类型和变量名
- heap(free store)堆:手动申请(initialize variable)储存实例对象/object
16 Basic garbage collection
Java 释放内存的机制叫做 Garbage collection,reference count 计数所有在refer这个object他和reference variable,当这个数为零即object没有任何被refer,叫做 unreachable variable,就可以release memory了。
17 Garbage collection and Variable Scope
当不再使用一个object,不用手动取消reference,若 object out of scope,Java自动会release memory。
如method中的local variable当method return之后就会被释放。