《JAVA语言程序设计》第2章.ppt



《《JAVA语言程序设计》第2章.ppt》由会员分享,可在线阅读,更多相关《《JAVA语言程序设计》第2章.ppt(62页珍藏版)》请在装配图网上搜索。
1、1,JAVA的基本语法,词法与句法 数据类型 (data type) 常量与变量 (variable) 运算符 (operator) 表达式 (expression) 语句 (statement),2,关键词 (Keyword),abstract double int strictfp ** boolean else interface super break extends long switch byte final native synchronized case finally new this catch float
2、 package throw char for private throws class goto * protected transient const * if public try continue implements return void default import short volatile do instanceof static while 注: *当前未被使用 * *使用于Java2,3,标识符 (Identifier),标识常量、变量、数据类型、类和方法 public class HelloWorl
3、d1 public static void main(String args) String message = “Hello World!”; myPrint(message); private static void myPrint(String s) System.out.println(s); ,4,标识符 (Identifier),组成规则 字母(AZ、az)、特殊符号($、_)和数字(09) 第1个符号不能为数字 不能为关键词(keyword)、true、false、null 区分大小写,5,标识符 (Identifier),例: point4、 5w、 A%、 thisP
4、icture、$currentValue、OK、_23b、Y_123、length、ab、if,5w、 A%、length、ab、if,6,标识符 (Identifier),一般约定 表示常量的标识符全部大写,如RED 表示类名的标识符用大写字母开始,如MyCar 表示公有方法和实例变量的标识符用小写字母开始,后面的描述性词以大写开始,如getCurrentValue 表示私有或局部变量的标识符全部用小写字母,如next_value,7,JAVA的基本语法,词法与句法 数据类型 (data type) 常量与变量 (variable) 运算符 (operator) 表达式 (expressio
5、n) 语句 (statement),8,数据类型,基本类型(primitive) 数字 (number) 整型 (integers) 字节整数 (byte, 8 bits): -128 127, 0 短整数 (short, 16 bits): -32768 32767, 0 整数 (int, 32 bits): -2147483648 2147483647, 0 长整数 (long, 64 bits): , 0L 实型 (real numbers): 浮点型 (有效位数不同) 单精度(float, 32 bits): , 0.0F 双精度(double, 64 bits): , 0.0D 字
6、符 (char, 16-bit Unicode字符): u0000 uffff 布尔 (boolean): true, false 构造/引用类型(reference): 数组(Array), 类(class), 接口(interface),9,数据类型,示例 int i = 178; long l = 8864L; (8864l) double d1 = 37.266; double d2 = 37.266D; (37.266d) double d3 = 26.77e3; float f = 87.363F; (87.363f) char c = d; boolean b1 = true
7、; boolean b2 = false;,10,JAVA的基本语法,词法与句法 数据类型 (data type) 常量与变量 (variable) 运算符 (operator) 表达式 (expression) 语句 (statement),11,常量,程序执行过程中,值(Value)保持不变的量 整型常量 实型常量 字符常量 字符串常量,12,常量,整型常量 常用十进制、八进制、十六进制表示 有正负号,13,常量,实型常量 双精度实数(double, 8个字节, 数字后加字母D或d) 浮点实数(float, 4个字节,数字后加字母F或f) 若无明确字母标识,则系统默认为双精度实数 两种表示
8、方法 十进制: 数字和小数点组成,必须有小数点,例 0.12, .12, 12., 12.0 科学计数法: 123e3, 123E3, 0.4e8D, -5e9,14,常量,字符常量 用单引号括起来的单个字符 例: a, A, , , System.out.println(javar语言); System.out.println(javat语言); System.out.println(java语言); System.out.println(java语言); System.out.println(“”java语言“”); ,C:java Test java 言 语言 java语言 jav
9、a语言 java语言 “java语言” C:,17,变量,程序执行过程中,值(Value)可以改变的量 整型变量、实型变量、字符变量、字符串变量、布尔变量、引用变量等 变量定义 类型 变量名 =初值, 变量名=初值 类型: 基本数据类型或引用类型 int x, y, z; float a, b; char c1, c2, c3; double d1; boolean mycom;,18,变量,变量赋初值/初始化(Initialization) 在变量声明时赋值 类型 变量名 =初值, 变量名=初值 int x=1, y=2, z=3; float e = 2.718281828f; 采
10、用赋值语句 float pi, y; pi = 3.1415926f; y = 2.71828f;,19,变量,final变量 带有关键字final的变量 final int aFinalVar = 0; final变量初始化后不能再改变 final int blankfinal; . . . blankfinal = 0; blankfinal = 3;,20,变量,final变量示例,public class TestSpot public static void main(String args) Spot x = new Spot(3, 4); Spot.st.Count = 2; S
11、pot.st.Count = 5; System.out.println(x.b + , + Spot.st.b + , + Spot.st.Count); ,class Spot final static Spot st = new Spot(1, 2); int a, b; int Count; Spot(int a, int b) this.a = a; this.b = b; ,Spot.st = new Spot(9, 5); cannot assign a value to final variable st,Spot.st.a = 5; Spot.st.b = 6;,21,,变量
12、,作用域(Scope)变量的使用范围,class Spot final static Spot st = new Spot(1, 2); int a, b; int Count; Spot(int a, int b) this.a = a; this.b = b; ,,成员变量 a,b,Count 范围,,方法参数 a,b 范围,,22,,变量,作用域(Scope),class MyClass 成员变量申明 public void aMethod(方法参数) 局部变量申明 ,,成员变量 范围,,方法参数 范围,,,,局部变量 范围,23,变量,作用域(Scope),if ( ..
13、. ) int i = 17; ... System.out.println(The value of i = + i);,24,JAVA的基本语法,词法与句法 数据类型 (data type) 常量与变量 (variable) 运算符 (operator) 表达式 (expression) 语句 (statement),25,运算符 (operator),一元运算符 unary operator operator op (++1) op operator (1++) 二元运算符 binary operator op1 operator op2 (1+1) 三元运算符 sternary
14、 operator op1 ? op2 : op3 注: op表示操作数,26,算术运算符 (Arithmetic Operators) 关系运算符 (Relational Operators) 逻辑运算符 (Logical Operators) 位运算符 (Bitwise Operators) 移位运算符 (Shift Operators) 条件运算符 (Conditional Operator) 运算符的优先级,运算符 (operator),27,算术运算符(Arithmetic Operators) 加法运算符 + “op1 + op2” 减法运算符 - “op1 - op2” 乘法运算
15、符 * “op1 * op2” 除法运算符 / “op1 / op2” 求模运算符% “op1 % op2” 计算余数,int i = 37 int j = 42 double x = 27.475 double y = 7.22,Adding... i + j = 79 x + y = 34.695,Subtracting... i - j = -5 x - y = 20.255,Multiplying... i * j = 1554 x * y = 198.37,Dividing... i / j = 0 x / y = 3.8054,Computing the remai
16、nder... i % j = 37 x % y = 5.815,运算符 (operator),28,关系运算符(Relational Operators) 比较运算,计算结果“true”或“false” 大于 “op1 op2” 大于等于 = “op1 = op2” 小于 、=、 (==、!=) 关系运算符低于算术运算符,运算符 (operator),29,关系运算符(Relational Operators) class Test public static void main(String args) int w=25, x=3; boolean y = w = w * 2
17、x * 9; boolean cc = b a; System.out.println(“w < x = ” + y); System.out.println(“z = ” + z); System.out.println(“cc = ” + cc); ,,运算符 (operator),30,逻辑运算符(Logical Operators) 操作数的逻辑关系,计算结果“true”或“false” 逻辑与 int aa = a; System.out.println(“aa=” + aa); 42 00101010 -43 11010101,,运算符 (operator),34,按位与
18、int aa = a << 2; System.out.println(“aa=” + aa); 42 00101010 <<2 168 10101000 相当于 42*22 = 168 运算速度比乘法快 注意: 溢出,,运算符 (operator),40,左移 << “op1 << op2” 溢出 byte j = 42; byte j1 = (byte) (j << 1); //84 byte j2 = (byte) (j << 2); //168 byte j3 = (byte) (j << 3); //336 System.out.println(j1= + j1); S
19、ystem.out.println(j2= + j2); System.out.println(j3= + j3); 00101010 //42 01010100 //84 10101000 //-88 01010000 //80,,运算符 (operator),41,右移 “op1 op2” 将操作数op1的二进制位向右移op2(正整数)位 高位补零(原为正数)、高位补1(原为负数) int a = 42; int aa = a 2; System.out.println(“aa=” + aa); 42 00101010 2 10 00001010 相当于 42/22 = 10.5 运
20、算速度比除法快,,运算符 (operator),42,无符号右移 “op1 op2” 将操作数op1的二进制位向右移op2(正整数)位 高位补零,零扩展(zero-extension) int a = 42; int aa = a 2; System.out.println(“aa=” + aa); 42 00101010 2 10 00001010 相当于 42/22 = 10.5 运算速度比除法快 实现数的拼接,,运算符 (operator),43,条件运算符 op1 ? op2 : op3 若op1为真,则运算结果为op2,否则为op3 例 z = a 0 ? a : -a; z
21、= a b ? a : b;,,运算符 (operator),44,自增、自减运算符,变量赋值,一元运算符 自增运算符(++)、自减运算符(--) int i=5; i++; ++i; i--; --i; “赋值”和“运算”的先后顺序,int i = 10; int n = i++%5;,float x =7, y=15, v1, v2; v1 = x++; v2 = ++y;,int i = 10; int n = ++i%5;,x=8 y=16 v1=7 v2=16,i = 11, n = 0,i = 11, n = 1,45,运算符的优先级,,,后缀运算符 postfix operato
22、rs . (params) expr++ expr-- 一元运算符 unary operators ++expr --expr +expr -expr ! 构造或类型转换creation or cast new (type)expr 乘法 multiplicative * / % 加法 additive + - 移位 shift 关系 relational = instanceof 相等 equality == != 按位与 bitwise AND x+y*z (x+y)*z x*-y 说明 表达式力求简单
23、明了 表达式中的变量必须赋值,,表达式 (expression),49,算术表达式 例: 若 i=10,i--%50 = ? 等价于 (i--%5)0 public class Test public static void main(String args) int i = 10; if ((i-- % 5)0) System.out.println(true); else System.out.println(false); ,,表达式 (expression),50,关系表达式 将两个表达式连接起来的式子 算术表达式、赋值表达式、字符表达式 ab; a+bb
24、-c; (a=3)(b=5); ba; 返回结果为一个布尔类型的值 例 若 int a=3, b=2, c=1; boolean d, f; d=ab; f=(a+b)(b+5); d=abc; ???,,表达式 (expression),51,逻辑表达式 用逻辑运算符将关系表达式和布尔值连接起来的式子 例 int x=23, y=98; boolean a = true, b=false, c, d; c=(xy),,表达式 (expression),52,逻辑表达式 例: 闰年(leap year) A year in the Gregorian calendar having 366
25、days, with the extra day, February 29, intercalated to compensate for the quarter-day difference between an ordinary year and the astronomical year. 条件符合下例两者之一 能被4整除,但不能被100整除 能被4整除,又能被400整除 (year%4==0 b=c=d=a+5; a=5+c=5; //错误 a=(b=4)+(c=6);,,表达式 (expression),54,复合赋值运算 复合赋值运算符 +=、 -=、 *=、 /=、%= =
26、、 = (),,表达式 (expression),55,类型转换 (Casting) 将一种类型的数据转换为另一种类型的数据 操作数转换为同种类型,然后运算 整数型、实数型和字符型 表达形式: (类型) 操作数 应用场合 二元运算符的二个操作数类型不同 表达式值的类型与变量的类型不同 两种方法 隐型类型转换: 自动类型转换(系统完成) 显型类型转换: 强制类型转换,表达式 (expression),56,隐型类型转换:自动类型转换(系统完成) 宽化转换(widening conversion) byte j=60; short k=4; int l=31; long m=4l; long re
27、sult=0l; result +=j-8; result *=k+2; result /=m+1; result -=l; result %=m;,类型转换 (casting),57,隐型类型转换:自动类型转换(系统完成) 类型转换表 源类型 转换后不会丢失数据的目的类型 byte short, char, int, long, float, double short char, int, long, float, double char int, long, float, double int long, float, double long float, double flo
28、at double,,类型转换 (casting),58,显型类型转换: 强制类型转换 窄化转换 (narrowing conversion) double a = 1.5; float b = a; System.out.println(“b= + b); 编译: “possible loss of precision” 数据精度丢失数据丢失 double a = 1.5; float b = (float)a; System.out.println(“b= + b);,类型转换 (casting),59,显型类型转换: 强制类型转换 class Test public static vo
29、id main(String args) int a = 257; byte b = (byte)a; System.out.println(a= + a); System.out.println(b= + b); 257 00000000 00000000 00000001 00000001 1 00000001,类型转换 (casting),60,显型类型转换: 强制类型转换 class Test public static void main(String args) char c1 = A, c2; // A的ASCII值为65 int i; i = (
30、int) c1 + 1; c2 = (char) i; System.out.println(c1 + c2); System.out.println(c1 + “ ,” +c2); ,,类型转换 (casting),61,JAVA的基本语法,词法与句法 数据类型 (data type) 常量与变量 (variable) 运算符 (operator) 表达式 (expression) 语句 (statement),62,语句 (statement),表达式 + 分号“;”表达式语句 x = 25; y += a*b+c; a+b; 只有分号空语句 i = 5; ; ; 符合语法规则(程序设计的初始阶段),,
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。