变量
变量表示源程序中对内存数据进行存储/获取的一种引用符号(可以被修改,属于标识符)
变量又分为全局变量和局部变量
public class Test{
int age = 15; //全局变量
public static void main(String []args){
int num = 123;//局部变量
int money = 456;//局部变量
}
}
位于类当中在任何方法之外的属于类的全局变量(在类的任何地方都能访问)
位于类中方法之内的属于局部变量(只有在该方法中可以访问)
当在上述方法中System.out.println(age);会出现错误,
这时要想在方法中输出age需要将全局变量变成static int age = 15;因为在静态方法中无法引用非静态的变量
常量
不能被修改的数据就是常量,常量在程序中是只读的(不能被修改,属于标识符)
常量分为 数字(数值,字符串,字符,布尔常量)和标志性常量(用final修饰的变量叫做标志性常量)
public class Test{
public static void main(String []args){
int age =15; //15为数字常量
final int MAX_NUM = 28;//MAX_NUM为标志性常量,28位数字常量
}
}
final修饰的变量必须赋值,而且只能赋值一次
final int NUM; ×
final int AGE = 15;
AGE = 13; ×



