第02章Java語言基礎(chǔ)用教案_第1頁
第02章Java語言基礎(chǔ)用教案_第2頁
第02章Java語言基礎(chǔ)用教案_第3頁
第02章Java語言基礎(chǔ)用教案_第4頁
第02章Java語言基礎(chǔ)用教案_第5頁
已閱讀5頁,還剩71頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、以往問題總結(jié)以往問題總結(jié)以往知識總結(jié)以往知識總結(jié)2.1 語言成分語言成分1.保留字與標識符保留字與標識符 基本數(shù)據(jù)類型基本數(shù)據(jù)類型 變量與常量變量與常量 運算符與表達式運算符與表達式2.1.1 保留字保留字(reserved word)具有專門的意義和用途,不能當作一般的標識符使用,這些標具有專門的意義和用途,不能當作一般的標識符使用,這些標識符稱為保留字,識符稱為保留字, Java-Defined Namesabstract break byte boolean catch case class char continue default doubl

2、e 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保留字在編輯器中以單獨顏色顯示W(wǎng)hy:WhatHow2.1.1 標 識

3、符 (identifier)1.程序員對程序中的各個元素(類名變量名方程序員對程序中的各個元素(類名變量名方法名)加以命名時使用的命名記號稱為標識法名)加以命名時使用的命名記號稱為標識符。符。 Programmer-Defined Names2.Java語言中,標識符是以字母,下劃線語言中,標識符是以字母,下劃線(_),美元符美元符($)開始的一個字符序列,后開始的一個字符序列,后面可以跟字母,下劃線,美元符,數(shù)字。面可以跟字母,下劃線,美元符,數(shù)字。A$(一美元)一美元)Why:WhatHow2.1.1 標識符標識符沒有長度限制。沒有長度限制。標識符對大小寫敏感,如標識符對大小寫敏感,如mf

4、h與與Mfh是不同的標是不同的標識符識符。不能使用保留字。以上為語法不能使用保留字。以上為語法(Syntax, compiler catches )習(xí)慣習(xí)慣(Style, widely adopted ) :保留字、變量名、:保留字、變量名、對象名、方法名、包名,全部字母對象名、方法名、包名,全部字母小寫小寫;如果名;如果名字由多個單詞構(gòu)成,則首字母小寫,其后的字由多個單詞構(gòu)成,則首字母小寫,其后的單詞單詞的的首字母大寫首字母大寫,如,如toString;類類名首字母名首字母大寫大寫;常量常量名全部字母名全部字母均大寫均大寫 必須掌握必須掌握2.1.1 標識符標識符(舉例舉例)1.合法的標識符

5、合法的標識符identifier,userName, _FH User_Name ,_sys_val, $change2. 非法的標識符2mail /數(shù)字打頭 , room#/非法字符 ,class /保留字, ab-cd/減號,ac eh /有空格2.1.1 分隔符分隔符(seperator)1.用于分開兩個不同的語法成分。不同的語法用于分開兩個不同的語法成分。不同的語法成分是用不同的分隔符。成分是用不同的分隔符。2.保留字、標識符保留字、標識符之間的分隔符是之間的分隔符是空格空格;語句語句之間的分隔符是之間的分隔符是英文分號(英文分號( ; ); 數(shù)據(jù)數(shù)據(jù)的的分隔號是分隔號是英文逗號(英文

6、逗號( , )。 Why:What:How:2.1.1 注釋注釋(Comments)1.Comments are notes in plain English inserted in the source code.2.Comments are used to:document the programs purpose, author, revision history, copyright notices, etc.describe fields, constructors, and methodsexplain obscure or unusual places in the codete

7、mporarily “comment out” fragments of codeWhy:What:How:2.1.1 注釋注釋(Comments)1.寫給人看的,機器不把它當作程序的一部分進行寫給人看的,機器不把它當作程序的一部分進行編譯處理。編譯處理。2.三種三種/ 單行注釋,單行注釋,single line/* */ 多行注釋,多行注釋,block/* 文檔注釋,用于從源代碼自動生成文文檔注釋,用于從源代碼自動生成文* 檔,執(zhí)行檔,執(zhí)行javadoc命令,可根據(jù)源代碼命令,可根據(jù)源代碼* 中的內(nèi)容生成中的內(nèi)容生成Web網(wǎng)頁網(wǎng)頁(HTML format) 。 */ Why:What:How

8、:2.1.1 注釋注釋(Comments)3. 不同不同格式的注釋可以嵌套。如我們格式的注釋可以嵌套。如我們經(jīng)常采用注釋的方式是一段代碼暫時經(jīng)常采用注釋的方式是一段代碼暫時不被執(zhí)行,如果這段代碼中原先有注不被執(zhí)行,如果這段代碼中原先有注釋,則新注釋與原有的注釋不能是相釋,則新注釋與原有的注釋不能是相同的形式。同的形式。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 kilograms2.1

9、.1 書寫風(fēng)格書寫風(fēng)格 Style1.Arrange code on separate lines; insert blank lines between fragments of code.2.Use comments.3.Indent blocks within braces.一定從開始注意。一定從開始注意。Why:What:How:Style (contd)public boolean moveDown()if (cubeY6*cubeX) cubeY+=yStep; return true;else return false;public boolean moveDown() if (

10、cubeY 6 * cubeX) cubeY += yStep; return true; else return false; Before:After:Compiles fine!Style (contd)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 readabilityAdd spaces around ope

11、rators and after semicolonsBlocks, Indentation1.Java code consists mainly of declarations and control statements.2.Declarations describe objects and methods.3.Control statement describe actions.4.Declarations and control statements end with a semicolon.5.No semicolon is used after a closing brace (e

12、xcept certain array declarations).1.Braces divide code into nested blocks.2.A block in braces indicates a number of statements that form one compound statement.3.Statements inside a block are indented, usually by two spaces or one tab.Blocks, Indentation (contd)Blocks, Indentation (contd) public voi

13、d 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 doubleWhy:What:How: 自動類型轉(zhuǎn)換規(guī)則操作數(shù)1類型操作數(shù)2類型 轉(zhuǎn)換后的類型byte、short、charintintbyte、short、char、intlonglongbyte、short、char、int、longfloatfloatbyte、short、char、int、long、floa

14、tdoubledouble強制類型轉(zhuǎn)換1.高級數(shù)據(jù)要轉(zhuǎn)換成低級數(shù)據(jù),需用到強制類型轉(zhuǎn)換,如:int i;byte b=(byte)i; /把int型變量i強制轉(zhuǎn)換為byte型Why:What:How:2.1.3 變量與常量變量與常量1.變量變量變量聲明變量聲明修飾符 類型 變量 = 表達式 , 變量= 表達式 例如:int i, j; 變量參與的運算和操作變量參與的運算和操作2.常量常量直接常量和符號常量直接常量和符號常量常量聲明常量聲明final int MAX=10;Why:What:How:Variables1.A variable is a “named container” that

15、 holds a value.2.q = 100 - q;means:1. Read the current value of q2. Subtract it from 1003. Move the result back into qcount5mov ax,qmov bx,100sub bx,axmov q,bx Variables (contd)1.Each variable has a scope the area in the source code where it is “visible.”2.If you use a variable outside its scope, th

16、e compiler reports a syntax error.3.Variables can have the same name. Caution: use only when their scopes do not intersect. int k; . int k; .Fields vs. Local Variables 1.Fields are declared outside all constructors and methods.2.Local variables are declared inside a constructor or a method (block).變

17、量與局部變量的概念必須掌握變量與局部變量的概念必須掌握Fields vs. Local Variables (contd) 1.Fields are usually grouped together, either at the top or at the bottom of the class.2.The scope of a field is the whole class.Fieldspublic class SomeClassFieldsConstructors and methodspublic class SomeClassScopeScopeFieldsConstructors

18、and methodsLocal Variables1.Local variables are declared inside a constructor or a method.2.Local variables lose their values and are destroyed once the constructor or the method is exited.3.The scope of a local variable is from its declaration down to the closing brace of the block in which it is d

19、eclared.Local Variables (contd)public class SomeClass . public SomeType SomeMethod (.) .Local variable declaredLocal variableScopeVariables (contd)1.Use local variables whenever appropriate; never use fields where local variables should be used.2.Give prominent names to fields, so that they are DIFF

20、ERENT from local variables.3.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 (contd)1.Common mistakes:public void SomeMethod (.) int x; . int x = 5; / should be: x = 5; .Varia

21、ble declared twice syntax errorVariables (contd)1.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變量的生命周期與作用域變量的生命周期與作用域1.變量要先定義,后使用,但也不是在變量定義后的語句一直都能使用變量

22、要先定義,后使用,但也不是在變量定義后的語句一直都能使用前面定義的變量。我們可以用大括號將多個語句包起來形成一個復(fù)合前面定義的變量。我們可以用大括號將多個語句包起來形成一個復(fù)合語句,變量只能在定義它的復(fù)合語句中使用。語句,變量只能在定義它的復(fù)合語句中使用。2.public class TestScope3. public static void main(String args) 4. int x = 12; 5. 6. int q = 96; / x和和q都可用都可用7. int x = 3;/錯誤的定義,錯誤的定義,Java中不允許有這種嵌套定義中不允許有這種嵌套定義8. System.o

23、ut.println(x is +x); 9. System.out.println(q is +q);10. 11. q = x; /* 錯誤的行,只有錯誤的行,只有x可用,可用, q 超出了作用域范圍超出了作用域范圍 */12. System.out.println(x is +x); 13. 14.Why:What:How:2.1.4 運算符與表達式運算符與表達式1.運算符運算符算術(shù)運算符:算術(shù)運算符:+(正)、(正)、-(負)、(負)、+、-、+(加)、(加)、-(減)、(減)、*、/(除)、(除)、%(取余)(取余) 【例2.1】 取余運算。關(guān)系運算符:關(guān)系運算符:=、!=、=、=

24、邏輯運算符:邏輯運算符:&(與)、(與)、|(或)、(或)、!(非)、(非)、(異(異或)、或)、&(條件與)、(條件與)、|(條件或)(條件或) 位運算符:位運算符:(非)、(非)、&(與)、(與)、|(或)、(或)、(異(異或)、或)、(右移位)、(右移位)、(無(無符號右移位)符號右移位) 賦值運算符:賦值運算符:= 、+=等等 1)算術(shù)運算符)算術(shù)運算符 +, , *, /, % !整數(shù)變量的運算結(jié)果至少是整數(shù)變量的運算結(jié)果至少是int型。型。 !如果有一個整數(shù)變量是如果有一個整數(shù)變量是longlong型,那么運算型,那么運算結(jié)果是結(jié)果是longlong型。型。

25、 byte b1=1; byte b1=1; byte b2=2; byte b2=2; byte b3=b1+b2;/errorbyte b3=b1+b2;/error byte b4=3+2; byte b4=3+2; byte b5=2+b1;/errorbyte b5=2+b1;/errorWhy: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)賦值運算符)賦值運算符=及其擴展賦及

26、其擴展賦 值運算符值運算符+=,=,*=,/= 1. =左邊的變量只是一個代號。 2. 賦值語句像函數(shù)一樣有返回值,它的返 回值即左值,如x=2+5; 的返回值是7, 所以可以把賦值語句當作表達式賦給一 個變量y,即 y=(x=2+5); sum+=i;即sum=sum+i; Why:WhatHow 賦值運算符遵循自右向左的結(jié)合性賦值運算符遵循自右向左的結(jié)合性 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

27、 ); m=4;n=2; m+=m*=n-=m*n; System.out.println(m = +m ) ; public static void main(String args) AssignOperator.ass(); 1.注釋:注釋:2.a=b=c=5相當于相當于a=(b=(c=5),結(jié)果,結(jié)果a,b,c都為都為5。而。而3.m=4;n=2;4.m+=m*=n-=m*n;的執(zhí)行步驟:的執(zhí)行步驟:5. Step1: m=4 n=26. Step2: n=2-4*2=-6/n=n-87. Step3: m=m*n=4*(-6)=-248. Step4: m=m+m=4-24=-20。

28、 m+=m*=n-=m*n; 4 4 2 4 2m=m+(m=m*(n=n-m*n) -6 -24 3)自增自減運算符自增自減運算符+,1.+ i 和i+它們都為i=i+1,但含義有所不同。2.+ i在使用i參與其相關(guān)運算之前,先使i加13.i+先使用i參與其相關(guān)運算,在下一輪時使i加1Why:WhatHowpublic 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

29、.println(j= +j+,+i= +i );public static void main(String args) AssignOperator.ass(); 輸出:j=0, i=0j=-2, i=-1j*=i-=(i+) 2 3 3 j = j * (i = i - i); 0整個程序執(zhí)行完后再整個程序執(zhí)行完后再i+1,for 循環(huán)就是執(zhí)行完整個程序循環(huán)就是執(zhí)行完整個程序i+14) 邏輯運算符邏輯運算符注注1:邏輯運算符用于對:邏輯運算符用于對boolean型結(jié)果的表達式進行運算,運算的結(jié)果都是型結(jié)果的表達式進行運算,運算的結(jié)果都是boolean型型 。注注2:“&”和和“&

30、amp;”的區(qū)別在于,如果使用前者連接,那么無論任何情況,的區(qū)別在于,如果使用前者連接,那么無論任何情況,“&”兩邊的表達式都會參與計算。如果使用后者連接,當兩邊的表達式都會參與計算。如果使用后者連接,當“&”的左邊為的左邊為false,則將不會計算其右邊的表達式。,則將不會計算其右邊的表達式?!皘”和和“|”的區(qū)別與的區(qū)別與“&”和和“&”的的區(qū)別一樣。區(qū)別一樣。 Why:What:How: 5)關(guān)系運算符關(guān)系運算符(結(jié)果是結(jié)果是boolean)小于ab,2大于ab,23=小于等于x=大于等于a=b= =等于a=b!=不等于a!=binstance of實例為

31、1.Public class CompareNumber 2. public static void main(Stringargs) 3. int n=3;4. int m=4;5. System.out.println(“nm is”+(nm is”+(+n)m);8. System.out.println(“n is”+n);9. 10. 運行結(jié)果: nm is true n is 5Public class ShortCircuit public static void main(Stringargs) int n=3; int m=4; System.out.println(“com

32、pare result is”+(nm)&(+n)m); System.out.println(“n is”+n); System.out.println (“compare result is”+(nm);/ | System.out.println(“n is”+n); 1.運行結(jié)果:pare result is false3.n is pare result is false5.n is 46.程序首先運行(nm)&(+n)m),因為nm為false,整個表達式為false,程序不再計算 (+n)m,所以n仍然為3,沒有機會再增加1了。7.程序接著計算下面的表達式:(nm

33、),因為nm的值,無論結(jié)果怎樣,n的值加1了??梢钥闯?,短路非常合理。Relational Operators (contd)1.Apply to numbers or chars:if ( x (右移),(無符號右移)2.左移x位就是乘2x。右移x位相當于除以2x。如:2564結(jié)果為256/24=16。對于正數(shù),和運算的結(jié)果相同,3.0001 10102 1111 00102 1111 0010 2 4.0000 0110 1111 1100 0011 11005.對于負數(shù),二進制數(shù)的高位為1,和的運算結(jié)果不相同,在移位時會復(fù)制符號位,(高位填1),如第二例,因此,負數(shù)在運算后仍然為負數(shù)。負

34、數(shù)在進行運算后,符號位右移,高位填零(因此叫無符號右移),負數(shù)在進行運算后,為正數(shù)。 7)條件運算符:)條件運算符: ?:布爾表達式1?表達式2:表達式3;執(zhí)行順序:先求解表達式1,為真,求取表達式2的值作為最終結(jié)果;為假,求取表達式3的值為最終結(jié)果。求值:(ab)? a: (cd)? c: d;假定a=5,b=8,c=1,d=9自右向左,先計算(cd)? c: d ,因為19為false,故取d=9為最終結(jié)果。以此類推。 (ad)? c: d ): a;(作業(yè)) 條件運算符實際中應(yīng)用較多,要熟練掌握Why:What:How: 8 8)其它:包括分量運算符)其它:包括分量運算符 下標運算符 ,

35、 實例運算符instanceof, 內(nèi)存分配運算符new, 強制類型轉(zhuǎn)換運算符 (類型), 方法調(diào)用運算符 () 等。2.1.4 運算符與表達式運算符與表達式強制類型轉(zhuǎn)換符強制類型轉(zhuǎn)換符(數(shù)據(jù)類型) 表達式條件運算符條件運算符表達式1 ? 表達式2 : 表達式3括號運算符括號運算符:()、字符串連接運算符:字符串連接運算符:+(max = +max);點運算符:點運算符:.對象運算符:對象運算符:instanceof 逗號運算符:逗號運算符:,2.1.4 運算符與表達式運算符與表達式運算符的優(yōu)先級運算符的優(yōu)先級運算符的結(jié)合性運算符的結(jié)合性單目運算符的結(jié)合性單目運算符的結(jié)合性雙目運算符的結(jié)合性雙目運算符的結(jié)合性表達式表達式表達式的運算規(guī)則表達式的運算規(guī)則表達式的數(shù)據(jù)類型表達式的數(shù)據(jù)類型書寫表達式時的注意事項書寫表達式時

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論