Java基础语法 - 变量的定义和使用

变量定义

 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;    // 此处需要在值的末尾加L
19         System.out.println(l);
20 
21         // 定义float类型的变量
22         float f = 12.34F;       // 此处需要再值的末尾加F
23         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:一行可以定义多个变量(不建议)

猜你喜欢

转载自www.cnblogs.com/CongZhang/p/9907179.html