字面常量
java里有两种常量,一种叫字面常量,而使用final修饰的常量为符号常量。
字面常量有很多种类型:
整型常量 | 1 3 45 |
---|---|
实型常量 | 2.78293 |
字符常量 | ‘a’ (单引号引起来的单个字母) |
字符串常量 | “string”(双引号) |
逻辑常量 | true false |
变量
变量在赋值前必须声明变量的类型,比如说下例的num我声明了为int类型:
public class VarTest{
public static void main(String[] args){
int num ; //you must define the variable's type
num = 1 ; // assignment
num = num - 3 ;
num = 8 ;
System.out.println(num) ;
}
}
发现输出的是最后一行我输入的8
也可以进行反编译:
ps:变量不能重复定义
变量的作用域
变量的作用域是在离他的最近的花括号内
变量在不同的地方代表不同的意义:
局部变量:定义在方法中
成员变量:定义在类中,方法外