




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、2/44contents (chapter 6 in textbook)udata types(數(shù)據(jù)類型)uoperators(運(yùn)算符)uexpressions(表達(dá)式)uarrays(數(shù)組)u包裝器類u字符串類u名字含義u控制流語(yǔ)句3/44data in javaujava is a “typed” language:every data value must have a typethe type gives rules for what values are allowed, and what operations can be done with the values.ufor the
2、 computer, values of a certain type usually take a known amount of storage space in memory4/44data in javauprimitive types(基本數(shù)據(jù)類型)a primitive type represents a single value, which cannot be decomposed.ureference types(引用數(shù)據(jù)類型)the reference types are class types, interface types and array types.variab
3、les of these types can refer to objects of the corresponding type.5/44primitive data typeujava has a number of pre-defined primitive data types. booleanboolean (logical) values, either true or falsecharsingle characterbyte8-bit integers (signed) short16-bit integers (signed) int32-bit integers (sign
4、ed) (e.g. 3)long64-bit integers (signed) float32-bit floating point double64-bit floating point (e.g. 3.0)6/44the boolean typeua boolean variable is one which can have only 2 possible values: true or false. uboolean variables are used when you need to do logical testsexample: “is x greater than 3?”
5、x 37/44type intua variable of type int may take values from2147483648 to 2147483647.exceeding the range of legal values results in overflow, a run-time error.ubase of the number:octal(8進(jìn)制) a leading 0(zero)hexadecimal(16進(jìn)制) a leading 0 x or 0xdecimal(10進(jìn)制) 8/44type long(長(zhǎng)整數(shù))(長(zhǎng)整數(shù))ua variable of type
6、long may take values from9223372036854775808l to 9223372036854775807l.uto indicate the difference between an int constant and a long constant, the character l/l is appended.examplel2 is of type intl2l is of type long9/44type float(長(zhǎng)整數(shù))(長(zhǎng)整數(shù))ufloat :4個(gè)字節(jié),個(gè)字節(jié),32位位 從1.40129846432481707e-45到 3.4028234663
7、8528860e+38 (正值或負(fù)值的絕對(duì)值)數(shù)字后必須用數(shù)字后必須用f指明,否則屬于指明,否則屬于double型。型。10/44type double (literals,直接量)utype double represents “real” numbers approximately from -1.7 10308 to 1.7 10308 with 15 accurate significant digits(有效數(shù)字).uformat of large or small values:12345600000000000.0 = 0.123456 1017 is 0.123456e170.
8、00000123456 = 0.123456 10-5 is 0.123456e-5uif the value of e is more negative than about -308, the result is underflow, and the value will be replaced by zero. this happens quietly, and is not a run-time error.11/44type char(字符)(字符)ucharacters are individual symbols, enclosed in single quotes(單括號(hào))ue
9、xamples:letters of the alphabet (upper and lower case are distinct) a, apunctuation symbol (e.g. comma, period, question mark)single blank spaceparentheses (,); brackets ,; braces ,single digit (0, 1, 9)special characters such as , $, *, and so on.12/44unicodeu字母字母:unicode字符集字符集中的任意一個(gè)都是字母。例:中的任意一個(gè)都是
10、字母。例: 版權(quán) “ 雙引號(hào) 分式1/2 大寫(xiě)希臘字母delta 斜杠穿過(guò)字母o的符號(hào)13/44special characters(轉(zhuǎn)義字符)轉(zhuǎn)義字符)usome characters are treated specially because they cannot be typed easily, or have other interpretations in java.new-line character ntab character tsingle quote character double quote character backslash character uthe bac
11、kslash is used as an escape character(轉(zhuǎn)義符): it signifies that the next character is not to have its “usual” meaning.14/44轉(zhuǎn)義字符:轉(zhuǎn)義字符:b (退格退格)、f (換頁(yè)換頁(yè))、n (回車回車)、r (換行換行)、t (水平制表符水平制表符(到下一個(gè)到下一個(gè)tab位置位置)、 (單引號(hào)單引號(hào))、“ (雙引號(hào)雙引號(hào)) (反斜杠反斜杠)15/44variables(變量)uto store a value, we use a variable a name referring t
12、o the location in which the value is stored.fieldslocal variables (局部變量)lcan be declared anywhere within a block of statements;lno default initialization value.parameter variables(傳遞參數(shù)變量)lare declared in methods, constructors, or catch blocks;lno explicit(清楚的) initializers.ufor local variables & par
13、ameter variables, the only modifier is final.16/44variablesuvariables must be declared before they can be used.ua variable declaration has three parts:the type of the variablethe name of the variable(optional) assign an initial value to the variable.uexample: int x = 3;17/44assigning values to varia
14、blesuthe equals sign = is used to represent assigning a value to a variable.ugeneral form: = ;uexample:assume x and y are declared previouslyx = 3 + y;18/44u為了修改方便,在程序中常把不允許修改的量用一個(gè)標(biāo)識(shí)符為了修改方便,在程序中常把不允許修改的量用一個(gè)標(biāo)識(shí)符表示,該標(biāo)識(shí)符稱為符號(hào)表示,該標(biāo)識(shí)符稱為符號(hào)常量常量,又叫常量符號(hào)。,又叫常量符號(hào)。ujava中一般只將類的成員定義為常量,稱為中一般只將類的成員定義為常量,稱為類常量類常量,而不,
15、而不在方法中定義常量,定義符號(hào)常量的同時(shí)必須賦值。具體格在方法中定義常量,定義符號(hào)常量的同時(shí)必須賦值。具體格式為:式為:final =u常量名一般大寫(xiě)。如:常量名一般大寫(xiě)。如:final double pi=3.14;final int n=100;/數(shù)組長(zhǎng)度。數(shù)組長(zhǎng)度。19/44operators(操作符)uarithmetic(算術(shù)) operations+addition-subtraction or negation*multiplication/division%remainder20/44general operatorsuincrement & decrement operato
16、rs自增自增/自減運(yùn)算符自減運(yùn)算符-urelational & equality operators關(guān)系運(yùn)算符關(guān)系運(yùn)算符greater than=greater than or equal toless than 帶符號(hào)右移6)6) 不帶符號(hào)右移25/44練習(xí)練習(xí): (思路)思路)1.輸出一個(gè)整數(shù)的二進(jìn)制表示形式的字符串輸出一個(gè)整數(shù)的二進(jìn)制表示形式的字符串 integer.tobinarystring(x);2.用用實(shí)現(xiàn)對(duì)一句話的加解密實(shí)現(xiàn)對(duì)一句話的加解密26class secret public static void main(string args) char a=金金,木木,水水,火火
17、,土土; char secret=z; for(int i=0;ia.length;i+) ai=(char)(aisecret); system.out.println(密文密文:n); for(int i=0;ia.length;i+) system.out.print( +ai); for(int i=0;ia.length;i+) ai=(char)(aisecret); system.out.println(n原文原文:n); for(int i=0;i3?a+5:a-5;30/44字符串連接符 ustring concatenation(連接) operatoryou can us
18、e + to concatenate (連接) two strings.uexample:string + string (字符串 + 字符串)string boo=“boo”;string cry=boo+“hoo”;string + other types(字符串 + 其它類型)int no=1001;string stuno=“cuit”+no;31string concatenation(連接) operator strings can be concatenated (joined) using the + operator:my name is + alan givesmy nam
19、e isalan string values can also be concatenated to values of other types with the + operator.the speed is + 15.5 gives the speed is 15.5because one of the values for the + operator is a string , the double is temporarily converted to a string value 15.5 before doing the concatenation.32/44general op
20、eratorsu內(nèi)存分配運(yùn)算符內(nèi)存分配運(yùn)算符 newuinstance creation expression;u為變量或?qū)ο笠约皵?shù)組動(dòng)態(tài)分配內(nèi)存單元。為變量或?qū)ο笠约皵?shù)組動(dòng)態(tài)分配內(nèi)存單元。33/44precedence(優(yōu)先) of operatorsuoperators are evaluated left-to-right, with the following precedence (all operators on the same line are treated equally): (detail see page 177)() (expression) (array subsc
21、ript) . (object member) + (to indicate positive or negative values) ! (not)* / %+ - (for addition or subtraction of two values, concatenation) = = !=&|= (assignment to variable) 34/44expressions 表達(dá)式表達(dá)式uan expression consists of operators and their operands(操作數(shù)), which are evaluated to yield(產(chǎn)生) a re
22、sult;uthe result may be a variable or a value, or even void;uexample:x + y + z35/44expressionsuoperands to operators will be evaluated left-to-right.uexcept for the operators &, |, and ?:, every operand of an operator will be evaluated before the operation is performed;uif evaluation of the left ope
23、rand of a binary operator causes an exception, no part of the right-hand operand is evaluated.uthe evaluation stops as soon as an exception is encountered.36/44expression typeuevery expression has a data type. type decision rules are as follows:1.if an arithmetic or bit manipulation operator is appl
24、ied to integer values, the result of the expression is of type int unless one or both sides are long, in which case the result is long. 2.the smaller byte and short integer are always promoted to int before evaluation.3.if either operand of an arithmetic operator is floating point, the operation is
25、performed in floating-point arithmetic;37/44expression type4.a + operator is a string concatenation when either operand to + is of type string or if the left-hand side of a += is a string;5.when used in an expression, a char value is converted to an int by setting the top 16 bits to zero.uexample:un
26、icode character uffff integer 0 x0000ffff38/44implicit type conversions(隱式類型轉(zhuǎn)換)u原則:系統(tǒng)自動(dòng)進(jìn)行,在計(jì)算機(jī)中占內(nèi)存位(bit)數(shù)少的類型向占位數(shù)多的類型轉(zhuǎn)換。uwidening conversion(擴(kuò)展轉(zhuǎn)換) any numeric value can be assigned to any numeric variable whose type supports a larger range of values;byteshortintlongfloatdoublechar39/44explicit type casts(顯式類型轉(zhuǎn)換)u強(qiáng)制類型轉(zhuǎn)換強(qiáng)制類型轉(zhuǎn)換(cast)指從計(jì)算機(jī)中占內(nèi)存位(bit)數(shù)多的類型向占位數(shù)少的類型轉(zhuǎn)換??赡茉斐筛呶粩?shù)據(jù)的丟失。凡是數(shù)據(jù)類型高的轉(zhuǎn)換為數(shù)據(jù)類型低的都要進(jìn)行強(qiáng)制類型轉(zhuǎn)換,轉(zhuǎn)換方法如下:() 40/44explicit type casts(顯式類型轉(zhuǎn)換)uexample: double d=7.99;l
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國(guó)激光項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)口腔種植耗材項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)APE項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)仁用杏深加工項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)金銀花種植項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)計(jì)算機(jī)輔助制造(CAM)軟件項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)光聲成像系統(tǒng)項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 中國(guó)內(nèi)容分發(fā)網(wǎng)絡(luò)項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 數(shù)據(jù)驅(qū)動(dòng)的資源分析與預(yù)測(cè)-洞察闡釋
- 安全教育應(yīng)聘試題及答案
- 2025屆浙江省杭州市建蘭中學(xué)八年級(jí)英語(yǔ)第二學(xué)期期末綜合測(cè)試試題含答案
- 小麥?zhǔn)崭詈贤瑓f(xié)議書(shū)模板
- 商鋪退押金協(xié)議書(shū)
- 廣東省惠州市惠陽(yáng)區(qū)2023-2024學(xué)年一年級(jí)下學(xué)期語(yǔ)文期末隨堂練習(xí)試卷(含答案)
- 冷鏈物流配送中心建設(shè)項(xiàng)目可行性研究報(bào)告
- 起重吊裝作業(yè)安全與操作規(guī)范培訓(xùn)
- 2025遼寧沈陽(yáng)副食集團(tuán)所屬企業(yè)招聘25人筆試參考題庫(kù)附帶答案詳解
- 2023電氣裝置安裝工程 旋轉(zhuǎn)電機(jī)施工及驗(yàn)收規(guī)范
- 第六單元 年、月、日 單元測(cè)試(含答案)2024-2025學(xué)年三年級(jí)下冊(cè)數(shù)學(xué)人教版
- 2025-2030中國(guó)工業(yè)鉸接式機(jī)器人行業(yè)市場(chǎng)發(fā)展趨勢(shì)與前景展望戰(zhàn)略研究報(bào)告
- 國(guó)家開(kāi)放大學(xué)2025年《機(jī)電控制工程基礎(chǔ)》形考任務(wù)1-4答案
評(píng)論
0/150
提交評(píng)論