java基礎資料型別 (Elementary Data Type)

來源:互聯網
上載者:User

標籤:java

1.資料類型分類


2.基礎資料型別 (Elementary Data Type)


3.整數資料類型

A.基本概念  

   Int x=0;

   分配4個位元組記憶體,一個位元組佔8位,取值範圍 -2的31次方到2的31次方-1

   Bytex=-12;

   分配一個位元組,機器編碼

   Shortx=12;

   分配2個位元組,強調類型時,使用方法  (short)12;

   Long類型用尾碼L表示

   Long  width=12L;

   分配8個位元組佔64位

B.資料溢出

public class Demo1 {
public static void main(String[] args) {
int max=Integer.MAX_VALUE;
System.out.println("整形max:"+max);
System.out.println("max+1:"+(max+1));
System.out.println("max+2:"+(max+2));
}
}

結果:

整形max:2147483647
max+1:-2147483648//值變成最小的
max+2:-2147483647//值變成次最小的

解決方案:

int max=Integer.MAX_VALUE;
System.out.println("整形max:"+max);
System.out.println("max+1:"+(max+1));
System.out.println("max+2:"+(max+2L));
System.out.println("max+2:"+((long)max+2));

這裡如果是LONG資料類型溢出就無法解決了。

4.字元類型

常量:’123’   ‘A’  ‘!’  ‘你好’

 一般使用  char  聲明

代碼:

//字元和整形之間轉換
char ch1=‘a‘;
char ch2=97;
System.out.println(ch1+"---"+ch2);

常用的逸出字元:


代碼:

char chinaWord=‘好‘,japanWord=‘?‘;

char you=‘\u4F60‘;

int position=20320;

System.out.print("漢字:"+chinaWord+"的位置:"+(int)chinaWord);

5.浮點類型

Float 類型

後面必須有f或者F,一般保留8位有效資料

分配4個位元組

Double類型

後面有尾碼d或者D,可以省略

分配8個位元組

float num=3.0f;
System.out.println("num*num="+num*num);

6.布爾類型

常量:true   , false

變數:bool on=true;

代碼:

boolean flag=true;
System.out.println(flag);
System.out.println(!flag);

7.資料類型的預設值


作用:java程式中有時存在沒有賦值的變數,這樣就失去了意義,定義預設值使這種情況避免發生。

著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

java基礎資料型別 (Elementary Data Type)

聯繫我們

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