1 / 68

2. Java 编程语言基础知识

2. Java 编程语言基础知识. 主要内容. 标识符和保留字 数据类型 类型转换 运算符和表达式 控制语句. 标识符(课本第 10 页). 以字母,下划线( _ ) , 美元符号( $ )开始的一个字母序列;后续字符可以是字母,数字,下划线,美元符, Unicode 字符集中大于 0xC0 的所有符号,运算符除外。 大小写敏感,没有最大长度限制 不能与保留字重名( P10 ) 实际编程序中,甚至标识符完全可以用中文,比如可以把一个变量的名字命名为 int 中文 =1 ;. 标识符举例. 合法的标识符

casta
Download Presentation

2. Java 编程语言基础知识

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2. Java编程语言基础知识

  2. 主要内容 • 标识符和保留字 • 数据类型 • 类型转换 • 运算符和表达式 • 控制语句

  3. 标识符(课本第10页) • 以字母,下划线(_),美元符号($)开始的一个字母序列;后续字符可以是字母,数字,下划线,美元符,Unicode字符集中大于0xC0的所有符号,运算符除外。 • 大小写敏感,没有最大长度限制 • 不能与保留字重名(P10) • 实际编程序中,甚至标识符完全可以用中文,比如可以把一个变量的名字命名为 • int 中文=1;

  4. 标识符举例 • 合法的标识符 identifier userName User_Name _sys_val $change • 不合法的标识符 2mail room# class

  5. 练习题 Which declarations of identifiers are legal? • $persons • TwoUsers • *point • this • _endline

  6. 保留字(第10页) • 具有专门的意义和用途,不能当作一般的标识符使用,这些标识符称为保留字(reserved word),也称为关键字: • 都是小写,比如true,false,null等,不像c++中都是大写的。 • 注意和c ++的保留字的区别,增加了哪些,又去掉了哪些。 • 比如在java中没有sizeof运算符,因为数据类型的长度与平台无关。

  7. 保留字 abstract break byte boolean catch case class char continue default double do else extends false final float for finally if import implements int interface instanceof long length native new null package private protected public final return switch synchronized short static super try true this throw throws threadsafe transient void while

  8. 练习题 Which of the following are Java keywords? 1) NULL 2) new 3) instanceOf 4) wend

  9. 数据类型 • 数据类型的分类 • 简单数据类型,复杂数据类型 • byte,short,int,long,float,double • char,boolean • class,interface,数组 • 数的范围,位长 • 不支持c/C++中的指针类型,结构类型(struct)、联合类型(union),枚举类型(enum)

  10. 常量,变量 • 常量用文字串表示,比如 3,‘a’等 • 通过final定义常量, final typeSpecifier varName=value[,varName[=value]…]; 如 final int i=0; • 变量是java中的基本存储单元,包括变量名,类型,作用域等。 int i=0,j=1; • 局部变量,类变量,方法参数,例外处理参数等。

  11. 简单数据类型-boolean类型 • 布尔型数据只有两个值true和false,且它们不对应于任何整数值 布尔型变量的定义如: boolean b=true; boolean a=false;

  12. 简单数据类型-字符类型char • 字符常量 字符常量是用单引号括起来的一个字符,如‘a’,‘A’; • 字符型变量 类型为char,它在机器中占16位,16位无符号型数据,其范围为0~65535。字符型变量的定义如: char c=‘a’; /*指定变量c为char型,且赋初值为'a’*/ • Java也提供转义字符,以反斜杠(\)开头,P13,比如\’表示单引号,\uxxxx,1到4位16进制表示的字符。 • 如何从字符转换到它的编码,可以用jdk提供的工具native2ascii.exe程序。 • 举例,“中文”的unicode编码是:“\u4e2d\u6587”

  13. 简单数据类型-字符类型char(续) • Java中的字符型数据不能用作整数,因为java不提供无符号整数类型,但是同样可以把他当作整数数据来操作。例如: int three=3; int one=‘1’; char four=(char)(three+one);//four=‘4’

  14. 简单数据类型-整型数据 数据类型所占位数数的范围 byte8-27~27-1 short 16 -215~215-1 int 32 -231~231-1 long 64 -263~263-1 在定义时,long型数据要加后缀l或者L。

  15. 浮点型(实型)数据 • 实型常量 1. 十进制数形式 由数字和小数点组成,且必须有小数点,如0.123, .123, 123.,123.0 2. 科学计数法形式 如:123e3或123E3,其中e或E之前必须有数字,且e或E后面的指数必须为整数。

  16. 浮点型(实型)数据 • 实型变量 数据类型所占位数数的范围 float 32 3.4e-038~3.4e+038 double 64 1.7e-308~1.7e+308 • 注意:定义float时,需加后缀f或F

  17. 使用举例 public class Test { byte b=21; short s=256; int i=100000; long l=0xffL; double d=0.7e-3; boolean b=false; }

  18. 练习题 Which of the following are legal statements? 1) float f=1/3; 2) int i=1/3; 3) float f=1.01; 4) double d=999d;

  19. 类型转换 • 自动类型转换 整型,实型,字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算,转换从低级到高级; 低------------------------------------------------>高 byte ,short ,char—> int —> long —> float —> double

  20. 自动类型转换规则 • 操作数1类型 操作数2类型 转换后的类型 • byte、short、char int int • byte、short、char、int long long • byte、short、char、int、long float float • byte、short、char、int、long、float double double

  21. 强制类型转换 • 高级数据要转换成低级数据,需用到强制类型转换,如: int i; byte b=(byte)i; /*把int型变量i强制转换为byte型*/

  22. 运算符和表达式 • 与我们学过的C++是相同的。 • 注意运算符的类型 • 优先级 • 参与计算的数的类型 • 一些运算符的特殊用途。

  23. 运算符(按照操作数区分) • 一元运算符:++,--,+,- • 二元运算符:+,-,> • 三元运算符:?: 注:只列出了部分。

  24. 运算符(按照功能来分) 1)算术运算符: +,―,*,/,%,++,―― 3+2; a-b; i++; --i; 2)关系运算符: >,<,>=,<=,==,!= count>3; i==0; n!=-1; 3)布尔逻辑运算符: !,&&,|| flag=true; !(flag); flag&&false;

  25. 运算符(按照功能来分) 4)位运算符: >>,<<,>>>,&,|,^,~ a=10011101; b=00111001; a<<3 =11101000; a>>3 =11110011 a>>>3=00010011; a&b=00011001; a|b=10111101; ~a=01100010; a^b=10100100; • 注意,1<<32等于多少,还有1>>32, 它们的值等于多少?

  26. 运算符(按照功能来分) 5)赋值运算符 =,及其扩展赋值运算符如+=,―=,*=,/=等。 i=3; i+=3 等效于i=i+3 6)条件运算符 ? : result=(sum= =0 ? 1 : num/sum);

  27. 运算符(按照功能来分) 7)其它:包括分量运算符· ,下标运算符 [],实例运算符instanceof,内存分配运算符new,强制类型转换运算符 (类型),方法调用运算符 () 等。 System.out.println(“hello world”); int array1[]=new int[4];

  28. 练习题 Which of the following will compile without error? 1)char c='1';System.out.println(c>>1); 2)Integer i=Integer("1"); System.out.println(i>>1); 3)int i=1; System.out.println(i<<<1); 4)int i=1; System.out.println(i<<1);

  29. 表达式的类型 • 表达式的类型由运算以及参与运算的操作数的类型决定,可以是简单类型,也可以是复合类型: • 布尔型表达式: x&&y||z; • 整型表达式: num1+num2

  30. 练习题 Given the following class public class ZeroPrint{ public static void main(String argv[]){ int i =0; //Here } }

  31. SCJP试题 Which of the following lines if placed after the comment //Here will print out 0. 1) System.out.println(i++); 2) System.out.println(i+'0'); 3) System.out.println(i); 4) System.out.println(i--);

  32. 运算符的优先次序 • 表达式的运算按照运算符的优先顺序从高到低进行,同级运算符从左到右进行: • . [] () • ++ -- ! ~ instanceof • new (type) • * / % • + - • >> >>> << • > < >= <=

  33. 运算符的优先次序 • == != • & • ^ • | • && • || • ?: • = += -= *= /= %= ^= • &= |= <<= >>= >>>=

  34. 运算符的优先次序示例 • Resutl=sum==0?1:num/sum; • 第1步:result=sum==0?1:(num/sum) • 第2步:result=(sum==0)?1:(num/sum) • 第3步:result=((sum==0)?1:(num/sum)) • 第4步:result= • 注:可以用括号()显式的标明运算次序。

  35. 练习题 • Which of the following are Java key words?1) double2) Switch3) then4) instanceof

  36. 练习题 • Which of the following are not Java keywords?1)volatile2)sizeOf3)goto4)try

  37. 练习题 Given the following variables which of the following lines will compile without error? String s = "Hello"; long l = 99; double d = 1.11; int i = 1; int j = 0; 1) j= i <<s; 2) j= i<<j; 3) j=i<<d; 4)j=i<<l;

  38. 练习题 Given the following variables char c = 'c'; int i = 10; double d = 10; long l = 1; String s = "Hello"; Which of the following will compile without error? 1) c=c+i; 2) s+=i; 3) i+=s; 4) c+=s;

  39. 练习题 • What will be output by the following statement? System.out.println(-1 >>>1); 1) 02) -13) 14)2147483647

  40. 练习题 • What will be output by the following statement? System.out.println(1 <<32); 1) 12) -13) 324)-2147483648

  41. 控制语句 • 分支语句,if else,switch • 循环语句,for ,while,do while • 其他相关语句 break,continue,return • 例外处理语句,try..catch..finally,throw • 注释语句//,/*…*/,/**…/

  42. 分支语句-if • 条件语句 if-else if(boolean-expression) statement1; [else statement2;] 注意: 1.布尔表达式是任何一个返回布尔型数据的表达式。 2.每一个语句后面必须有分号; 3。If语句可以嵌套使用。

  43. 练习题 • 写出输出结果 int output=20; boolean b1=false; if((b1==true)&&((output+=10)==30)) { System.out.println(“We are equal :”+output); }else { System.out.println(“Not equal:”+output); }

  44. 分支语句-switch • 多分支语句 switch switch (expression){ case value1 : statement1; break; case value2 : statement2; break; …………

  45. switch case valueN : statemendN; break; [default : defaultStatement; ] } • 表达式expression的返回值类型必须是这几种类型之一:int,byte,char,short。 • case子句中的值valueN必须是常量,而且所有case子句中的值应是不同的。

  46. switch • default子句是可选的。 • break语句用来在执行完一个case分支后,使程序跳出switch语句,即终止switch语句的执行。 • Case语句只是起到一个标号作用,用来查找匹配的入口并从此处开始执行其后的语句序列,对后面的case子句不再进行匹配。

  47. switch试题 • 写出输出结果 int i = 9;switch (i) {default:System.out.println("default");case 0:System.out.println("zero");break;case 1:System.out.println("one");case 2:System.out.println("two");}

  48. 循环语句 • while语句 • do-while语句 • for语句

  49. While语句 [initialization] while (termination){ body; [iteration;] } • 先计算终止条件

  50. Do-while语句 [initialization] do { body; [iteration;] } while (termination); • 先执行循环体,后计算终止条件,若结果为true,则继续执行循环体。 • 循环体,至少执行一次。

More Related