02第2章基本數(shù)據(jù)類型和操作_第1頁
02第2章基本數(shù)據(jù)類型和操作_第2頁
02第2章基本數(shù)據(jù)類型和操作_第3頁
02第2章基本數(shù)據(jù)類型和操作_第4頁
02第2章基本數(shù)據(jù)類型和操作_第5頁
已閱讀5頁,還剩62頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1 1 1第 2 章 基本數(shù)據(jù)類型和操作第1章 java概述 第 2 章 基本數(shù)據(jù)類型和操作 第3章 控制語句 第5章 數(shù)組 第4章 方法 基礎的計算機知識 第第 i 部分必備部分必備2 2 2jdk、jre與jvm的作用與關系?jdk: java development kit java開發(fā)工具包,開發(fā)工具包,其中包含其中包含java編譯器編譯器(javac.exe)、java運行時環(huán)境運行時環(huán)境jre:java runtime environment java運行時環(huán)境運行時環(huán)境(java 虛擬機虛擬機)+支持類庫支持類庫jvm:java virtual machine 負責將負責將jav

2、a字節(jié)碼翻譯為本字節(jié)碼翻譯為本地機器可以執(zhí)行二進制機器碼地機器可以執(zhí)行二進制機器碼問題問題3 3 3問題問題jdk、jre與jvm的作用與關系?4 4 4學習目標學習目標編寫簡單的java程序 (2.2).使用標識符命名變量、常量、方法和類 (2.3).java數(shù)據(jù)類型使用java運算符書寫表達式 (2.7 2.10).使用 joptionpane 輸入對話框輸入 (2.14).熟悉java的文檔管理、編程風格和命名習慣 (2.18).5 5 5編寫一個簡單的程序計算的圓的面積.computearearun6 6 6跟蹤程序執(zhí)行過程public class computearea /* mai

3、n method */ public static void main(string args) double radius; double area; / assign a radius radius = 20; / compute area area = radius * radius * 3.14159; / display results system.out.println(the area for the circle of radius + radius + is + area); no valueradius為 radius分配內(nèi)存單元7 7 7跟蹤程序執(zhí)行過程public c

4、lass computearea /* main method */ public static void main(string args) double radius; double area; / assign a radius radius = 20; / compute area area = radius * radius * 3.14159; / display results system.out.println(the area for the circle of radius + radius + is + area); no valueradiusmemoryno val

5、uearea為 area分配內(nèi)存單元8 8 8跟蹤程序執(zhí)行過程public class computearea /* main method */ public static void main(string args) double radius; double area; / assign a radius radius = 20; / compute area area = radius * radius * 3.14159; / display results system.out.println(the area for the circle of radius + radius +

6、 is + area); 20radiusno valueareaassign 20 to radius9 9 9跟蹤程序執(zhí)行過程public class computearea /* main method */ public static void main(string args) double radius; double area; / assign a radius radius = 20; / compute area area = radius * radius * 3.14159; / display results system.out.println(the area f

7、or the circle of radius + radius + is + area); 20radiusmemory1256.636areacompute area and assign it to variable area101010跟蹤程序執(zhí)行過程public class computearea /* main method */ public static void main(string args) double radius; double area; / assign a radius radius = 20; / compute area area = radius *

8、radius * 3.14159; / display results system.out.println(the area for the circle of radius + radius + is + area); 20radiusmemory1256.636area控制臺輸出信息111111數(shù)據(jù)類型i)值類型(value type)、基本類型、原生類型 8種ii)引用類型(reference type)、類類型 很多 121212值類型類型名類型名所占內(nèi)存寬度所占內(nèi)存寬度取值范圍取值范圍說明說明byte1字節(jié)字節(jié)-128127short2字節(jié)字節(jié)-3276832767int4字節(jié)字節(jié)

9、-21億億21億億java中無中無unsigned關鍵字關鍵字long8字節(jié)字節(jié)float4字節(jié)字節(jié)double8字節(jié)字節(jié)char2字節(jié)字節(jié)065535unicode編碼,可以保存世界上編碼,可以保存世界上文字的常用符號,注意:文字的常用符號,注意:char和數(shù)字類型間是可以轉(zhuǎn)和數(shù)字類型間是可以轉(zhuǎn)換的。漢字范圍:換的。漢字范圍:1996840869boolean1字節(jié)字節(jié)true / false131313引用類型(reference type)java.lang.bytejava.lang.shortjava.lang.integer: int值類型的包裝類值類型的包裝類java.lang.

10、long:long值類型的包裝類值類型的包裝類java.lang.floatjava.lang.doublejava.lang.characterjava.lang.booleanjava.lang.string141414變量 值類型名 變量名 = 值; 引用類型名 變量名 = new 構造方法();變量名可以包含變量名可以包含中文嗎?中文嗎?151515標識符標識符是一個由字母、數(shù)字、下劃線(_)和美元符號($)構成的字符轉(zhuǎn). 標識符必須由字母、下劃線(_)或美元符號開始,不能用數(shù)字開頭。標識符不能是保留字(參見附錄a)。標識符不能是 true, false和null.標識符可以有任何長度

11、.161616變量/ compute the first arearadius = 1.0;area = radius * radius * 3.14159;system.out.println(the area is “ + area + for radius +radius);/ compute the second arearadius = 2.0;area = radius * radius * 3.14159;system.out.println(the area is “ + area + for radius +radius);171717變量說明int x; / declare

12、 x to be an / integer variable;double radius; / declare radius to / be a double variable;char a; / declare a to be a / character variable;變量名使用小寫字母,如果一個名字有多個詞組成,變量名使用小寫字母,如果一個名字有多個詞組成,每個詞的第一個字母大寫每個詞的第一個字母大寫181818賦值語句x = 1; / assign 1 to x;radius = 1.0; / assign 1.0 to radius;a = a; / assign a to a;1

13、91919在第一步中說明和初始化變量 int x = 1; double d = 1.4; float f = 1.4;is this statement correct?float f = 1.4f202020常量final datatype constantname = value; final double pi = 3.14159; final int size = 3;常量用大寫字母命名,例如常量用大寫字母命名,例如pi212121算術運算符+, -, *, /, and %5 / 2結果為整型2.5.0 / 2結果為double類型2.55 % 2 結果為 1 (求余數(shù)) satu

14、rday is the 6th day in a week a week has 7 days january has 31 days the 2nd day in a week is tuesday (6 + 31) % 7 is 2 january 1, 2005 是 saturday, 可推算 february 1, 2005 是 tuesday222222注意浮點型數(shù)不一定是精確存儲. 例如, system.out.println(1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);顯示 0.5000000000000001, 而不是 0.5, system.out.pr

15、intln(1.0 - 0.9);顯示 0.09999999999999998, 而不是 0.1. 整型數(shù)據(jù)是精確存儲. 232323數(shù)值直接量直接量是在程序中直接出現(xiàn)的常量值。直接量是在程序中直接出現(xiàn)的常量值。 int i = 34;整型直接量在整型直接量在 -231 (-2147483648) 到到 2311 (2147483647) 之間之間.long類型在其后加類型在其后加l.浮點型默認為浮點型默認為 double 類型類型. float類型在其后加類型在其后加f 或或f, double 類型加類型加 d or d. 例如:例如: float :100.2f or 100.2f, do

16、uble :100.2d or 100.2d.科學計數(shù)法可表示為:科學計數(shù)法可表示為: 1.23456e+2, 或者或者 1.23456e2, 等價于等價于 123.456, and 1.23456e-2 等價于等價于 0.0123456.242424算術表達式)94(9)(5(10543yxxxcbayx翻譯成(3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)252525簡捷賦值運算符operatorexample等價于+=i+=8i = i+8-=f-=8.0f = f-8.0*=i*=8i = i*8/=i/=8i = i/8%=i%=8i

17、= i%8262626增量和減量運算符operator name description+var前置增量運算符 表達式執(zhí)行前變量var先加1.var+后置增量運算符 表達式執(zhí)行后變量var加1. -var前置減量運算符 表達式執(zhí)行前變量var先減1. var-后置減量運算符 表達式執(zhí)行后變量var減1 int i = 10; int newnum = 10 * i+; int newnum = 10 * i; i = i + 1; same effect as int i = 10; int newnum = 10 * (+i); i = i + 1; int newnum = 10 * i;

18、 same effect as 272727賦值表達式和賦值語句以下表達式可作語句:variable op= expression; / op 可選 +, -, *, /, or %+variable;variable+;-variable;variable-;282828數(shù)值類型轉(zhuǎn)換考慮以下語句:byte i = 100;long k = i * 3 + 4;double d = i * 3.1 + k / 2;byte, short, int, long, float, double范圍增加范圍增加擴寬可自動轉(zhuǎn)擴寬可自動轉(zhuǎn)換換,縮窄需明確縮窄需明確指明指明292929類型轉(zhuǎn)換隱式轉(zhuǎn)換隱式轉(zhuǎn)

19、換 double d = 3; (類型拓寬類型拓寬)顯式轉(zhuǎn)換顯式轉(zhuǎn)換 int i = (int)3.0; (類型所在類型所在) int i = (int)3.9; (小數(shù)部分被截去小數(shù)部分被截去) what is wrong?int x = 5 / 2.0;303030字符型數(shù)據(jù)類型char letter = a; (ascii) char numchar = 4; (ascii)char letter = u0041; (unicode)char numchar = u0034; (unicode)4位十六進制組成位十六進制組成. 注意:字符型可作自加、自減運算 char ch = a; s

20、ystem.out.println(+ch);313131unicode 統(tǒng)一碼unicode 占2個字節(jié), 以 u開頭的4個16進制數(shù),范圍從u0000 到 uffff. 共65536個字符。unicode u03b1 u03b2 u03b3 for three greek letters323232特殊字符的轉(zhuǎn)義字符序列description escape sequence unicodebackspace bu0008tab tu0009換行換行 nu000a回車回車 ru000d斜杠斜杠 u005c單引號單引號 u0027雙引號雙引號 u0022333333字符型和數(shù)值型之間的轉(zhuǎn)換in

21、t i = a; / same as int i = (int)a;char c = 97; / same as char c = (char)97;343434比較運算符operator nameless thangreater than=greater than or equal to=equal to!=not equal to353535布爾運算符operator name!not&and|orexclusive or (異或)363636! 真值表 p !p true false false true example !(1 2) is true, because (1 2) is

22、false. !(1 0) is false, because (1 0) is true. 373737&真值表 p1 p2 p1 & p2 false false false false true false true false false true true true example (3 2) & (5 = 5) is true, because (3 2) and (5 = 5) are both true. (3 2) & (5 5) is false, because (5 5) is false. 383838| 真值表 p1 p2 p1 | p2 false false f

23、alse false true true true false true true true true example (2 3) | (5 5) is false, because (2 3) and (5 5) are both false. (3 2) | (5 5) is true, because (3 2) is true. 393939 真值表 p1 p2 p1 p2 false false false false true true true false true true true false example (2 3) (5 1) is true, because (2 3

24、) is false and (5 1) is true. (3 2) (5 1) is false, because both (3 2) and (5 1) are true. 404040舉例system.out.println(is + num + divisible by 2 and 3? + (num % 2 = 0) & (num % 3 = 0); system.out.println(is + num + divisible by 2 or 3? + (num % 2 = 0) | (num % 3 = 0); system.out.println(is + num + di

25、visible by 2 or 3, but not both? + (num % 2 = 0) (num % 3 = 0); 414141閏年?boolean isleapyear = (year % 4 = 0) & (year % 100 != 0) | (year % 400 = 0);年份年份:能被能被4整除整除,但是不能被但是不能被100整除整除;或者能被或者能被400整除整除424242& 和 | 運算符&: 條件與條件與(捷徑與捷徑與):如果第一個條如果第一個條件為假件為假,則不需要再計算下個條件則不需要再計算下個條件&: 無條件與無條件與(按位與按位與)|:條件或條件或(捷徑

26、或捷徑或):):如果第一個條件如果第一個條件為真為真,則不需要再計算下個條件則不需要再計算下個條件|: 無條件或無條件或(按位或按位或) if x is 1, what is x after this expression?(x 1) & (x+ x) & ( 1 x+) how about (1 = x) | (10 x+)?(1 = x) | (10 x+)?434343運算符優(yōu)先級表達式3 + 4 * 4 5 * (4 + 3) 1的結果? var+, var- +, - (unary plus and minus), +var,-var (type) casting ! (not) *

27、, /, % (multiplication, division, and remainder) +, - (binary addition and subtraction) , , = (comparison) =, !=; (equality) & (unconditional and) (exclusive or) | (unconditional or) & (conditional and) short-circuit and | (conditional or) short-circuit or =, +=, -=, *=, /=, %= (assignment operator)

28、高高低低444444運算符結合方向 除賦值運算符外,所有的雙目運算符都是左結合的。 a b + c d等價于 (a b) + c) d 賦值運算符右是結合的。 a = b += c = 5 等價于 a = (b += (c = 5)454545舉例根據(jù)運算符優(yōu)先級和結合方向,表達式 3 + 4 * 4 5 * (4 + 3) - 1 的求值過程如下: 3 + 4 * 4 5 * (4 + 3) - 1 3 + 4 * 4 5 * 7 1 3 + 16 5 * 7 1 3 + 16 35 1 19 35 1 19 34 false (1) inside parentheses first (2)

29、 multiplication (3) multiplication (4) addition (5) subtraction (6) greater than 464646運算對象的計算順序如果運算對象沒有修改變量值的副作用,運算對象的運算順序是無關緊要的。然而副作用確實存在。 int a = 0;int x= (a+)+ a;int a = 0;int x = +a + a;x=1x=2474747表達式計算的規(guī)則規(guī)則1:可能的情況下,從左向右依次計算所有的表達式;規(guī)則2:根據(jù)運算符的優(yōu)先級進行運算;規(guī)則3:對優(yōu)先級相同的相鄰運算符,根據(jù)結合方向進行運算。484848表達式計算的規(guī)則 根據(jù)

30、規(guī)則,表達式 3 + 4 * 4 5 * (4 + 3) - 1 的計算如下: 3 + 4 * 4 5 * (4 + 3) - 1 3 + 16 5 * (4 + 3) - 1 19 5 * (4 + 3) - 1 19 5 * 7 - 1 19 35 1 19 34 false (1) 4 * 4 is the first subexpression that can be evaluated from left. (2) 3 + 16 is evaluated now. (3) 4 + 3 is now the leftmost subexpression that should be e

31、valuated. (4) 5 * 7 is evaluated now. (5) 35 1 is evaluated now. (6) 19 34 is evaluated now. 494949string 類型string實際上java定義的一個類。 例如:string message = welcome to java;字符串可以用“+”鏈接起來/ three strings are concatenatedstring message = welcome + to + java; / string chapter is concatenated with number 2string

32、 s = chapter + 2; / s becomes chapter2 / string supplement is concatenated with character bstring s1 = supplement + b; / s becomes supplementb505050字符串轉(zhuǎn)為數(shù)值int intvalue = integer.parseint(intstring); intstring 是一個數(shù)值型字符串,例如 “123”. double doublevalue =double.parsedouble(doublestring); doublestring是一個數(shù)值

33、型字符串,例如 “123.45”. string s = string.valueof( value); 其中 value 為任意一種數(shù)字類型。 515151從輸入對話框獲取輸入string string = joptionpane.showinputdialog( null, “prompting message”, “dialog title”, joptionpane.question_message);525252兩種調(diào)用方法第一種: string string = joptionpane.showinputdialog(null, x, y, joptionpane.question

34、_message); 第一個參數(shù)總是null, x 表示顯示內(nèi)容的字符串, y 表示對話框的標題,第四個參數(shù)是對話框顯示的圖標.第二種:joptionpane.showmessagedialog(x); x 表示顯示內(nèi)容的字符串535353example 2.3 計算貸款支付額computeloanrun輸入年利率、年數(shù)和貸款總額顯示月支付額和總支付額12)1 (11numofyearserestratemonthlyinterestratemonthlyintloanamount545454example 2.4 整錢兌零輸入總錢數(shù),然后兌換成 dollars(1元), quarters(2

35、角5分), dimes(1角), nickels(5分), and pennies(1分). computechangerun555555程序跟蹤 int remainingamount = (int)(amount * 100); / find the number of one dollars int numberofonedollars = remainingamount / 100; remainingamount = remainingamount % 100; / find the number of quarters in the remaining amount int num

36、berofquarters = remainingamount / 25; remainingamount = remainingamount % 25; / find the number of dimes in the remaining amount int numberofdimes = remainingamount / 10; remainingamount = remainingamount % 10; / find the number of nickels in the remaining amount int numberofnickels = remainingamoun

37、t / 5; remainingamount = remainingamount % 5; / find the number of pennies in the remaining amount int numberofpennies = remainingamount;1156remainingamountremainingamount initializedsuppose amount is 11.56565656程序跟蹤 int remainingamount = (int)(amount * 100); / find the number of one dollars int num

38、berofonedollars = remainingamount / 100; remainingamount = remainingamount % 100; / find the number of quarters in the remaining amount int numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; / find the number of dimes in the remaining amount int numberofdimes = remainin

39、gamount / 10; remainingamount = remainingamount % 10; / find the number of nickels in the remaining amount int numberofnickels = remainingamount / 5; remainingamount = remainingamount % 5; / find the number of pennies in the remaining amount int numberofpennies = remainingamount;1156remainingamounts

40、uppose amount is 11.5611numberofonedollarsnumberofonedollars assigned575757程序跟蹤 int remainingamount = (int)(amount * 100); / find the number of one dollars int numberofonedollars = remainingamount / 100; remainingamount = remainingamount % 100; / find the number of quarters in the remaining amount i

41、nt numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; / find the number of dimes in the remaining amount int numberofdimes = remainingamount / 10; remainingamount = remainingamount % 10; / find the number of nickels in the remaining amount int numberofnickels = remainin

42、gamount / 5; remainingamount = remainingamount % 5; / find the number of pennies in the remaining amount int numberofpennies = remainingamount;56remainingamountsuppose amount is 11.5611numberofonedollarsremainingamount updated585858程序跟蹤 int remainingamount = (int)(amount * 100); / find the number of

43、 one dollars int numberofonedollars = remainingamount / 100; remainingamount = remainingamount % 100; / find the number of quarters in the remaining amount int numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; / find the number of dimes in the remaining amount int numb

44、erofdimes = remainingamount / 10; remainingamount = remainingamount % 10; / find the number of nickels in the remaining amount int numberofnickels = remainingamount / 5; remainingamount = remainingamount % 5; / find the number of pennies in the remaining amount int numberofpennies = remainingamount;

45、56remainingamountsuppose amount is 11.5611numberofonedollars2numberofonequartersnumberofonequarters assigned595959程序跟蹤 int remainingamount = (int)(amount * 100); / find the number of one dollars int numberofonedollars = remainingamount / 100; remainingamount = remainingamount % 100; / find the numbe

46、r of quarters in the remaining amount int numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; / find the number of dimes in the remaining amount int numberofdimes = remainingamount / 10; remainingamount = remainingamount % 10; / find the number of nickels in the remainin

47、g amount int numberofnickels = remainingamount / 5; remainingamount = remainingamount % 5; / find the number of pennies in the remaining amount int numberofpennies = remainingamount;6remainingamountsuppose amount is 11.5611numberofonedollars2numberofquartersremainingamount updated606060格式化輸出jdk 1.5featurespecifie

溫馨提示

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

評論

0/150

提交評論