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位元 | -128 | 127 |
短整數 | 2位元組=16位元 | -32,768 | 32,767 |
整數 | 4位元組=32位元 | -2,147,483,648 | 2,147,483,647 |
長整數 | 8位元組=64位元 | -9,223,372,036,854,780,000 | 9,223,372,036,854,780,000 |
浮點數 | 4位元組=32位元 | 1.4*10^-45 | 3.4*10^38 |
倍精數 | 8位元組=64位元 | 4.9*10^-324 | 3.4*10^308 |
布林 | 1位元 | false | true |
字元 | 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);
}
}
https://cpc920508123.blogspot.com/2022/10/javaminvaluemaxvalue.html
回覆刪除https://lilyhuangyuanntakming.blogspot.com/2023/01/java.html
回覆刪除https://lilyhuangyuanntakming.blogspot.com/2023/01/javaoverflow.html
回覆刪除https://lilyhuangyuanntakming.blogspot.com/2023/01/java-data-type.html
回覆刪除https://lilyhuangyuanntakming.blogspot.com/2023/01/javaminvaluemaxvalue.html
回覆刪除