java基础06变量、常量、作用域

//变量
public class Demo05 {
public static void main(String[] args) {
//int a, b, c;
//int a=1,b=2,b=3; //程序的可读性
String name = "Leo";
char x = 'X';
double pi = 3.14;
}
}

//常量
public class Demo07 {

//修饰符,不存在先后顺序
static final double PI = 3.14;
final static double PI = 3.14;

public static void main(String[] args) {
System.out.println(PI);
}
}


//变量作用域
public class Demo06 {

//类变量 static
static double salary = 2500;

//属性:变量

//实例变量:从属于对象; 如果不自行初始化,这个类型默认值 0 0.0
//布尔值;默认是false
//除了基本类型,其余的默认值是null
String name;
int age;

//main方法
public static void main(String[] args) {

//局部变量;必须声明和初始化
int i = 10;
System.out.println(i);

//变量类型 变量名字 = new Demo06()
Demo06 demo06 = new Demo06();
System.out.println(demo06.age);
System.out.println(demo06.name);

//类变量 static
System.out.println(salary);
}

//其他方法
public void add(){

}
}



原文地址:https://www.cnblogs.com/yuanzhihui/p/14856943.html