1. 程式人生 > 實用技巧 >【進階之路】Redis基礎知識兩篇就滿足(二)

【進階之路】Redis基礎知識兩篇就滿足(二)

Java的資料型別

資料型別簡稱資料元( Data Element),也稱為資料元素,是用一組屬性描述其定義、標識、表示和允許值的資料單元

分為基本型別和引用型別兩種

1.基本型別

整型

  1. byte佔1個位元組

  2. short佔2個位元組

  3. int佔4個位元組

  4. long佔8個位元組 建立這個物件時需要給值後面加個L long number=12345678765432345L;

浮點型

  1. float佔4個位元組 建立這個物件時需要給值後面加個F float number=12.234F;
  2. double佔8個位元組

字元

單字元
char name='國' 只能有一個字元
字串 String不是基本型別 是類 屬於引用型別
String name="中國"
char使用'' String使用""

boolean型別

只有true和false兩個值 佔一個位

boolean result=true;

boolean result=false;

2.引用型別

介面

陣列

什麼是位元組

位元組(Byte)是計算機資訊科技用於計量儲存容量的一種計量單位,也表示一些計算機程式語言中的資料型別和語言字元

資料儲存是以“位元組”(Byte)為單位,資料傳輸大多是以“位”(bit,又名“位元”)為單位,一個位就代表一個0或1(即二進位制),每8個位(bit,簡寫為b)組成一個位元組(Byte,簡寫為B),是最小一級的資訊單位

位元組(Byte)=8位(bit)

1KB( Kilobyte,千位元組)=1024B

1MB( Megabyte,兆位元組)=1024KB

1GB( Gigabyte,吉位元組,千兆)=1024MB

1TB( Trillionbyte,萬億位元組,太位元組)=1024GB

基本型別拓展

public class Demo {
    public static void main(String[] args) {
        //整型拓展: 進位制  二進位制0b 十進位制 八進位制0 十六進位制0x
        int i=10;
        int i2=010;
        int i3=0x10;
        System.out.println(i);
        System.out.println(i2); //八進位制0
        System.out.println(i3); //十六進位制0x 0-9 A-F 16

        System.out.println("==============================================================");
        //浮點數拓展: 銀行的錢怎麼表示
        //BigDecimal 數學工具類
        //float 有限 離散 舍入誤差 大約 接近但是不等於
        //double
        //最好完全不要使用浮點數進行比較
        float f=0.1F;
        double d=1.0/10;
        System.out.println(f==d);

        float f1=123456763212345678F;
        float f2=f1+1;
        System.out.println(f1==f2);
        System.out.println("==============================================================");

        //字元拓展
        char c='a';
        char c1='國';
        System.out.println(c);
        System.out.println(c1);
        System.out.println((int)c);
        System.out.println((int)c1);
        //所以字元的本質還是數字
        System.out.println("中\n國");
        //\n 換行
        System.out.println("中\t國");
        //\t 空格符
    }
}

型別轉換

型別轉換級別的低高

  1. byte,short,char-->int-->long-->float-->double 從高到低需要強轉
int i=12;
byte b= (byte) i;
System.out.println(b);
  1. 從低到高可以自動轉換,不需要強轉
char c='a';
float f=c;
System.out.println(f);

注意點:

  1. 不能對布林型別進行轉換
  2. 不能把物件型別轉成不相干的型別
  3. 轉換可能遇到記憶體溢位(長度過長),或者精度(不能精確值)問題
System.out.println((int)123456784322345678765432345678765); //超出了int的最大設定長度
System.out.println((int)39.9);//值為39 不能精確值 精度丟(diu)失
  1. 當值過大時,jdk7增加了新特性,可以使用下劃線
long number=1244_5794_9694L;
  1. 當需要計算的值太大,容易出現溢位問題
int money=1234566546;
int year=20;
long result=(long)money*year; //當不使用強轉時,由於溢位問題,會出現任意值,所以可以在計算完結果是給他強轉
System.out.println(result);		//要麼就在計算的時候給他強轉long result=money*(long)year;