Java變數型態,溢位overflow,補數

w3schools陳述Java定義變數的規則

變數名稱的規定:
  • 使用字母,數字,底線_和金額符號$命名
  • 必須使用字母或金額符號$開頭或底線_開頭
  • 必須使用小寫字母開頭,不能使用空白
  • 也可以使用金額符號$開頭或底線開頭,但w3schools的示範案例不會如此做
  • 字母有大小寫之分,例如"myVar"和"myvar"是不同的變數
  • 不能使用保留字當作變數名稱,例如int或boolean等
The general rules for naming variables are:
  • Names can contain letters, digits, underscores, and dollar signs
  • Names must begin with a letter
  • Names should start with a lowercase letter and it cannot contain whitespace
  • Names can also begin with $ and _ (but we will not use it in this tutorial)
  • Names are case sensitive ("myVar" and "myvar" are different variables)
  • Reserved words (like Java keywords, such as int or boolean) cannot be used as names

來源: https://www.w3schools.com/java/java_variables_identifiers.asp

Data Type	Size	Description	
byte	1 byte	Stores whole numbers from -128 to 127	
short	2 bytes	Stores whole numbers from -32,768 to 32,767	
int	4 bytes	Stores whole numbers from -2,147,483,648 to 2,147,483,647	
long	8 bytes	Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807	
float	4 bytes	Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits	
double	8 bytes	Stores fractional numbers. Sufficient for storing 15 decimal digits	
boolean	1 bit	Stores true or false values	
char	2 bytes	Stores a single character/letter or ASCII values
資料型態長度(位元組)範圍最小範圍最大
位元數1位元組=8位元-128127
短整數2位元組=16位元-32,76832,767
整數 4位元組=32位元-2,147,483,6482,147,483,647
長整數8位元組=64位元-9,223,372,036,854,780,000 9,223,372,036,854,780,000
浮點數4位元組=32位元1.4*10^-453.4*10^38
倍精數8位元組=64位元4.9*10^-3243.4*10^308
布林1位元falsetrue
字元2位元組=16位元'\u0000''\uffff'
public class Main {
  public static void main(String[] args) {
    float f1 = 35e3f;
    double d1 = 12E4d;
    System.out.println(f1);
    System.out.println(d1);  
    System.out.println( Byte.MIN_VALUE ); 
    System.out.println( Byte.MAX_VALUE ); 
    System.out.println( Short.MIN_VALUE ); 
    System.out.println( Short.MAX_VALUE ); 
    System.out.println( Integer.MIN_VALUE ); 
    System.out.println( Integer.MAX_VALUE ); 
    System.out.println( Float.MIN_VALUE );
    System.out.println( Float.MAX_VALUE ); 
    System.out.println( Double.MIN_VALUE ); 
    System.out.println( Double.MAX_VALUE );
    System.out.println( Integer.toString( Character.MIN_VALUE ) ); 
    System.out.println( Integer.toString( Character.MAX_VALUE ) );
  }
}
public class Main {
  public static void main(String[] args) {
    char s = 40845;     //宣告字元 s 16進位'\u9f8d'表示 
    System.out.println(s);
    System.out.println(s % 128);
    System.out.println(s % 128 -128);
    System.out.println(s % 32768);
    System.out.println(s % 32768 -32768);
    System.out.println( (byte) s);  
    System.out.println( (short) s);  
    System.out.println( (int) s);  
    System.out.println( (long) s);  
  }
}

留言

  1. https://cpc920508123.blogspot.com/2022/10/javaminvaluemaxvalue.html

    回覆刪除
  2. https://lilyhuangyuanntakming.blogspot.com/2023/01/java.html

    回覆刪除
  3. https://lilyhuangyuanntakming.blogspot.com/2023/01/javaoverflow.html

    回覆刪除
  4. https://lilyhuangyuanntakming.blogspot.com/2023/01/java-data-type.html

    回覆刪除
  5. https://lilyhuangyuanntakming.blogspot.com/2023/01/javaminvaluemaxvalue.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

劉任昌對照Python與JavaScript指令

劉任昌Python集合set清單list元組tuple字典dictionary

劉任昌python, input, str, float