1. 程式人生 > 其它 >相關資料型別筆記

相關資料型別筆記

資料型別

強型別語言:要求變數的使用要嚴格符合規定,所有變數都必須先定義後才能使用

安全性高,速度慢

弱型別語言

資料型別分類

  1. 基本型別(重點):
public class Six1 {
    public static void main(String[] args) {
      //八大基本資料型別
        //整數
        int num1 = 10;//最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 40L;//Long型別要在數字後面+L
        //小數:(浮點數)
        float num5 = 50.1F;//float型別要在數字後面+F
        double num6 = 3.14159264323598562398659328562395;
        //char字元
        char name = 'a';
        //字串string不是關鍵字,他是一個類
        String name2 = "sfhsoduhsd";
        //布林值:是非
        boolean flag = true;
        //boolean flag = false;

    }
}
  1. 引用型別
    1. 介面
    2. 陣列

位元組

位(bit):是計算機內部資料儲存的最小單位

位元組(byte):是計算機中資料處理的基本單位,習慣用Byte來表示

1B = 8 bit

1024B = 1KB;

1024KB = 1M;

1024M = 1G;

在比較大小時,最好不要使用浮點數進行比較!

BigDecimal 數學工具類

拓展

整數拓展

進位制

  1. 二進位制 0b
  2. 十進位制
  3. 八進位制0
  4. 十六進位制0x
int i= 10;
int i1 = 010;
int i2 = 0x10;
System.out.println(i);
System.out.println(i1);
System.out.println(i2);

輸出:10
8
16

浮點數拓展

float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);//false
System.out.println(f);
System.out.println(d);
float d1 = 1213545215465432545132f;
float d2 = d1 + 1;
System.out.println(d1);
System.out.println(d2);
System.out.println(d1==d2);//true

輸出:false
0.1
0.1
1.2135452E21
1.2135452E21
true
float:有限、離散、舍入誤差、大約、接近但不等於

建議不要用float來作比較!

面試時:銀行業務怎麼表示?->BigDecimal數學工具類

字元型拓展

char c1 = 'a';
char c2 = '中';
char c3 = 'A';
System.out.println(c1);
System.out.println((int)c1);//強制轉換
System.out.println(c2);
System.out.println((int)c2);//強制轉換
System.out.println(c3);
System.out.println((int)c3);//強制轉換

//所有的字元本質還是數字
//編碼 Unicode 0-65536個字元

輸出:a
97

20013
A
65

轉義字元

//轉義字元
//\t 製表符
//\n 換行
//....
System.out.println("Hello\tWorld");
System.out.println("Hello\nWorld");

輸出:Hello World
Hello
World

布林值拓展

//布林值擴充套件
boolean flag  = true;
if(flag==true){ }//新手
if(flag){}//老手

物件 從記憶體分析

String original;
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);//false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);//true
//物件 從記憶體分析、

輸出:false
true