1. 程式人生 > >Java基礎學習 一 (字符集、識別符號、關鍵字、註釋、變數、基本資料型別、非基本資料型別、高精度數字)

Java基礎學習 一 (字符集、識別符號、關鍵字、註釋、變數、基本資料型別、非基本資料型別、高精度數字)

一 字符集:

       java採用Unicode字符集 包含65535個字元

二 識別符號:

        由字母、數字、下劃線及美元符號等組成的一個字元序列,用來標識變數、方法名、類名、介面名等。

         規則:1. 數字不能開頭

                    2. 第一個字元後,可以是貨幣符號、連字元號、數字、字母、漢字的任意字元。

                    3. 識別符號的字元數量沒有限制。一般不要太長

                    4. java的關鍵字不能做識別符號

                    5. java的識別符號是區分大小寫的

                    6. 識別符號中不能包含空格、單引號、雙引號、分號等

三 關鍵字:

abstract

assert

boolean

break

byte

case

catch

char

class

const
continue default do double else
enum extends final finally float
for
goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatile while

四 註釋:

// 這是雙斜線註釋 用於單行註釋

/*

 這是多行註釋
*/

/**
文件註釋
*/

五 變數:(儲存資料的儲存單元)

        宣告變數: 型別 變數名

int a = 0;
//或
int a;

六 基本資料型別:(8個 四大類)

         【一個位元組八位  1byte = 8bit】

        整型:

            byte (1byte  -128 — 127) 預設值 (byte)0

            short (2byte  -2**15 — 2**15 -1)  預設值 (short)0

            int (4byte  -2**31 — 2**31 -1)  預設值 0

            long (8byte  -2**63 — 2**63 -1)  預設值 0L

        浮點數:float (4byte)  預設值 0.0f、double (8byte)  預設值 0.0d

       字元型別:char (2byte)  預設值 '\u0000'(null)

        布林型別[邏輯型別]:boolean (大小JVM規範並沒有指定)   預設值 false

// 十進位制
int int10 = 10;

// 八進位制 0開頭
int int8 = 012; //10

// 十六進位制  0X開頭
int int16 = 0XFF; //255

        基本資料型別的自動轉換關係   

        強制轉換

            用於級別高的向級別低的型別進行轉化和 char 與 byte short相互轉化 精確度可能損失

char ch = 'a';
short s = 21;
ch = (char) s;

七 非基本資料型別:(由基本型別組合的新型別、陣列、字串、類、介面)

boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

 

八 高精度數字:

        BigInteger    儲存超大型數字 

        BigDecimal    儲存貨幣金額

        使用方法

BigInteger bigInteger1 = new BigInteger("123");
BigInteger bigInteger2 = new BigInteger("456");
BigInteger result;
result = bigInteger1.add(bigInteger2); //加
result = bigInteger1.subtract(bigInteger2); //減
result = bigInteger1.multiply(bigInteger2); //乘
result = bigInteger1.divide(bigInteger2); //除
result = bigInteger1.remainder(bigInteger2); //取餘%
result = bigInteger1.pow(2); //乘方