java中類型轉化及例題

1.自動轉換類型:容量小的數據類型與容量大的數據類型作運算,容量小的會自動轉換爲容量大的數據類型。java

2.自動轉換:char,byte,short=>int=>long=>float=>double。Char,byte,short之間作運算默認的是int類型。spa

3.強制類型轉換;容量大的轉換爲容量小的,要使用強制類型轉換符:(),可能致使精度損失。命令行

例:long l1=121345;對象

    Int m1=(int)l1;blog

System.out.println(ml)文檔

4.字符串(string,加」」)與基本數據類型之間的運算,只能是鏈接運算(如+),獲得的結果仍爲一個字符串。字符串

  題1:string str1=」hello」;string

      Int myInt1=12;it

      Char chr1=’a’;io

System.out.println(strl+myInt1+chr1);//hello12a

System.out.println(myInt1+chr1+str1);//109hello

System.out.println(chrl+strl+myInt1);//ahello12

  題2:string str1=4;//錯

String str2=3.5f+」」;//對

System.out.println(str2);//輸出3.5

System.out.println(3+4+」hello!」);//輸出7hello!

System.out.println(「hello!」+3+4);//輸出hello!34

System.out.println(‘a’+1+」hello!」);//輸出98hello!

System.out.println(「hello」+’a’+1);//輸出helloa1

5.進制;二進制:0或1,以0b或0B開頭,開頭符號位,0爲正、1爲負;

十進制:0-9;

八進制:0-7,以0開頭;

十六進制:0-9及A-F,以0x或0X開頭,不區分A-F的大小寫。

6.原碼、反碼和補碼;①正數時,原碼、反碼和補碼三碼合一;②負數時,反碼爲在原碼基礎上符號位不動其他取反;補碼爲在反碼基礎上+1。

題3:

                

題4:byte的值(計算機底層-15)

 

7.十進制=>二進制:除2取餘數的逆向

例:

 

8.二進制=>八進制

 

9.二進制=>十六進制

 

10.算術運算符;+、-、*、/、%、++、--

11.除號;/

   題5:int i=12;

Int j=i/5;

Double d=i/5;

Double d2=(double)i/5;

Double d1=i/5.0;

System.out.println(j);//2

System.out.println(d);//2.0

System.out.println(d2);//2.4

System.out.println(d1);2.4

12.取模;%,取餘,結果的符號取決於被模數

   題6:int i1=12%5;

       Int i2=-12%5;

       Int i3=-12%(-5);

       Int i4=12%(-5);

       System.out.println(i1);//2

       System.out.println(i2);//-2

       System.out.println(i3);//-2

       System.out.println(i4);//2

13.加加,減減;++,--

   ①前++:先自增1,後作運算;

     後++:先作運算,後自增1;

     題7:int myInt1=10;

         Int myInt2=myInt1++;//後++

System.out.println(myInt1);//11

System.out.println(myInt2);//10

 

Int myInt3=10;

Int myInt4=++myInt;//前++

System.out.println(myInt3);//11

System.out.println(myInt4);//11

  ②前--:先自減1,後作運算;

    後--:先作運算,後自減1;

14.命令行方式;dir,md,rd,cd,cd..,cd/,del,exit

15.JDK、JRE和JVM三者關係,Java程序:編寫=>編譯=>運行。

16.單行註釋、多行註釋和文檔註釋,文檔註釋/**      */。

   解析文檔註釋:javadoc-d文件目錄名-autor-version源文件名.java;

17.賦值運算;+、+=、-=、*=、/=、%=。

            ①+=:既能夠實現運算,又不會更改s的數據類型;

              題8:i+=3至關於i=i+3;

題9:short s=10;

      S=s+3;//編譯不會經過

      S=(short)(s+3);//編譯經過,不建議此編譯

    S+=3;//既能夠實現運算,又不更改s的數據類型

②=:賦值非等號;

  題10:boolean b1=false;

       If(b1=true){system.out.println(「結果爲真」)}

       Else{system.out.println(「結果爲假」)}

       //輸出結果爲真

  題11:boolean b1=false;

       If(b1==true){system.out.println(「結果爲真」)}

       Else{system.out.println(「結果爲假」)}

       //輸出結果爲假

  題12:int i=1;

        i*=0.1;

       System.out.println(i);//0,類型仍爲int,0.1即0

18.比較運算符;①==,相等於;②!=,不等於;③<、>、<=、>=;

               ④instanceof,檢查是不是類的對象;

例:」Hello」 instanceof string//爲true 

相關文章
相關標籤/搜索