变量定义
1 public class Main { 2 3 public static void main(String[] args) { 4 5 // 定义byte类型的变量 6 byte b = 10; 7 System.out.println(b); 8 9 // 定义short类型的变量10 short s = 100;11 System.out.println(s);12 13 // 定义int类型的变量14 int i = 1000;15 System.out.println(i);16 17 // 定义long类型的变量18 long l = 100000000L; // 此处需要在值的末尾加L19 System.out.println(l);20 21 // 定义float类型的变量22 float f = 12.34F; // 此处需要再值的末尾加F23 System.out.println(f);24 25 // 定义double类型的变量26 double d = 12.34;27 System.out.println(d);28 29 // 定义char类型的变量30 char c = 'a';31 System.out.println(c);32 33 // 定义boolean类型的变量34 boolean bb = true;35 System.out.println(bb);36 37 }38 }
定义变量的注意事项:
A:变量未赋值,不能使用 B:变量只在所属范围内有效,变量属于它所在的那对大括号 C:一行可以定义多个变量(不建议)