1. 程式人生 > 資訊 >蘋果 AirTag 主要競爭對手、知名藍芽防丟器公司 Tile 被收購,作價 2.05 億美元

蘋果 AirTag 主要競爭對手、知名藍芽防丟器公司 Tile 被收購,作價 2.05 億美元

Java 變數

變數的概念

  • 記憶體中的一個儲存區域
  • 該區域的資料可以在同一類型範圍內不斷變化
  • 變數是程式中最基本的儲存單元。包含變數型別,變數名和儲存的值

變數的作用

  • 用於在記憶體中儲存資料

使用變數注意的地方

  • Java 中每個變數必須先宣告,後使用
  • 使用變數名來訪問這塊區域的資料
  • 變數的作用域:其定義所在的一對{}內
  • 變數只有在其作用域內才有效
  • 同一個作用域內,不能定義重名的變數
class IdentifierTest{
    public static void main(String[] args){
        // 變數的定義
        int myNumber = 1001;
        
// 變數的使用 System.out.println(myNumber); // 變數的宣告 int my2Number; // 變數的賦值 my2Number = 1002; System.out.println(my2Number); } }

Java 變數的分類

基本資料型別:

整型: byte, short, int, long
浮點型: float, double
字元型: char
布林型: boolean

引用資料型別:

類(class)
介面(interface)
陣列(array)

注:引用型別的變數,只可能儲存兩類值:null 或 地址值。

整型: byte, short, int, long

  • java 的整型常量預設為 int 型,宣告 long 型常量須後加 'l' 或 'L'
  • java 程式中變數統籌宣告為 int 型,除非不足以表示較大的數,才使用 long
型別佔用儲存空間表數範圍
byte 1位元組=8bit位 -128~127
short 2位元組 -2^15 ~ 2^15-1
int 4位元組 -2^31 ~ 2^31-1(約21億)
long 8位元組 -2^63 ~ 2^63-1
class VariableTest {
    
public static void main(String[] args){ byte b1 = 12; byte b2 = -128; System.out.println(b1); System.out.println(b2); short s1 = 128; int i1 = 1234; long l1 = 1234567L; System.out.println(s1); System.out.println(i1); System.out.println(l1); } }

浮點型: float, double

浮點型常量有兩種表示方式:

  • 十進位制數形式: 如: 5.12, 512.0f, .512 (必須有小數點)
  • 科學計數法形式:如:5.12e2, 512E2, 100E-2
  • fload: 單精度,尾數可以精確到7位有效數字。很多情況下,精度很難滿足需求。
  • double: 雙精度, 精度是 float 的兩倍。通常採用此型別。
  • Java 的浮點型常量預設為 double 型,宣告fload型常量,須後加 'f' 或'F'
型別佔用儲存空間表數範圍
單精度 float 4位元組 -3.403E38~3.403E38
雙精度 double 8位元組 -1.798E308~1.798E308
class VariableTest {
    public static void main(String[] args){
        // 浮點型:float(4位元組), double (8位元組)
        // 浮點型,表示帶小數點的數值
        // float 表示數值的範圍比 long 還大
        double d1 = 123.3;
        System.out.println(d1 + 1);
        
                // 定義 float 型別時,需要以 "f" 或 "F" 結尾
                float f1 = 12.3F;
        System.out.println(f1);
                // 通常定義浮點型變數時,使用 double 型

    }
}

浮點型:

  • char 型資料用來表示通常意義上"字元"(2位元組)
  • Java 中所有字元都使用 Unicode 編碼,故一個字元可以儲存一個字母,一個漢字或其他書面語的一個字元
  • char 型別是可以進行運算的。如: \u000a 表示 \n
class VariableTest {
    public static void main(String[] args){

                // 字元型: char(1個字元=2個位元組)
                // 定義 char 型變數,通常使用一對 ''
                char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println(c2);

                // 宣告一個轉義字元
                char c3 = '\n';
        System.out.print("Hello" + c3);
        System.out.println("World");

                char c4 = '\u0043';
        System.out.println(c4);

    }
}

布林型: boolean

boolean 型只能取兩個值之一: true, false
常常在條件判斷,迴圈結構中使用

class VariableTest {
    public static void main(String[] args){
                boolean bb1 = true;
        System.out.println(bb1);

                boolean isMarried = true;
        if(isMarried){
            System.out.println("Good");
        } else {
            System.out.println("Not bad");
        }
    }
}

字串型別:String

  • String 不是基本資料型別,屬於引用資料型別, 翻譯為:字串
  • 使用方式與基本資料型別一致。例如: String str="abcd"
  • 宣告 String 型別變數時,使用一對 ""
  • 一個字串可以串接另外一個字串,也可以直接串接其他型別的資料。
  • String 可以和8種基本資料型別變數做運算,且運算只能是連線運算:+
  • 運算的結果仍然是 String 型別
class VariableTest4 {
    public static void main(String[] args) {
        String s1 = "Hello World!";
        
        System.out.println(s1);   // Hello World!
    }
    
}
class StringTest{
    public static void main(String[] args){
        char c = 'a';
        int num = 10;
        String str = "hello";
        System.out.println(c + num + str);      //107hello
        System.out.println(c + str + num);      //ahello10
        System.out.println(c + (num + str));    //a10hello
        System.out.println(str + num + c);      //hello10a
        
        System.out.println("*    *");           //*    *   
        System.out.println('*' + '\t' + '*');   //93
        System.out.println('*' + "\t" + '*');   //*    *
        System.out.println('*' + '\t' + "*");   //51*
        System.out.println('*' + ('\t' + "*")); //*    *
    }
}