第02章Java语言基础用教案.ppt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 02 Java 语言 基础 教案
- 资源描述:
-
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Java2,程序设计实用教程(第,2,版),*,第,2,章,Java,语言基础,以往问题总结,以往知识总结,2.1,语言成分,2.1.1,保留字与标识符,2.1.2,基本数据类型,2.1.3,变量与常量,2.1.4,运算符与表达式,2.1.1,保留字,(reserved word),具有专门的意义和用途,不能当作一般的标识符使用,这些标识符称为保留字,,Java-Defined Names,abstract break byte boolean catch case class char continue default double do else extends false final float for finallyif 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,保留字在编辑器中以单独颜色显示,Why:,What,How,2.1.1,标 识 符,(identifier),程序员对程序中的各个元素(类名变量名方法名)加以命名时使用的命名记号称为标识符。,Programmer-Defined Names,Java,语言中,标识符是以字母,下划线(,_,),美元符,($),开始的一个字符序列,后面可以跟字母,下划线,美元符,数字。,A,$(,一美元),Why:,What,How,2.1.1,标识符,没有长度限制。,标识符对大小写敏感,如,mfh,与,Mfh,是不同的标识符,。,不能使用保留字。以上为语法,(Syntax,compiler catches,),习惯,(,Style,widely adopted),:保留字、变量名、对象名、方法名、包名,全部字母,小写,;如果名字由多个单词构成,则首字母小写,其后的,单词,的,首字母大写,,如,toString,;,类,名首字母,大写,;,常量,名全部字母,均大写,必须掌握,2.1.1,标识符,(,举例,),合法的标识符,identifier,,,userName,,,_FH User_Name,,,_sys_val,,,$change,2.,非法的标识符,2mail/,数字打头,,room#/,非法字符,,class/,保留字,,ab-cd/,减号,,ac eh/,有空格,2.1.1,分隔符,(seperator),用于分开两个不同的语法成分。不同的语法成分是用不同的分隔符。,保留字、标识符,之间的分隔符是,空格,;,语句,之间的分隔符是,英文分号(,;,),;,数据,的分隔号是,英文逗号(,),。,Why:,What,:,How,:,2.1.1,注释,(Comments),Comments are notes in plain English inserted in the source code.,Comments are used to:,document the program,s,purpose,author,revision history,copyright notices,etc.,describe,fields,constructors,and,methods,explain obscure or unusual places in the code,temporarily,“,comment out,”,fragments of code,Why:,What,:,How,:,2.1.1,注释,(Comments),写给人看的,机器不把它当作程序的一部分进行编译处理。,三种,/,单行注释,,single line,/*,*/,多行注释,,block,/*,文档注释,用于从源代码自动生成文,*档,执行,javadoc,命令,可根据源代码,*中的内容生成,Web,网页,(,HTML format),。,*/,Why:,What,:,How,:,2.1.1,注释,(Comments),3.,不同,格式的注释可以嵌套。如我们经常采用注释的方式是一段代码暂时不被执行,如果这段代码中原先有注释,则新注释与原有的注释不能是相同的形式。,2.1.1,注释,(Comments),/*Exercise 5-2 for Java Methods,*Author:Miss Brace,*Date:3/5/2010,*Rev.1.0,*/,wt,*,=2.2046;,/,Convert to kilograms,2.1.1,书写风格,Style,Arrange code on separate lines;insert blank lines between fragments of code.,Use comments.,Indent blocks within braces.,一定从开始注意。,Why:,What,:,How,:,Style(cont,d),public boolean moveDown()if(cubeY6*cubeX)cubeY+=yStep;return true;else return false;,public boolean moveDown(),if(cubeY 6,*,cubeX),cubeY+=yStep;,return true;,else,return false;,Before:,After:,Compiles fine!,Style(cont,d),public void fill(char ch),int rows=grid.length,cols=grid0.length;,int r,c;,for(r=0;r rows;r+),for(c=0;c cols;c+),gridrc=ch;,Add blank lines for readability,Add spaces around operators and after semicolons,Blocks,Indentation,Java code consists mainly of declarations and control statements.,Declarations describe objects and methods.,Control statement describe actions.,Declarations and control statements end with a semicolon.,No semicolon is used after a closing brace(except certain array declarations).,Braces divide code into nested blocks.,A block in braces indicates a number of statements that form one,compound,statement.,Statements inside a block are indented,usually by two spaces or one tab.,Blocks,Indentation(cont,d),Blocks,Indentation(cont,d),public void fill(char ch),int rows=grid.length,cols=grid0.length;,int r,c;,for(r=0;r rows;r+),for(c=0;c,高,byte,short,char,int,long,float,double,Why:,What,:,How,:,自动类型转换规则,操作数,1,类型,操作数,2,类型,转换后的类型,byte,、,short,、,charintint,byte,、,short,、,char,、,intlonglong,byte,、,short,、,char,、,int,、,longfloatfloat,byte,、,short,、,char,、,int,、,long,、,floatdoubledouble,强制类型转换,高级数据要转换成低级数据,需用到强制类型转换,如:,int i;,byte b=(byte)i;,/,把,int,型变量,i,强制转换为,byte,型,Why:,What,:,How,:,2.1.3,变量与常量,变量,变量声明,修饰符,类型 变量,=,表达式,变量,=,表达式,例如:,int i,j;,变量参与的运算和操作,常量,直接常量和符号常量,常量声明,final int MAX=10;,Why:,What,:,How,:,Variables,A variable is a,“,named container,”,that holds a value.,q=100,-,q;,means:,1.Read the current value of,q,2.Subtract it from 100,3.Move the result back into,q,count,5,mov ax,q,mov bx,100,sub bx,ax,mov q,bx,Variables(cont,d),Each variable has a,scope,the area in the,source code,where it is,“,visible.,”,If you use a variable outside its scope,the compiler reports a syntax error.,Variables,can,have the same name.,Caution:,use only when their scopes do not intersect.,int k;,.,int k;,.,Fields vs.Local Variables,Fields are declared,outside,all constructors and methods.,Local variables are declared,inside,a constructor or a method,(block).,变量与局部变量的概念必须掌握,Fields vs.Local Variables(cont,d),Fields are usually grouped together,either at the top or at the bottom of the class.,The scope of a field is,the whole class.,Fields,public class,SomeClass,Fields,Constructors and methods,public class,SomeClass,Scope,Scope,Fields,Constructors and methods,Local Variables,Local variables are declared,inside,a constructor or a method.,Local variables lose their values and are destroyed once the constructor or the method is exited.,The scope of a local variable is from its declaration down to the closing brace of the block in which it is declared.,Local Variables(cont,d),public class,SomeClass,.,public,SomeType,SomeMethod,(.),.,Local variable,declared,Local variable,Scope,Variables(cont,d),Use,local,variables whenever appropriate;,never,use fields where local variables should be used.,Give,prominent,names to fields,so that they are DIFFERENT from local variables.,Use,the same,name for local variables that are used in similar ways in different methods(e.g.,x,y,for coordinates,count,for a counter,i,k,for indices,etc.).,Variables(cont,d),Common mistakes:,public void SomeMethod(.),int x;,.,int,x=5;/should be:x=5;,.,Variable declared twice syntax error,Variables(cont,d),Common mistakes:,private int cubeX;,.,public SomeClass(.)/constructor,int,cubeX=5;/should be:cubeX=5;,.,A field is overridden by a local variable;the value of the field,cubeX,remains unset,变量的生命周期与作用域,变量要先定义,后使用,但也不是在变量定义后的语句一直都能使用前面定义的变量。我们可以用大括号将多个语句包起来形成一个复合语句,变量只能在定义它的复合语句中使用。,public class TestScope,public static void main(String args),int x=12;,int q=96;/x,和,q,都可用,int x=3;/,错误的定义,,Java,中不允许有这种嵌套定义,System.out.println(x is+x);,System.out.println(q is+q);,q=x;/*,错误的行,只有,x,可用,,q,超出了作用域范围*,/,System.out.println(x is+x);,Why:,What,:,How,:,2.1.4,运算符与表达式,运算符,算术运算符:,+,(正)、,-,(负)、,+,、,-,、,+,(加)、,-,(减)、*、,/,(除)、,%,(取余),【,例,2.1】,取余运算。,关系运算符:,=,、,!=,、,、,=,、,=,逻辑运算符:,&,(与)、,|,(或)、,!,(非)、(异或)、,&,(条件与)、,|,(条件或),位运算符:,(非)、,&,(与)、,|,(或)、,(异或)、,(右移位)、,(无符号右移位),赋值运算符:,=,、,+=,等,1,)算术运算符,+,*,/,%,!,整数变量的运算结果至少是,int,型。,!,如果有一个整数变量是,long,型,那么运算结果是,long,型。,byte b1=1;,byte b2=2;,byte b3=b1+b2;/error,byte b4=3+2;,byte b5=2+b1;/error,Why:,What,:,How,:,举例,long m2=2L;,int i1=m2+b1;/error int i2=2L+3;/error,long m3=m2+b1;,float z3=2.0f+3.0;/error,double z4=2.0f+3.0;,2,)赋值运算符,=,及其扩展赋,值运算符,+=,,,=,,*,=,,,/=,1.=,左边的变量只是一个代号。,2.,赋值语句像函数一样有返回值,它的返,回值即左值,如,x=2+5;,的返回值是,7,,,所以可以把赋值语句当作表达式赋给一,个变量,y,,即,y=(x=2+5);,sum+=i;,即,sum=sum+i;,Why:,What,How,赋值运算符遵循自右向左的结合性,public,class,AssignOperator,public,static,void,ass(),int,a,b,c,m,n;,a=b=c=5;,System.,out,.println(a=+a+,+b=+b+,+c=+c);,m=4;n=2;,m+=m*=n-=m*n;,System.,out,.println(m=+m );,public,static,void,main(String args),AssignOperator.,ass,();,注释:,a=b=c=5,相当于,a=(b=(c=5),,结果,a,,,b,,,c,都为,5,。而,m=4;n=2;,m+=m*=n-=m*n;,的执行步骤:,Step1:m=4 n=2,Step2:n=2-4*2=-6/n=n-8,Step3:m=m*n=4*(-6)=-24,Step4:m=m+m=4-24=-20,。,m+=m*=n-=m*n;,4 4 2 4 2,m=m+(m=m*(n=n-m*n),-6,-24,3),自增自减运算符,+,,,+i,和,i+,它们都为,i=i+1,,但含义有所不同。,+i,在使用,i,参与其相关运算之前,先使,i,加,1,i+,先使用,i,参与其相关运算,在下一轮时使,i,加,1,Why:,What,How,public,class,AssignOperator,public,static,void,ass(),int,j=2,i=3;,j*=i-=(i+);,System.,out,.println(j=+j+,+i=+i);,j=2;i=3;,j*=i-=(+i);,System.,out,.println(j=+j+,+i=+i);,public,static,void,main(String args),AssignOperator.,ass,();,输出:,j=0,i=0,j=-2,i=-1,j*=i-=(i+),2 3 3,j=j*(i=i-i);,0,整个程序执行完后再,i+1,,,for,循环就是执行完整个程序,i+1,4),逻辑运算符,注,1,:逻辑运算符用于对,boolean,型结果的表达式进行运算,运算的结果都是,boolean,型。,注,2,:,“,&,”,和,“,&,”,的区别在于,如果使用前者连接,那么无论任何情况,,“,&,”,两边的表达式都会参与计算。如果使用后者连接,当,“,&,”,的左边为,false,,则将不会计算其右边的表达式。,“,|,”,和,“,|,”,的区别与,“,&,”,和,“,&,”,的区别一样。,Why:,What,:,How,:,5),关系运算符,(,结果是,boolean),小于,ab,2,大于,ab,23,=,小于等于,x=,大于等于,a=b,=,等于,a=b,!=,不等于,a!=b,instance of,实例为,Public class CompareNumber,public static void main(Stringargs),int n=3;,int m=4;,System.out.println(,“,nm is,”,+(nm is,”,+(+n)m);,System.out.println(,“,n is,”,+n);,运行结果:,nm is true,n is 5,Public class ShortCircuit,public static void main(Stringargs),int n=3;,int m=4;,System.out.println(,“,compare result is,”,+(nm),&(+n)m,);,System.out.println(,“,n is,”,+n);,System.out.println(,“,compare result is,”,+(nm,);/|,System.out.println(,“,n is,”,+n);,运行结果:,compare result is false,n is 3,compare result is false,n is 4,程序首先运行,(nm),&(+n)m),,因为,nm,为,false,,整个表达式为,false,,程序不再计算,(+n)m,,所以,n,仍然为,3,,没有机会再增加,1,了。,程序接着计算下面的表达式:,(nm),,因为,nm,的值,无论结果怎样,,n,的值加,1,了。可以看出,短路非常合理。,Relational Operators(cont,d),Apply to numbers or,char,s:,if (x,(右移,),,,(无符号右移),左移,x,位就是乘,2,x,。右移,x,位相当于除以,2,x,。如:,2564,结果为,256/2,4,=16,。对于正数,,和,运算的结果相同,,0001 10102,1111 00102,1111 0010 2,0000 0110,1111 1100 0011 1100,对于负数,二进制数的高位为,1,,,和,的运算结果不相同,,在移位时会复制符号位,(高位填,1,),如第二例,因此,负数在,运算后仍然为负数。负数在进行,运算后,符号位右移,高位填零(因此叫无符号右移),负数在进行,运算后,为正数。,7,)条件运算符:,?,:,布尔表达式,1,?表达式,2,:表达式,3,;执行顺序:先求解表达式,1,,为真,求取表达式,2,的值作为最终结果;为假,求取表达式,3,的值为最终结果。求值:,(ab)?a:,(cd)?c:d,;,假定,a=5,b=8,c=1,d=9,自右向左,先计算,(cd)?c:d,,因为,19,为,false,,故取,d=9,为最终结果。以此类推。,(ad)?c:d,):a,;(,作业,),条件运算符实际中应用较多,要熟练掌握,Why:,What,:,How,:,8,)其它:包括分量运算符,下标运算符,,实例运算符,instanceof,,内存分配运算符,new,,强制类型转换运算符,(,类型,),,方法调用运算符,(),等。,2.1.4,运算符与表达式,强制类型转换符,(,数据类型,),表达式,条件运算符,表达式,1?,表达式,2 :,表达式,3,括号运算符,:,(),、,字符串连接运算符:,+,System.out.println,(,max=+max);,点运算符:,.,对象运算符:,instanceof,逗号运算符:,2.1.4,运算符与表达式,运算符的优先级,运算符的结合性,单目运算符的结合性,双目运算符的结合性,表达式,表达式的运算规则,表达式的数据类型,书写表达式时的注意事项,2.1.4,运算符与表达式,类型兼容原则,类型相同,类型相容,赋值相容,【,例,2.2】,求一个三位数的数字和。,【,例,2.3】,求圆面积。,【,例,2.4】,判断一个年份是否为闰年。,17,名称标记,new,为对象分配空间,n/a,L,R,16,ai,下标,后缀,L,R,m(),方法的引用,后缀,L,R,m(),域选择符,后缀,L,R,+-,自增 自减,前缀,L,R,15,+-,自增 自减,后缀,L,R,14,按位取反,单目,L,R,!,逻辑非,单目,L,R,+,正号,单目,L,R,-,负号,单目,L,R,13,(,类型名,),强制类型转换,单目,L,R,12,*,/,乘 除,双目,L,R,%,求余,双目,L,R,11,-+,减法 加法,双目,L,R,10,左移 右移 零填充右移,双目,L,R,9,instance of =,关系运算符,双目,L,R,8,=,!,=,等于 不等于,双目,L,R,7,&,按位与,双目,L,R,6,按位异或,双目,L,R,5,|,按位或,双目,L,R,4,&,逻辑与,双目,L,R,3,逻辑或,双目,L,R,2,?:,条件运算符,三目,L,R,1,=*=/=-=%=,赋值运算符,双目,L,R,求以下表达式的值,:,76,|,40.2,按照关系运算优先于逻辑运算,,&,优先于,|,,先得出,40.2,为,true,逻辑与连接的表达式部分为,true,然后考虑逻辑或的结果为,true.,/,算术运算,整除与取余,public class Operator1,public static void main(String args),System.out.println(7/2=+(7/2);,System.out.println(7.0/2=+(7.0/2);,System.out.println(7%2=+(7%2);,System.out.println(7.0%2=+(7.0%2);,System.out.println(-7%2=+(-7%2);,System.out.println(7%-2=+(7%-2);,/*,程序运行结果如下:,7/2=3 /,整除,7.0/2=3.5 /,除法,7%2=1 /,余数为整数,7.0%2=1.0 /,余数为浮点数?这样定义有什么意义?实数哪里有余数的概念?,-7%2=-1 /,结果的符号与被除数相同,7%-2=1,*/,“,整除和取余,”,与,“,判断闰年,”,之间的程序,展开阅读全文

咨信网声明:本文档由用户上传并分享,仅供学习交流、研究之用,未经授权,严禁复制、发行、汇编、翻译或网络传播等,侵权必究。
咨信网小贴示:
1、浏览或下载可查看了解,请点击【咨信网告知】。
2、下载文件中如有侵权或不适当内容,将立即纠正。
3、如果因为网速或其他原因下载失败请重新下载,重复下载[60天内]不扣币。
4、开具发票登录电脑端申请,请点击【索取发票】。
咨信网客服组:【微信客服】
1、浏览或下载可查看了解,请点击【咨信网告知】。
2、下载文件中如有侵权或不适当内容,将立即纠正。
3、如果因为网速或其他原因下载失败请重新下载,重复下载[60天内]不扣币。
4、开具发票登录电脑端申请,请点击【索取发票】。
咨信网客服组:【微信客服】


第02章Java语言基础用教案.ppt
















自信AI助手














微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/14596241.html