java 基礎資料類型

來源:互聯網
上載者:User
資料|資料類型

1.單一資料型別
邏輯類型  boolean
文本類型  char,String(特殊類型)
整數類型  byte,short,int,long
浮點類型  double,float

boolean  兩個值:true and false
char  16位無符號(不分正負的)Unicode字元 必須包含在單引號內('')
       eg:'\t' 表示一個定位字元
            '\u????' 表示一個特殊的Unicode字元 ????應嚴格按照4個16進位數進行替換.
String  不是一個簡單的資料類型 而是一個類(class) 它被用來表示字元序列.字元本身符合Unicode標準
         且上述char類型的反斜線符號適用於String.
         與c and c++ 不同,String不能用'\o'作為結束.(本來要寫'\零'的,可是它就是不顯示,沒招兒了,就用o來代替吧~~)
byte short int long
         在資料後面直接跟一個字母"L".L表示這是一個long型數值.在Java程式設計語言中L無論是大寫還是小寫都同樣有效,但由於小寫l與數字1容易混淆,因而,使用小寫不是一個明智的選擇.
                                           整型資料的長度和數值範圍
   ----------------------------------------------------------------------------------------------------------------------
        長度                                    資料類型                                  數值範圍
   ----------------------------------------------------------------------------------------------------------------------
        8 bits                                     byte                                   -2(7)~2(7)-1
        16 bits                                   short                                   -2(15)~2(15)-1
        32 bits                                   int                                      -2(31)~2(31)-1
        64 bits                                   long                                    -2(63)~2(63)-1
   ---------------------------------------------------------------------------------------------------------------------
float double  
          數值包含小數點或指數,或者在數字後面帶有字母F or f(float), D or d(double)
                                           浮點型資料的長度和數值範圍
   ----------------------------------------------------------------------------------------------------------------------
        長度                                    資料類型                                  數值範圍
   ----------------------------------------------------------------------------------------------------------------------
        32 bits                                   float                              1.401e-45~3.402e+38
        64 bits                                   long                              4.94e-324~1.79e+308d
   ----------------------------------------------------------------------------------------------------------------------

2.單一資料型別變數的聲明和賦值
public class Assign{
  public static void main(String[] args){
     int x,y;                                          //declare int variables
     float z=3.414f;                               //declare and assign float variable
     double w=3.1415;                          //declare and assign double variable
     boolean truth=true;                        //declare and assign boolean variable
     char c;                                           //declare character variable
     String str;                                       //declare String variable
     String str1="bye";                            //declare and assign String variable
  c='A';                                                //assign value to char variable
  str="Hi out there!";                             //assign value to String variable
  x=6;
  y=1000;                                            //assign values to int variable
  ...
  }
}

下面是一些非法賦值的語句:
y=3.1415926;                               //is not an int.Requires casting and decimal will be truncated
w=175,000;                                 //the comma symbol(,)cannot appear
truth=1;                                      //a common mistake made by ex- c/c++ programmers
z=3.14156;                                  //can't fit double into a Float 
                                             



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.