本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
cs:programming:java:courses:gtx_cs1311x:foundations_n_syntaxs [2024/01/15 13:08] – 移除 - 外部编辑 (未知日期) 127.0.0.1 | cs:programming:java:courses:gtx_cs1311x:foundations_n_syntaxs [2024/01/15 13:08] (当前版本) – ↷ 页面名由cs:programming:java:courses:gtx_cs1311x:module_1改为cs:programming:java:courses:gtx_cs1311x:foundations_n_syntaxs codinghare | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======Foundations and Syntax Basics====== | ||
+ | //Course I notes// | ||
+ | ---- | ||
+ | ====Module 1==== | ||
+ | ===Introduction to Java=== | ||
+ | ==Histroy== | ||
+ | {{ cs: | ||
+ | |||
+ | ==Java 的基本运行条件== | ||
+ | * 至少有一个 Method | ||
+ | * 至少有一个被称之为 '' | ||
+ | * mehod 需要被包含在 class 里 | ||
+ | * 程序至少要一个类 | ||
+ | <code js> | ||
+ | //class | ||
+ | public class project { | ||
+ | //method | ||
+ | public static void main(String[] args) { | ||
+ | // | ||
+ | String x = "Hello world"; | ||
+ | //print line | ||
+ | System.out.println(x); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ==java 的文件名== | ||
+ | * **文件的名字**必须与**文件中的类名匹配** | ||
+ | * 后缀为 '' | ||
+ | ===Executing Java Programs=== | ||
+ | ==两种类型的程序转换== | ||
+ | * 编译器(// | ||
+ | * 速度快:只用编译一次即可反复使用 | ||
+ | * 不同的机器支持的指令集不同(需要的中间文件也不一样),因此编译器是基于机器来设计的。因此,在一台机器上编译好的程序很可能不能在另外一台机器上跑。如果需要跨平台,那么针对每个平台都要编译一次。 | ||
+ | * 解释器(// | ||
+ | * 速度慢,每次运行都要执行 | ||
+ | * 没有中间文件的生成,因此可以在任何电脑上执行。 | ||
+ | ==Java' | ||
+ | * Java 不会直接编译基于机器码的中间文件,而是编译程序为 '' | ||
+ | * 当编译了 '' | ||
+ | <WRAP center round box 100%> | ||
+ | * Java 的解释器被称为 Java Virtual Machine(// | ||
+ | * 这种混合的方式被称为 // | ||
+ | </ | ||
+ | ==Java 程序的编译与执行== | ||
+ | <code bash> | ||
+ | # complie java | ||
+ | javac + file_name.java | ||
+ | |||
+ | # a file_name.class file will generated | ||
+ | # initiate interpreter, | ||
+ | # no extension needed! | ||
+ | java + file_name | ||
+ | </ | ||
+ | ==The Java Development Kit== | ||
+ | //Java Development Kit(JDK)// 是 java 用于编译和执行文件的工具集合。与之前概念一些区别: | ||
+ | * //Java Development Kit(JDK)// : 包含了写 java 的工具,以及编译 java 需要的环境(// | ||
+ | * //Java Runtime Environment(JRE)// | ||
+ | * //Java Virtual Machine(JVM)// | ||
+ | 三者从上到下是依次包含的关系。 | ||
+ | ===Why OOP=== | ||
+ | ==Identifier== | ||
+ | java 的类名,函数名,变量名等一切由用户自定义的 name 被称为 identifier: | ||
+ | * identifier 可以包括:字母(letters),数字(digits),下划线 '' | ||
+ | * 数字**不能**作为 name 的开头 | ||
+ | * identifier 不能为关键字(均为小写) | ||
+ | * indentier 区分大小写 | ||
+ | * Java 使用骆驼写法(头字母小写)来对 identifier | ||
+ | ==Java 的数据类型== | ||
+ | ^ Type ^ Size ^ Range ^ | ||
+ | | <color # | ||
+ | | <color # | ||
+ | | <color # | ||
+ | | <color # | ||
+ | ==变量的声明与赋值== | ||
+ | <code js> | ||
+ | # | ||
+ | int a = 78; | ||
+ | double d = a * 5; | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | 跟 C++ 类似,Java 的运算中存在隐式转换。赋值的时候需要注意。 | ||
+ | </ | ||
+ | ==Objects== | ||
+ | //Objects// 的两种属性: | ||
+ | * // | ||
+ | * //actions// 代表行为,比如点火,熄火,踩刹车 | ||
+ | ====Module 2==== | ||
+ | ===Basics=== | ||
+ | ==whitespace== | ||
+ | * blanks, tabs, newline | ||
+ | * 编译器将其视作分界线 | ||
+ | * 增强代码的可读性 | ||
+ | ===Errors=== | ||
+ | * Error types: Complier, Runtime, Logical | ||
+ | * Smantics vs systax : meaning of the code vs coding rule | ||
+ | ==Compile and Runtime Errors== | ||
+ | * Complie error: 指程序在编译过程中出现的错误,具体指systax error | ||
+ | * Runtime error : 指程序在执行中出现的错误(比如除零) | ||
+ | ==Logical Errors== | ||
+ | * Logial error 指 semetics error | ||
+ | <WRAP center round box 100%> | ||
+ | * 推荐写完每一个 method 之后测试一次 | ||
+ | * 添加 comments | ||
+ | </ | ||
+ | ==Comments== | ||
+ | <code js> | ||
+ | // this is inline comment | ||
+ | |||
+ | /* this is | ||
+ | block comment | ||
+ | */ | ||
+ | |||
+ | /** javadoc comments | ||
+ | this scan your source code for certain comments | ||
+ | and automatically creates html files to describe your code, | ||
+ | starts with forward slash and to asterisks | ||
+ | end with an astrisk and forward slash | ||
+ | */ | ||
+ | </ | ||
+ | ===Variables and Constants=== | ||
+ | * variable 拥有自己的 scope | ||
+ | * 其区域通常以 method 为单位 | ||
+ | ==constant 的定义== | ||
+ | * 使用 '' | ||
+ | <code js> | ||
+ | final double PI = 3.1415926 | ||
+ | </ | ||
+ | ==Primitive Types== | ||
+ | ^ Type ^ Size | ||
+ | | <color # | ||
+ | | <color # | ||
+ | | <color # | ||
+ | | <color # | ||
+ | |<color # | ||
+ | |<color # | ||
+ | ==default type== | ||
+ | * 整型的 default type 是 '' | ||
+ | * 浮点型的 default type 是 '' | ||
+ | 因此,如下的语句会错误: | ||
+ | <code js> | ||
+ | long bigNum = 99999999999999 | ||
+ | </ | ||
+ | 因为此处将 '' | ||
+ | <code js> | ||
+ | //adding L to the end of the number | ||
+ | long bigNum = 99999999999999L | ||
+ | </ | ||
+ | 同理,如果要使用 '' | ||
+ | <code js> | ||
+ | float PI = 3.14159F | ||
+ | float PI = 3.14159f | ||
+ | </ | ||
+ | 我们也可以使用类似后缀强制将默认为 '' | ||
+ | <code js> | ||
+ | double num1 = 2D; | ||
+ | double num1 = 2d; | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | 低精度的数据有助于减少内存的使用。 | ||
+ | </ | ||
+ | ==char== | ||
+ | * char 的排序取决于 character set | ||
+ | * Java 使用 unicode 作为 character set,该 set 是 ASCII 的超集 | ||
+ | 定义 char 使用单引号(single qoute): | ||
+ | <code js> | ||
+ | char yes = ' | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | double quote 代表 string,不能用于 char | ||
+ | </ | ||
+ | ==boolean== | ||
+ | '' | ||
+ | <code js> | ||
+ | boolean parked = true; | ||
+ | </ | ||
+ | ==escape sequences== | ||
+ | 使用 back slash 作为 escape sequence 的开始,表示特殊字符: | ||
+ | <code js> | ||
+ | // single quote | ||
+ | \' | ||
+ | // double quote | ||
+ | \" | ||
+ | // back slash | ||
+ | \\ | ||
+ | // tab | ||
+ | \t | ||
+ | // new line | ||
+ | \n | ||
+ | // carriage return | ||
+ | \r | ||
+ | </ | ||
+ | ===Arithmetic Expressions=== | ||
+ | * 包括 operand 和 operator | ||
+ | ==Integer Division== | ||
+ | Java 的整数除法会完全忽略小数部分,比如 '' | ||
+ | * 将任意一个 operand 改为浮点数 | ||
+ | * 强制转化 operand 为浮点数类型: | ||
+ | <code js> | ||
+ | 9.0 / 2 = 4.5 | ||
+ | 9 / 2.0 = 4.5 | ||
+ | 9.0 / 2.0 = 4.5 | ||
+ | 9D / 2 = 4.5 | ||
+ | 9 / 2D = 4.5 | ||
+ | 9D / 2D = 4.5 | ||
+ | </ | ||
+ | ==String Arithmetic== | ||
+ | String 的加法代表链接两个 string: | ||
+ | <code js> | ||
+ | //result is " | ||
+ | " | ||
+ | </ | ||
+ | ==order of precedence== | ||
+ | * 遵从基本的算数优先级 | ||
+ | * 使用括号改变优先级 | ||
+ | ==Mixed Type Expressions== | ||
+ | 在有不同类型参与的表达式计算中,Java 会对某些不合要求的 operand 进行 “promotion”,以此达到完成运算的目的。具体的类说: | ||
+ | * 整型和浮点型:整型会被提升为浮点型 | ||
+ | * 任何类型与 string 进行算术相加,都会被提升为 string,再进行连接。 | ||
+ | ===Conversion=== | ||
+ | 分为两种: | ||
+ | * Assignment Convenrsion | ||
+ | * Casting | ||
+ | ==Assignment Conversion== | ||
+ | * Coversion 是否合法取决于数据类型的范围是否能表示数据。比如: | ||
+ | <code js> | ||
+ | double average = 4.0; | ||
+ | int gpa = average; | ||
+ | </ | ||
+ | 就是非法的,因为 '' | ||
+ | \\ \\ | ||
+ | {{ cs: | ||
+ | ==Casting== | ||
+ | Casting 是手动的强制转换。写法为: | ||
+ | <code js> | ||
+ | (type)expression | ||
+ | </ | ||
+ | 比如下面的表达式: | ||
+ | <code js> | ||
+ | //casting 5 from int to double | ||
+ | //then do the 5.0 / 9 | ||
+ | (double)5/9 | ||
+ | </ | ||
+ | 注意,casting 的优先级高于所有的运算,除了括号。因此在括号参与的运算中,要先运算括号中的内容: | ||
+ | <code js> | ||
+ | //result is 0.0 | ||
+ | (double)(5/ | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | casting 不单用于算术类型。 | ||
+ | </ | ||
+ | ===Using Predefined Classes=== | ||
+ | ==Declaring Variables== | ||
+ | * Objects 变量被称为 reference 变量。其本身值是引用。 | ||
+ | * Java 中的对象存储在 heap 中,通过引用找到地址进行访问。 | ||
+ | ==Instantiation== | ||
+ | Java 中 Object 的实例化被称为 // | ||
+ | <code js> | ||
+ | new ClassName(parameters) | ||
+ | </ | ||
+ | 实例化通过构造函数(constructor)建立。以 '' | ||
+ | <code js> | ||
+ | String major = new String(" | ||
+ | </ | ||
+ | ==Invoking Methods== | ||
+ | Methods 通过 instance 的 identfier,也就是某个类的 reference 进行调用。比如打印: | ||
+ | <code js> | ||
+ | System.out.println(" | ||
+ | //no newline version | ||
+ | System.out.print(" | ||
+ | </ | ||
+ | '' | ||
+ | |||
+ | <WRAP center round box 100%> | ||
+ | 实例化的只有对象的数据。methods 属于 class,是唯一的。 | ||
+ | </ | ||
+ | ==Java 中的拷贝== | ||
+ | * primtive 类型的拷贝是直接复制 | ||
+ | * 对象名存储的是引用,因此拷贝对象等于**拷贝引用**,也就是复制地址。两者指向同一个实例。这种情况下,两者被称为 // | ||
+ | <WRAP center round tip 100%> | ||
+ | 跟 C++ 不同的是,如果在 Java 中更改了指向某个地址的引用,但忘记了释放指向的资源,Java 会在随后的恰当时间内**自动**回收这部分资源。这个功能被称为 Java 的 //Garbage Collection// | ||
+ | </ | ||
+ | ===String methods=== | ||
+ | * //Signiture Type// | ||
+ | * //String literal// (比如 ''" | ||
+ | * //String// is **IMMUTABLE**! | ||
+ | <WRAP center round box 100%> | ||
+ | Docs: [[https:// | ||
+ | </ | ||
+ | |||
+ | <code js> | ||
+ | major = " | ||
+ | interest = " | ||
+ | //compute the length of the string, return int | ||
+ | //ret_num has val 2 | ||
+ | int ret_num | ||
+ | |||
+ | //return a new string will switching all uppercases to lowercases. | ||
+ | // | ||
+ | //" | ||
+ | String newString = major.toLowerCase(); | ||
+ | |||
+ | // | ||
+ | //return a new string | ||
+ | //" | ||
+ | String concated = major.concat(interest) | ||
+ | |||
+ | //return a new string | ||
+ | //replace oldchar with newchar | ||
+ | //" | ||
+ | String replaced = major.replace(' | ||
+ | |||
+ | //return new string | ||
+ | //copying a range of characters from the string | ||
+ | //range is LEFT include. | ||
+ | //" | ||
+ | String subStr = interest.substring(0, | ||
+ | |||
+ | // returns the index of the first occurrence of the sequence, or return - 1 if no match | ||
+ | // support int, char, string | ||
+ | // char will be converted to Unicode int | ||
+ | indexOf(String str) | ||
+ | indexOf(int char) | ||
+ | indexOf(String str, int fromIndex) | ||
+ | indexOf(int char, int fromIndex) | ||
+ | </ | ||
+ | ==formal & actual parameter== | ||
+ | * method 使用的 paramter 被称为// actual parameter// | ||
+ | * 直接传递 actual paramter 给 formal paramter 时,actual parameter 会拷贝后再传递给 formal parameter。这个过程被称为 //pass by value// | ||
+ | ==substring index== | ||
+ | * Java 的 index 也是从 $0$ 开始。 | ||
+ | * Index error 属于 runtime error。编译器不会检查 | ||
+ | ===Input & MutiInput=== | ||
+ | ==Scanner== | ||
+ | <WRAP center round box 100%> | ||
+ | Docs: [[https:// | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | <code js> | ||
+ | import java.util.Scanner; | ||
+ | public class FahrenheitToCelsius { | ||
+ | public static void main(String[] args) { | ||
+ | Scanner input = new Scanner(System.in); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | 这里的 '' | ||
+ | <code bash> | ||
+ | 78 long | ||
+ | walks | ||
+ | </ | ||
+ | 会以: | ||
+ | <code bash> | ||
+ | 78 long\n\walks\n | ||
+ | </ | ||
+ | 这种 “流(stream)” 的形式被接收。流中的各个部分被称为 // | ||
+ | \\ \\ | ||
+ | '' | ||
+ | \\ \\ | ||
+ | 整个输入环节的顺序: | ||
+ | - 实例化 Scanner 对象 | ||
+ | - 使用 Scanner 对象调用对应的 Method (比如我要一个 '' | ||
+ | - 再创建一个变量接受 Scanner 对象调用 Method 的结果。 | ||
+ | |||
+ | <code js> | ||
+ | import java.util.Scanner; | ||
+ | public class FahrenheitToCelsius { | ||
+ | public static void main(String[] args) { | ||
+ | Scanner input = new Scanner(System.in); | ||
+ | System.out.print(" | ||
+ | int fah = input.nextInt(); | ||
+ | System.err.print(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | * Scanner 需要被导入才能使用:'' | ||
+ | * //next// 系列 method (除开 '' | ||
+ | * 输入类型不匹配的数据会导致抛出异常 | ||
+ | * //hasNext// 系列可以检查 Token 的数据类型。 | ||
+ | </ | ||
+ | ==hasNext 的用法== | ||
+ | //hasNext// 系列函数会返回一个 bool 值。为 true 时输入有效,反之无效。以 '' | ||
+ | <code js> | ||
+ | int num; | ||
+ | if (input.hasNextInt()) { | ||
+ | num = input.nextInt(); | ||
+ | } | ||
+ | else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | </ | ||
+ | ==多个输入== | ||
+ | 多个输入可以通过顺序性的调用 '' | ||
+ | <code js> | ||
+ | int fah = input.nextInt(); | ||
+ | System.out.print(" | ||
+ | String day = input.next(); | ||
+ | </ | ||
+ | 但有一点需要注意的是,'' | ||
+ | \\ \\ | ||
+ | 首先要明确的是存在多个输入的情况时,我们的输入流程: | ||
+ | - 输入数据 | ||
+ | - 回车输入下一个数据 | ||
+ | 这里的回车(// | ||
+ | <WRAP center round box 100%> | ||
+ | 输入数据 + 回车 + 输入数据只是一种流程,原理上是使用了 '' | ||
+ | </ | ||
+ | |||
+ | ==nextLine 的应用场景== | ||
+ | 使用 '' | ||
+ | <code js> | ||
+ | int fah = input.nextInt(); | ||
+ | //clean up the newline info | ||
+ | input.nexLine(); | ||
+ | //read the input | ||
+ | System.out.print(" | ||
+ | String day = input.nextLine(); | ||
+ | </ | ||
+ | Java 也提供了 method '' | ||
+ | <code js> | ||
+ | String day = input.nextLine().trim(); | ||
+ | </ | ||
+ | ===Packages & import=== | ||
+ | <WRAP center round box 100%> | ||
+ | Docs: | ||
+ | </ | ||
+ | |||
+ | Class 可以按照其提供的函数进行打包。 这些包通常被称为 packages,比如 System, String 等等。Java 中存在一个 package Long list,使用属于该 list 中的 package 时无需做额外的准备。任何不属于该 list 的 package,都需要 // | ||
+ | <code js> | ||
+ | import packageName.memberName; | ||
+ | </ | ||
+ | 需要注意的是,package 之间存在着继承关系。在导入的时候,需要按层级将所有的 package 写出来。比如 '' | ||
+ | <code js> | ||
+ | import java.util.Scanner; | ||
+ | </ | ||
+ | <WRAP center round info 100%> | ||
+ | Import 的 overhead cost 处于编译期,不影响运行期的 performance。 | ||
+ | </ | ||
+ | |||
+ | ==using wildcard in import== | ||
+ | 如果需要导入当前 package 的所有内容,可以使用通配符(// | ||
+ | <code js> | ||
+ | import java.util.*; | ||
+ | </ | ||
+ | ==confilct in import== | ||
+ | 假设有如下的两个 package: | ||
+ | <code js> | ||
+ | long.walks.Beach | ||
+ | swim.surf.Beach | ||
+ | </ | ||
+ | 这两个 package 都被称为 '' | ||
+ | <code js> | ||
+ | import long.walks.*; | ||
+ | import swim.surf.Beach; | ||
+ | //using the package | ||
+ | Beach tybee; | ||
+ | </ | ||
+ | 那么此时编译器会报错包冲突。比较好的解决方法是,导入其中一个,使用另外一个的时候直接使用其 // | ||
+ | <code js> | ||
+ | import long.walks.*; | ||
+ | //using full-qualified name to make the declaration | ||
+ | swim.surf.Beach tybee; | ||
+ | </ | ||
+ | ===Output & Formating=== | ||
+ | ==printf()== | ||
+ | Java 使用 '' | ||
+ | * '' | ||
+ | * // | ||
+ | 下面例子中,'' | ||
+ | <code js> | ||
+ | System.out.printf(" | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | * '' | ||
+ | * 如果希望打印 '' | ||
+ | </ | ||
+ | |||
+ | ==placeholder== | ||
+ | placeholder 的结构如下: | ||
+ | <code js> | ||
+ | %[flag][width][.precision]type | ||
+ | </ | ||
+ | 中间方括号的内容是可选的部分,type 是指之前例子中的 '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | 再来看看可选部分: | ||
+ | * 如果想要控制小数的位数,使用 '' | ||
+ | <code js> | ||
+ | double fahd = 25.6666666 | ||
+ | System.out.printf(" | ||
+ | </ | ||
+ | 打印结果为 '' | ||
+ | * 如果想要控制宽度,使用 '' | ||
+ | <code js> | ||
+ | System.out.printf(" | ||
+ | System.out.printf(" | ||
+ | </ | ||
+ | 这里设置 '' | ||
+ | <code bash> | ||
+ | The temperture is: 3.1 | ||
+ | Next day is: | ||
+ | |||
+ | The temperture is: 82.1 | ||
+ | Next day is: Tuesday | ||
+ | </ | ||
+ | 默认情况下长度是按照右对齐的。如果希望**左对齐**,将 '' | ||
+ | <code js> | ||
+ | System.out.printf(" | ||
+ | System.out.printf(" | ||
+ | </ | ||
+ | <code bash> | ||
+ | Please enter the day: Monday | ||
+ | The temperture is: 3.1 | ||
+ | Next day is: Monday | ||
+ | |||
+ | The temperture is: 82.1 | ||
+ | Next day is: Tuesday | ||
+ | </ | ||
+ | ==String.format== | ||
+ | '' | ||
+ | <code js> | ||
+ | String celsiusOutput = String.format(" | ||
+ | </ | ||
+ | ==NumberFormat== | ||
+ | Java 提供了一个 package: '' | ||
+ | <code js> | ||
+ | import java.text.NumberFormat; | ||
+ | </ | ||
+ | 在打印的时候需要建立一个 '' | ||
+ | <code js> | ||
+ | import java.text.NumberFormat; | ||
+ | |||
+ | double total = 25.666666; | ||
+ | NumberFormat currencyFmt = NumberFormat.getCurrencyInstance(); | ||
+ | System.out.println(" | ||
+ | </ | ||
+ | 打印结果为: | ||
+ | <code js> | ||
+ | Total is: ?25.67 | ||
+ | </ | ||
+ | 可以看到货币符号为 ''?'' | ||
+ | <code js> | ||
+ | import java.text.NumberFormat; | ||
+ | import java.util.Locale; | ||
+ | |||
+ | double total = 25.666666; | ||
+ | NumberFormat currencyFmt = NumberFormat.getCurrencyInstance(Locale.CANADA); | ||
+ | System.out.println(" | ||
+ | </ | ||
+ | 打印结果为: | ||
+ | <code js> | ||
+ | Total is: $25.67 | ||
+ | </ | ||
+ | ==DecimalFormat== | ||
+ | <WRAP center round box 100%> | ||
+ | Docs: [[https:// | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | * 数字 '' | ||
+ | 如果 '' | ||
+ | <code js> | ||
+ | double num = 13.1; | ||
+ | DecimalFormat outNum = new DecimalFormat(" | ||
+ | // print 013.10 | ||
+ | System.out.println(outNum.format(num)); | ||
+ | </ | ||
+ | 如果 '' | ||
+ | <code js> | ||
+ | | ||
+ | DecimalFormat outNum = new DecimalFormat(" | ||
+ | // print 13.1 | ||
+ | System.out.println(outNum.format(num)); | ||
+ | </ | ||
+ | 如果 '' | ||
+ | <code js> | ||
+ | double num = 1313.11933334; | ||
+ | DecimalFormat outNum = new DecimalFormat(" | ||
+ | // print 1313.12 | ||
+ | System.out.println(outNum.format(num)); | ||
+ | </ | ||
+ | * 字符 '' | ||
+ | <code js> | ||
+ | double num = 0.119333334; | ||
+ | DecimalFormat outNum = new DecimalFormat(" | ||
+ | // print 11.93% | ||
+ | System.out.println(outNum.format(num)); | ||
+ | </ | ||
+ | 注意这里小数部分,也就是 '' | ||
+ | * 字符 ''#'' | ||
+ | <code js> | ||
+ | double num = 0.119333334; | ||
+ | DecimalFormat outNum = new DecimalFormat("# | ||
+ | // print .12 | ||
+ | System.out.println(outNum.format(num)); | ||
+ | </ | ||
+ | ====Module 3==== | ||
+ | ==if & else statment== | ||
+ | 用法 与 C++ 相同,格式: | ||
+ | <code js> | ||
+ | if (condition) { | ||
+ | statement; | ||
+ | //... | ||
+ | } | ||
+ | else if (condition) { | ||
+ | statement; | ||
+ | //... | ||
+ | } | ||
+ | else { | ||
+ | statement; | ||
+ | //... | ||
+ | } | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | if / else 配对是找最近的彼此,与 C++ 相同。 | ||
+ | </ | ||
+ | ==The switch Statement== | ||
+ | 同 C++。 格式: | ||
+ | <code js> | ||
+ | switch (expression) { | ||
+ | case value1: | ||
+ | statement(s) | ||
+ | break; | ||
+ | case value2: | ||
+ | statement(s) | ||
+ | break; | ||
+ | default: | ||
+ | statement(s) | ||
+ | } | ||
+ | </ | ||
+ | * '' | ||
+ | * 不要忘记 '' | ||
+ | ===Comparing Non-numeric Data=== | ||
+ | * 字符的大小比较通过 ASCII 码来比较 | ||
+ | ==equals method== | ||
+ | Java 中比较两个 String 的时候,通常会出现两种方式:'' | ||
+ | * '' | ||
+ | * '' | ||
+ | 以上的区别解释了 Java 为什么会存在两种初始化 String 的方式: | ||
+ | <code js> | ||
+ | //method 1 | ||
+ | String xOne = " | ||
+ | //method 2 | ||
+ | String yOne = new String(" | ||
+ | </ | ||
+ | 如果是第一种方式,那么 '' | ||
+ | \\ \\ | ||
+ | 第二种方式会在堆上强制申请新的空间,因此其内容与第一种方式相同,但代表的 instance,在内存中的地址是不一样的。 | ||
+ | 因此: | ||
+ | <code js> | ||
+ | String xOne = " | ||
+ | String xTwo = " | ||
+ | String yOne = new String(" | ||
+ | |||
+ | //True, same address | ||
+ | if (xOne == xTwo) { //......} | ||
+ | //True, same content | ||
+ | if xOne.equals(xTwo) {//... } | ||
+ | |||
+ | //False, different address | ||
+ | if (xOne == yOne ) { //...} | ||
+ | //True, same content | ||
+ | if xOne.equals(yOne) {//... } | ||
+ | </ | ||
+ | ==compareTo method== | ||
+ | * String 不能用关系运算符比较**大小** | ||
+ | * 需要使用 '' | ||
+ | '' | ||
+ | - 返回值为 '' | ||
+ | - 按位比较,找出第一位不同的字符。返回的 '' | ||
+ | - 如果 '' | ||
+ | 一些实例: | ||
+ | <code py> | ||
+ | String a = " | ||
+ | String b = " | ||
+ | String c = " | ||
+ | |||
+ | // return -1 | ||
+ | System.out.println(a.compareTo(b)); | ||
+ | // return -3 | ||
+ | System.out.println(a.compareTo(c)); | ||
+ | // return 1 | ||
+ | System.out.println(b.compareTo(c)); | ||
+ | </ | ||
+ | ===Operators=== | ||
+ | ==Logical Operators== | ||
+ | * 与 C++ 相同。 | ||
+ | * 优先级: | ||
+ | * 括号最优先(推荐使用) | ||
+ | * '' | ||
+ | ==Short-circuit Evaluation== | ||
+ | Java 在逻辑表达式评估中会首先评估 left operand: | ||
+ | * 对于 | ||
+ | * 对于 '' | ||
+ | 这种评估方式被称为 //Short circuit evaluation// | ||
+ | |||
+ | * // | ||
+ | 尽量将运算复杂的条件作为 right operand,比如 function call 之类的。 | ||
+ | ==Ternary Conditional Operator== | ||
+ | 与 C++ 一致。 | ||
+ | <code js> | ||
+ | condition ? expression1 : expression2 | ||
+ | </ | ||
+ | ===Iteration=== | ||
+ | ==while== | ||
+ | * 写法与 C++ 相同 | ||
+ | * 循环的条件一般写称**左闭右开**,也就是初始次数包含在条件里,而终止条件是小于循环次数。 | ||
+ | 使用输入作为条件的循环: | ||
+ | <code js> | ||
+ | int condition = myInput.nextInt(); | ||
+ | while(condition != 100) | ||
+ | { | ||
+ | // ...do sth | ||
+ | condition = myInput.nextInt(); | ||
+ | } | ||
+ | </ | ||
+ | ==Do-while== | ||
+ | * 写法与 C++ 相同 | ||
+ | * 循环次数是额外的,不包括初始运行的那一次,可以写成 ''< | ||
+ | ==For== | ||
+ | * 同 C++ | ||
+ | * 循环变量是局部变量 | ||
+ | * 变量改变部分是运算表达式 | ||
+ | ==break & continue== | ||
+ | * 同 C++ | ||
+ | ====Module 4==== | ||
+ | ===Arrays=== | ||
+ | * Java 的 Array 性质与 C++ 类似:长度和类型都是被定义的。 | ||
+ | * Array 是指向内存序列的首地址。 | ||
+ | * 每个 Array 都会附带一个 final variable(常量)用于存储数组的长度。 | ||
+ | * Java 的 Array 可以存储 Objects;准确的说,是存储指向 Objects 的 references | ||
+ | ==Array 的定义== | ||
+ | 声明的方式: | ||
+ | <code js> | ||
+ | //both are fine | ||
+ | //first is recommonded | ||
+ | elementType[] identifier; | ||
+ | elementType identifier[]; | ||
+ | </ | ||
+ | 定义的方式: | ||
+ | <code js> | ||
+ | //default value of elements is 0.0 | ||
+ | double[] weekHighs = new double[7]; | ||
+ | </ | ||
+ | ==Default Values== | ||
+ | * Primtive numeric type 的初始值是 '' | ||
+ | * boolean 的初始值是 '' | ||
+ | * char 的初始值为空(不打印) | ||
+ | * String 的初始值为 '' | ||
+ | ==Specifying Values== | ||
+ | <code js> | ||
+ | //direct init | ||
+ | double[] weekHighs = {80, 70, 75, 69, 72, 74, 90}; | ||
+ | |||
+ | // | ||
+ | double[] weekHighs; // | ||
+ | weekHighs = {80, 70, 75, 69, 72, 74, 90}; // | ||
+ | |||
+ | //accessing by index(starts from 0, end at len - 1) | ||
+ | weekHighs[0]; | ||
+ | // | ||
+ | weekHighs[0] = 80; | ||
+ | // | ||
+ | weekHighs[0] = input.nextDouble(); | ||
+ | </ | ||
+ | ==Using Arrays== | ||
+ | <code js> | ||
+ | //accessing the length | ||
+ | weekHighs.length; | ||
+ | |||
+ | //looping array | ||
+ | for (int i = 0; i < weekHighs.length; | ||
+ | System.out.println(weekHighs[i]); | ||
+ | } | ||
+ | </ | ||
+ | ==The for-each Statement== | ||
+ | * 类似 C++ 的 Range based loop | ||
+ | * 优势是不用担心 index 越界 | ||
+ | 定义如下: | ||
+ | <code js> | ||
+ | for (arrayType element : array) {...} | ||
+ | //example | ||
+ | for (int element : weekHighs) { | ||
+ | System.out.println(element); | ||
+ | } | ||
+ | </ | ||
+ | ==Sparse Arrays and Null Checking== | ||
+ | 如果遍历的数组是对象数组(比如 String array): | ||
+ | * Java 默认该数组为 non-sparse array,即任意元素值都不为 '' | ||
+ | * 如果有值为 '' | ||
+ | // | ||
+ | <code js> | ||
+ | String concepts[] = new String[5]; | ||
+ | for (int i = 0; i < 4; ++i) { | ||
+ | concepts[i] = " | ||
+ | } | ||
+ | |||
+ | String result = "not found."; | ||
+ | |||
+ | for (String element : concepts) { | ||
+ | if ((element != null) && element.equals(" | ||
+ | result = " | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | System.out.println(result); | ||
+ | </ | ||
+ | <WRAP center round box 100%> | ||
+ | 注意这里的 null check 必须要优先检查,也就是至于 ''&&'' | ||
+ | </ | ||
+ | ==Command-line Arguments== | ||
+ | Java 的 '' | ||
+ | <code js> | ||
+ | // | ||
+ | double total = 0; | ||
+ | for (String num : args) { | ||
+ | total += Double.parseDouble(num); | ||
+ | } | ||
+ | System.out.println(total / args.length); | ||
+ | </ | ||
+ | 编译之后,只需要从命令行输入数据就可以得到结果了: | ||
+ | <code bash> | ||
+ | java Array 10 10 10 10 20 | ||
+ | 12.0 | ||
+ | </ | ||
+ | 需要注意的是,程序中自带了: | ||
+ | <code js> | ||
+ | total += Double.parseDouble(num); | ||
+ | </ | ||
+ | '' | ||
+ | ==2-D Arrays== | ||
+ | * 实质上是存储了一系列 array 首地址 的 array | ||
+ | * 可以想象为 '' | ||
+ | <code js> | ||
+ | // | ||
+ | elementType[][] indentifier; | ||
+ | indentifier elementType[][]; | ||
+ | |||
+ | //direct init | ||
+ | double[][] array2d = {{80, | ||
+ | |||
+ | //init with diaensions, with default value | ||
+ | //2 arrays, each has 3 double elements | ||
+ | double[][] array2d = new double[2][3] | ||
+ | |||
+ | // | ||
+ | array2d[0][0] = 80; | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | for (int row = 0; row < array2d.length; | ||
+ | for (int col = 0; col < array2d[0].length; | ||
+ | System.out.println(array2d[row][col]); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | <WRAP center round important 100%> | ||
+ | 必须记得使用 // | ||
+ | </ | ||
+ | ===Methods=== | ||
+ | ==method 的定义== | ||
+ | Java 的 method 定义分为四大块: | ||
+ | * modifier:处于定义的最开始,一般是 '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * return value type:如果没有返回类型,则使用 '' | ||
+ | * parameter list:method 需要的参数列表,必须指定类型。 | ||
+ | * method body: 如果 method 有返回类型,必须返回一个类型为返回类型的值,使用 return 语句。 | ||
+ | 简单的示例: | ||
+ | <code js> | ||
+ | public static String serachKeywords(String keyword, String[] array) { | ||
+ | | ||
+ | String result = "not found."; | ||
+ | for (String element : array) { | ||
+ | if (element != null && element.equals(keyword)) { | ||
+ | result = " | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | // calling | ||
+ | public static void main(String[] args) { | ||
+ | String[] myDict = {" | ||
+ | String result = serachKeywords(" | ||
+ | System.out.println(result); | ||
+ | } | ||
+ | </ | ||
+ | ==外部调用 method== | ||
+ | 假设 '' | ||
+ | * 将 '' | ||
+ | * 使用 '' | ||
+ | <code js> | ||
+ | public class External { | ||
+ | public static void main(String[] args) { | ||
+ | String[] newArray = {" | ||
+ | System.out.println(Methods.serachKeywords(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ==method overloading== | ||
+ | * 与 C++ 类似,相似功能但是 method 的 singature 不同,也就是**至少**满足下面一项: | ||
+ | * parameter 的**类型**不同 | ||
+ | * parameter 的**顺序**不同 | ||
+ | * parameter 的**数量**不同 | ||
+ | * return type 不是 signature 的一部分,因此只有 return type 不同不是 overloading | ||
+ | <code js> | ||
+ | // overloading | ||
+ | public static boolean searchArray(String target, String[] array) | ||
+ | public static boolean searchArray(int target, int[] array) | ||
+ | |||
+ | // not a overloading | ||
+ | public static int searchArray(int target, int[] array) | ||
+ | public static boolean searchArray(int target, int[] array) | ||
+ | </ | ||
+ | * 编译器会根据 parameter 的类型来进行函数匹配。如果没有匹配的函数会报错。 | ||
+ | * 参考 C++ 的[[cs: | ||