type varName [=value] [{,varName[=value]}]; // 数据类型 变量名 = 值;也可以使用逗号隔开来声明多个同类型的变量
注意事项
public class Demo03 { public static void main(String[] args) { int a,b,c; int d=1,e=1,f=1; String name = "葛老头"; char x = ‘x‘; double g = 4.45; } }
public class Demo04 { // 类变量 static static double salary = 2500; // 实例变量:从属于对象;如果不赋值的话,会有默认值 // 布尔型:默认是false // 引用类型:默认是null // 八大基本类型:默认是0 String name; int age; // main方法 public static void main(String[] args) { //局部变量;必须声明和赋值 int i =10; System.out.println(i); // 实例变量的引用 // 变量类型 变量名字 = new Demo04(); Demo04 demo04 = new Demo04(); System.out.println(demo04.age); System.out.println(demo04.name); // 类变量的引用 System.out.println(salary); } }
final 常量类型 常量名 = 值; final double PI = 3.14;
public class Demo05 { // final和static是修饰符不存在先后顺序 // final修饰这个变量为常量 // static 修饰这个变量作用域为类变量 static final double PI = 3.14; final static double UI = 3.14; public static void main(String[] args) { System.out.println(PI); System.out.println(UI); } }
Java基础系列(11)- 变量、常量、作用域以及变量的命名规范
原文:https://www.cnblogs.com/gltou/p/15223380.html