IT計算機 第四單元_第1頁
IT計算機 第四單元_第2頁
IT計算機 第四單元_第3頁
IT計算機 第四單元_第4頁
IT計算機 第四單元_第5頁
已閱讀5頁,還剩31頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

SunEducationalService

第四罩元

宣告、初始化和使用燮數(shù)

FundamentalsoftheJavan,ProgrammingLanguage

SunEducationalService

軍元概

?罩元目檄

-阜雪建數(shù)的使用方法典定羲燮數(shù)的言吾法

-列出Java程式言吾言的八彳固基本型別

-根獴Java程式言吾言的指醇方金十和音吾法規(guī)即來宣告、初始

化及使用燮數(shù)典常數(shù)

-使用55算子(operator)來更改燮數(shù)值

-使用晉升(promotion)典型別醇換(typecasting)

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide2of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

擘曾燮數(shù)的使用和言吾法

翱列:

1publicclassShirt{

2

3publicintshirtID=0;//IS衫的ID

4publicStringdescription=''-descriptionrequired-";//

5

6//彥真色代礁卷R4工色,B=盛色,G/景色,U=未^定

7publiccharcolorCode='U';

8

9publicdoubleprice=0.0;//所有勰衫的fO殳^格

10

11publicintquantityInStock=0;//所有勰衫的JO殳數(shù)量

12

13//道方法印出項目的所有值

14publicvoiddisplayShirtlnformation(){

15

16System,out.printin(''ShirtID:''+shirtID);

17System,out.printin(''Shirtdescription:"+description);

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide3of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

18System,out.printin(''ColorCode:''+colorCode);

19System,out.printin(''Shirtprice:''+price);

20System,out.printin(''Quantityinstock:''+quantity工nStock);

21

22}//display方法的結(jié)尾

23}//類直別的結(jié)尾

FundamentalsoftheJavaProgrammingLanguageModule4,slide4of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

燮數(shù)的使用

?物件^醴中唯一的資料

?指派一彳固燮數(shù)值幺合其他燮數(shù)

?在數(shù)擘表示式中用來代表數(shù)值

?在簧幕中印出燮數(shù)值

?侑者存參考至其他物件

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide5of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

燮數(shù)的宣告典初始化

?言吾法(腐性或燮數(shù)):

[modifiers]typeidentifier=[value];

?^法(11域燮數(shù)):

typeidentifier;

?吉吾法(11域燮數(shù)):

typeidentifier=[value];

?蒯列:

publicintshirtID=0;

publicStringdescription="-descriptionrequired-";

publiccharcolorCode='Uz;

publicdoubleprice=0.0;

publicintquantitylnStock=0;

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide6of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

描述基本資料型別

?整數(shù)型別(byte>short>int和long)

?浮黑占數(shù)型別(float和double)

?字元型別(char)

?暹申苴型別(boolean)

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide7of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

整數(shù)型別

(K合法的字面字

型別是度如國(1itcralvalue)

靶例

byte8bits-2?到2--1(-128至U127,2

共256他可能的值)-114

short16bits-2”到26-1(-32,768到32,767,2

or65,535《固可能的值)-32699

int32bits-2”到2^-1(-2,147,483,648到2

2,147,483,647共147,334,778

4,294,967,296他可能的值)

long64bits-2。到26J-12

(-9,223,372,036854,775,808到-2,036,854,775,808L

9,223,372,036854,775,807,共IL

18,446,744,073,709,551,616

他可能的值)

FundamentalsoftheJava^ProgrammingLanguageModule4,slide8of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RevistonD

SunEducationalService

整數(shù)基本型別

publicintshirtID=0;//勰衫JIBS的ID

publicintquantityInStock=0;//所有勰衫的數(shù)量

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide9of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

浮黠數(shù)基本型別

型別浮黠艮度合法的字面字,literalvalue例

float32bits99F

-327,456,99.01F

4.2E6F(工程襟IB投

4.2*106)

double64bits-1111

2.1E12

999,701,327,456,2。。。

publicdoubleprice=0.0;//所有勰衫的^格

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide10of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

文字基本型別

?唯一的資料型別卷char

?用央侑者存16bits的罩一字元,如'y,字元

-輪例:

publiccharcolorCode='U';

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide11of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

暹it基本型別

?唯一^的資料型月ll^bool一an

?只能用來侑者存tru一或falsa

?用來信者存建算式的建算結(jié)果卷tru一或falsa

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide12of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

燮數(shù)命名

?命名規(guī)劃:

-燮數(shù)^別字必須以大嘉或小嘉字母、底^(_)或金菱字

符虢($)作卷^豆真字母。

-燮數(shù)言戢別字不要使用檄粘符虢、空白、破折虢或任何

Java程式言吾言的展融建字。

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide13of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

燮數(shù)命名

?命名型即:

-以小嘉字母卷每他燮數(shù)的^豆尾隨彳友的罩字就以大嘉

字母回如:myVariabl一。

-it攆可以落助言刖意,加者殳升可以1?使用者了解此燮數(shù)

使用目的的建數(shù)名。

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide14of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

指派一彳固數(shù)值

?鸛列:

price=12.99;

?IS例(boolean):

booleanisOpen=false;

FundamentalsoftheJavaProgrammingLanguageModule4,slide15of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

在一行程式中宣告典初始化燮數(shù)

?吉吾法:

typeidentifier=value[,identifier=value];

?翱列:

doubleprice=0.0,wholesalePrice=0.0;

FundamentalsoftheJavaProgrammingLanguageModule4,slide16of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

其他宣告典初始化燮數(shù)的方法

?指派字面字(literal)值:

intID=0;

floatpi=3.14F;

charmyChar='G';

booleanisOpen=false;

?指派一彳固建數(shù)值幺合另一彳固燮數(shù):

intID=0;

intsalelD=ID;

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide17of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

其他宣告典初始化燮數(shù)的方法

?揩算式的結(jié)果指派給整數(shù)、浮黠數(shù)或布林建數(shù)

floatnumberOrdered=908.5F;

floatcasePrice=19.99F;

floatprice=(casePrice*numberOrdered);

inthour=12;

booleanisOpen=(hour>8);

?指派方法呼叫的回停值幺合建數(shù)

FundamentalsoftheJavaProgrammingLanguageModule4,slide18of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

常數(shù)

?燮數(shù)(可更改):

doublesalesTax=6.25;

?常數(shù)(不可更改):

finaldoubleSALES_TAX=6.25;

?命名型即一常數(shù)全部的字母都卷大癮加使用底^

()來分可句。

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide19of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

信者存於言己tfrIB內(nèi)的基本資料型別典常數(shù)

Variablededaredgectewith

insideofamethodattributevariables

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide20of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RevistonD

SunEducationalService

幺束曾一:在程式中使用基本資料型別建數(shù)

?目檄

?步驟

?Bttt

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide21of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

檄^算子

用途算子初列

加法+sum=numl+num2

如果numl等於10、num2

等於2,更Jsum等於12。

減法一diff=numl-num2

如果numl等於10、num2

等於2,邱Jdiff等於8。

乘法*prod=numl*num2

如果numl等於10、num2

等於2,用Jprod等於20。

除法/quot=numl/num2除法is算畬回傅一他

如果numl等於10、num2整數(shù)值(非模數(shù))

等於2,即Jquot等於5。

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide22of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

模數(shù)%mod=numl%num2模數(shù)是言十算第一他

如果numl等於31、num2數(shù)除以第二彳固數(shù)所

等於6,邱Jmoci等於1。得到的繪數(shù)。

典第

相同

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide23of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

遮增典遮澗算子

?晨的表示方式:

age=age+1

?短的表示方式:

建算子用途蒯列

++前置遮增inti=6;

(++variable)intj=++i;

i等於7,j等於7

彳爰置遮增inti=6;i在遽增之前

(variabl一++)intj=i++;就指定區(qū)合jo

i等於7,j等於6所以j仍等於

6o

FundamentalsoftheJavaProgrammingLanguageModule4,slide24of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

一前置遽減int1=6;

(--variable^)intj=--i;

i等於5,j等於5

彳爰置遮減inti=6;i在遽減之前

(variable--)intj=i--;就指定幺合jo

i等於5,j等於6所以j仍等於6。

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide25of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

it增輿遮減il算子(++輿一)

翱列:

intcount=15;

inta,b,c,d;

a=count++;

b=count;

c=++count;

d=count;

System,out.printIn(a+''+b+''+c+''+d);

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide26of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

算子的侵先/慢序

?建算子侵先順序的規(guī)即

1.有一舉寸II括弧的建算子

2.遮增及遮減建算子

3.乘法典除法建算子是由左而右做言十算

4.加法輿減法建算子是由左而右做言十算

?需要建算子侵先順序的靶例:

c=25-5*4/2-10+4;

答案是34或9呢?

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide27of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

使用IB括弧

竄創(chuàng)列:

c=(((25-5)*4)/(2-10))+4;

c=((20*4)/(2-10))+4;

c=(80/(2-10))+4;

c=(80/-8)+4;

c=-10+4;

c=-6;

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide28of36

Copyngnt2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices.RevisionD

SunEducationalService

晉升典型別醇換

?冏愚

intnuml=53;//32bits言己t意醴來信者存值

intnum2=47;//32bits言己t意艘來信者存值

bytenum3;//fl留8bits言己首亶

num3=(numl+num2);//畬造成褊群金昔

?解法:

intnuml=53;

intnum2=47;

longnum3;

num3=(numl+num2);

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide29of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

晉升

?自勤彝生的晉升:

-如果你指派一彳固較小型別的燮數(shù)幺合一彳固較大型別的建

數(shù)畤

-如果你指派一彳固整數(shù)型別的燮數(shù)幺合一彳固浮黑占數(shù)型別的

?自勤褻生晉升的靶例:

longbig=6;

intsmall=99L;

FundamentalsoftheJava」巾ProgrammingLanguageModule4,slide30of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

型別斡換

?吉吾法:

identifier=(target_type)value

intnuml=53;//32bits言己t意艘來存放值

intnum2=47;//32bits言己t意艘來存放值

bytenum3;//fl留8bits言己t意骨亶

num3=(numl+num2);//與芟生褊言翳昔

?解法:

intnuml=53;//32bits言己t意艘來存放值

intnum2=47;//32bits言己驚艘來存放值

bytenum3;//fl留8bits言己首亶

num3=(byte)(numl+num2);//沒有資料ifi失

FundamentalsoftheJavan,ProgrammingLanguageModule4,slide31of36

copyright2002SunMicrosystems.Inc.AllRightsReserved.EnterpnseServices,RovistonD

SunEducationalService

型別斡換

輪例:

intmylnt;

longmyLong=99L;

mylnt=(int)(myLong);//沒有資料遺失,只有零被刪除

//更大的數(shù)目招畬造成資料遺失

//失

intmylnt;

longmyLong=123987654321L;

mylnt=(int)(myLong);//數(shù)目曾■被截掉

Fundamentalsofthe

溫馨提示

  • 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)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論