零基礎學習java:變數(二),基礎學習java變數

來源:互聯網
上載者:User

零基礎學習java:變數(二),基礎學習java變數
 

上文介紹了變數的概念與資料類型,現在給大家介紹一下變數幾種資料類型之間的轉換。

1、自動類型轉換:容量小的類型自動轉換為容量大的資料類型。資料類型按容量大小排序為:

 

有多種類型的資料混合運算時,系統首先自動將所有資料轉換成容量最大的那種資料類型,然後再進行計算。

byte,short,char之間不會相互轉換,他們三者在計算時首先轉換為int類型。

當把任何基本類型的值和字串值進行串連運算時(+),基本類型的值將自動轉化為字串類型。

2、強制類型轉換

自動類型轉換的逆過程,將容量大的資料類型轉換為容量小的資料類型。使用時要加上強制轉換符(()),但可能造成精度降低或溢出,格外要注意。 

通常,字串不能直接轉換為基本類型,但通過基本類型對應的封裝類則可以實現把字串轉換成基本類型。

 

如: String a = “43”; int i = Integer.parseInt(a);

boolean類型不可以轉換為其它的資料類型。

案例:

/*

變數之間的運算:(不考慮boolean。剩下:char byte short int long floatdouble)

1.自動類型轉換

2.強制類型轉換

*/

class TestVeriable1{

    publicstaticvoid main(String[] args){

       //1.自動類型轉換:當容量小的資料類型與容量大的資料類型做運算時,容量小的會自動轉換為

       //容量大的資料類型:char,byte,short ===>int===>long ===>float===double

       int i1 = 12;

       short s1 = 2;

       int i2 = i1 + s1;

       float f1 = 12.3F;

       float f2 = f1 + i2;

       //float d1 = f2 + 12.3;

 

       long l = 12L;

       float f3 = l;

       System.out.println(i2);

       System.out.println(f2);

 

       char c1 = 'a';//97

       c1 = 'A';//65

       int i3 = c1 + 1;

       System.out.println(i3);

 

       //需要注意的:當char\byte\short之間做運算時,預設的結果為int類型

       short ss1 = 12;

       byte bb1 = 1;

       char cc1 = 'a';

       //short ss2 = ss1 + bb1;

       int ii1 = ss1 + bb1;

       //char cc2 = cc1 + bb1;

       int ii2 = cc1 + bb1;

       short ss2 = 11;

       //short ss3 = ss1 + ss2;

      

       //2.強制類型轉換:容量大轉換為容量小的.要使用強制類型轉換符:()

       //強制類型轉換的問題:導致精度的損失

       long l1 = 12345L;

       int m1 = (int)l1;

       System.out.println(m1);

 

       byte by1 = (byte)m1;

       System.out.println(by1);

 

       //平時常用的字串,也是一種資料類型:String

       String nation = "我是一個中國人";

       System.out.println(nation);

       //字串與基礎資料型別 (Elementary Data Type)之間的運算:只能是串連運算:+。得到的結果仍為一個字串

       String str = "abc";

       String str1 = str +m1;//abc12345

       System.out.println(str1);

 

       //題目:

       String st1 = "hello";

       int myInt1 = 12;

       char ch1 = 'a';//97

       System.out.println(str1+ myInt1 + ch1);//hello12a

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

       System.out.println(ch1+ str1 + myInt1);//ahello12

 

       String st2 = "12";

       str2 = 12 + "";

    }

}

 

 

練習:

String str1 = 4;        //判斷對錯:錯

String str2 = 3.5f + “”;             //判斷str2對錯:對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);            //輸出:Hello!a1

如果對java感興趣,想瞭解更多的java入門知識,請大家加(858568103),我們這裡有java高手,協助大家解決問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.