1. Hello World
public class HelloWorld{public static void main(String[] args){System.out.println("Hello World");}}
要点:明白几个key words public, static , main , String[] args 。
2. Constant Value 常量
常量就是不变的数据量, 在程序执行的过程中其值不可以发生改变
整数类型:
- 十进制表示方式:正常数字 如 13、25等
- 二进制表示方式:以0b(0B)开头 如0b1011 、0B1001
- 十六进制表示方式:以0x(0X)开头 数字以0-9及A-F组成 如0x23A2、0xa、0x10
- 八进制表示方式:以0开头 如01、07、0721
3. Variables and Primitive Data Type 变量和基本类型
A. Primitive Data Types:
| 四类 | 八种 | 字节数 | 范围 | | :—-: | :—-: | :—-: | :—-: | | Integer | byte | 1 | -128~127 | | | short | 2 | -32768~32767 | | | int | 4 | -2147483648~2147483648 | | | long | 8 | -2的63次方到2的63次方-1 | | Floater | float | 4 | | | | double | 8 | | | Character | char | 2 | 采用unicode编码,它的前128字节编码与ASCII兼容 | | Boolean | boolean | 1 | |
注意:
- 整形常量默认是int类型, 定义长整形数据如果值超过int取值范围后面要
+L - 小数常量默认是double类型, 定义float类型的数据后面要
+f - 字符常量的表示:
char myUnicodeChar = ‘\u00A9’


- char类型的数据参加运算时,会自动转换转成int数据类型。
int转成char类型的时候,要强制转换。 ```java //char参加int运算时会自动转换成int类型 char c = ‘a’; int i = c+1;
//int转换成char时需要强制转换 int j = 90; char h = (char) j;
<a name="AH8kI"></a>## B. CastingCasting means to treat or convert a number from one type to another. We put the type we want the number to be in parenthesis like this:<br />`(byte)(myMinByteValue/2)`<a name="bS3tC"></a>### 1) Auto-casting范围小的数据类型转换成范围大的数据类型时,会进行 `auto-casting` 。如:```javadouble d = 1000;int i = 100; double d2 = i;
2) Force-casting
当要把占字节多的数据类型转换成占字节少的数据类型,数据会溢出(overflow),此时必须进行强制转换。
int i = (int)6.718;int i2 = (int)7000L;
3. Operators 运算符
A. Arithmetic Operator
| 运算符 | 运算规则 | 范例 | 结果 |
|---|---|---|---|
| + | positive | +3 | 3 |
| + | plus | 2+3 | 5 |
| + | 连接字符串 | “中”+“国” | “中国” |
| - | negative | int a=3;-a | -3 |
| - | minus | 3-1 | 2 |
| * | multiply | 2*3 | 6 |
| / | divide | 5/2 | 2 |
| % | mod | 5/2 | 1 |
| ++ | auto-increasement | int a=1; a++/++a |
2 |
| — | auto-decreasement | int b=3; b—/—b |
2 |
注意事项
- 加法运算符在连接字符串时要注意,只有直接与字符串相加才会转成字符串。
- 除法
/当两边为整数时,取整数部分,舍余数。当其中一边为浮点型时,按正常规则相除。 %为整除取余符号,小数取余没有意义。结果符号与被取余符号相同。- 整数做被除数,0不能做除数,否则报错。
小数做被除数,整除0结果为Infinity,对0取模结果为
NaNB. Assignment Operator
+=,-=,*=,/=,%=
上面的运算符作用:将等号左右两边计算,会将结果自动强转成等号左边的数据类型,再赋值给等号左边的-
C. Comparation Operator
D. instaceof(var)
用于检测变量的类型: ```java class Animal{ }
class Dog extends Animal{ }
Dog d = new Dog(); d instanceof Animal //true
<a name="qEhf5"></a>## E. Ternary operator (?:)(条件表达式)?表达式1:表达式2;<br />`System.out.println( 3>2 ? "正确":"错误");`<a name="CIwVc"></a>## F. Operator Priority| 优先级 | 描述 | 运算符 || --- | --- | --- || 1 | 括号 | ()、[] || 2 | 正负号 | +、- || 3 | 自增自减,非 | ++、--、! || 4 | 乘除,取余 | *、/、% || 5 | 加减 | +、- || 6 | 移位运算 | <<、>>、>>> || 7 | 大小关系 | >、>=、<、<= || 8 | 相等关系 | ==、!= || 9 | 按位与 | & || 10 | 按位异或 | ^ || 11 | 按位或 | | || 12 | 逻辑与 | && || 13 | 逻辑或 | || || 14 | 条件运算 | ?: || 15 | 赋值运算 | =、+=、-=、*=、/=、%= || 16 | 位赋值运算 | &=、|=、<<=、>>=、>>>=<br /> |<a name="lL1Wv"></a># 4. Reference Type 引用类型<a name="eeYgN"></a>## A. How to use reference type?与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式。<br />`数据类型 变量名 = new 数据类型();` <br />每种引用数据类型都有其功能,我们可以调用该类型实例的功能。<br />`变量名.方法名();`<a name="gVWjn"></a>## B. ScannerScanner类是引用数据类型的一种,我们可以使用该类来完成用户键盘录入,获取到录入的数据。<br />Scanner类的使用:1. 导包 `import java.util.Scanner;`1. 创建键盘录入对象 `Scanner sc = new Scanner(System.in);`1. 读取键盘录入的一个整数 `int enterNumber = sc.nextInt();`1. 读取键盘录入的字符串 `String enterString = sc.nextLine();`**注意:**当读完一个整数后,必须加 `scanner.nextLine()` 去吸收回车。 否则下一个读字符串时系统会认为你已经输了一个字符串。<a name="OlcfW"></a># 5. Statements 语句<a name="kbaPn"></a>## A. If statement```javaif(condition) {//condition is true} else {//condition is false}if (condition1) {//match condition 1.} else if (condition2) {// match condition 2 but doesn't match condition 1.} else {//match other conditions.}
B. Switch
int switchValue = 3;switch(switchValue){case 1:System.out.println("Value was 1");break;case 2:System.out.println("Value was 2");break;case 3:case 4:case 5:System.out.println("Value was 3 or 4 or 5");break;default:System.out.println("Actually it was "+switchValue);}
C. For loop
一般用在循环次数已知的情况下。
// 判断一个数是不是质数public static boolean isPrime(int num){for(int i = 2; i<=num/2; i++){if(n%i==0){return false;}}return true;}
D. While loop
while语句会反复地进行条件判断,只要条件成立,{}内的执行语句就会执行,直到条件不成立,while循环结束。
int x = 1;while(x <= 4){System.out.println("x = " + x);x++;}
E. Do While loop
With the do while loop the code block is executed at once and then the condition is checked.
Always check your conditions and expressions to ensure there’s no infinite loop.
F. Break, Continue and Return
break;跳出所在当前的循环体。只能跳出最近的代码块,不能跨越多级代码块。continue;直接跳过本轮循环进行下次循环。只要遇到return,该function就停止执行并返回返回值。
G. Flag
- 为什么使用循环flag:
当在双层循环或者循环内有switch选择语句时,我们发现,使用break或者continue所作用的对象均是内层语句,无法直接跳出外层循环,这时就需要使用flag语句跳转了. - 使用方式:
- 在外层循环外的某行前边,使用后边跟有冒号”:”的标识符,即定义完毕。
myFlag: for(int i=0; i<10;i++) - 使用时当在内层循环使用break或continue时后边紧跟之前定义的标号即可.
break itcast;
- 在外层循环外的某行前边,使用后边跟有冒号”:”的标识符,即定义完毕。
- 运行规律:
- 当外层循环外定义了标号
- 内层使用
break+flag,终止内外双层循环。 - 内层使用
continue+flag,终止内层循环,继续外层循环。myFlag: for (int i=0; i< 5; i++){for(int j=0; j<10; j++){if(i > 4){break myFlag; //跳出外层循环}System.out.println("*");}System.our.println("\n");}
6. Method 函数方法
A. Method Format:
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) {
方法体语句;
return 返回值;
}public static int getArea(int w, int h){//实现方法的功能主体int area = w * h;return area;}

- 为什么使用循环flag:
方法的格式说明:
- 修饰符(method modifer):目前就用 public static。后面我们再详细的讲解其他的修饰符。
- 返回值类型(return type):就是功能结果的数据类型。
- 方法名(function name):符合命名规则即可。方便我们的调用。
- 参数:
- 实际参数(arguments):就是实际参与运算的。
- 形式参数(parameters):就是方法定义上的,用于接收实际参数的。
- 参数类型:就是参数的数据类型
- 参数名:就是变量名
- 方法体语句:就是完成功能的代码。
- return:结束方法的。
- 返回值:就是功能的结果,由return带给调用者。
需要特别注意的是,方法中的“参数类型 参数名1,参数类型 参数名2”被称作参数列表,它用于描述方法在被调用时需要接收的参数,如果方法不需要接收任何参数,则参数列表为空,即()内不写任何内容。方法的返回值必须为方法声明的返回值类型,如果方法中没有返回值,返回值类型要声明为void,此时,方法中return语句可以省略。
B. Overload 重载
在同一个类中,方法名相同,参数列表不同。与返回值类型无关。
参数列表不同:
- 参数个数不同
- 参数类型不同
- 参数的顺序不同(算重载,但是在开发中不用)
C. Pass parameter
- 当调用方法时,如果传入的数值为基本数据类型(包含String类型),形式参数的改变对实际参数不影响。
当调用方法时,如果传入的数值为引用数据类型(String类型除外),形式参数的改变对实际参数有影响。

改变数组指针不会影响传入数组的真实指针。但是改变数组元素的值,会对实际数组的值有影响。
D. Flexible Args 方法的可变参数
前提:方法参数数据类型确定,参数的个数任意。本质就是一个数组。
可变参数语法:数据类型``...``变量名public class VarArgumentsDemo {public static void main(String[] args) {//调用一个带有可变参数的方法,传递参数,可以任意int sum = getSum(5,34,3,56,7,8,0);System.out.println(sum);}/** 定义方法,计算10个整数和* 方法的可变参数实现*/public static int getSum(int...a){int sum = 0 ;for(int i : a){sum = sum + i;}return sum;}}
