顾名思义 变量即可以变化的量
Java是一种强类型语言,每个变量都必须声明其类型、
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
type varName [=value] [{,varName[=value]}];
//数据类型 变量名 =值 ; 可以使用逗号隔开来声明多个同类变量
注:
*** 每个变量都有类型,类型可以是基本类型,也可以是引用类型***
变量名必须是合法的标识符
变量名声明是一条完整的语句,因此每一个声明都必须以分号结束
变量作用域
类变量
//类变量 static
static double salary = 2500;
类变量在方法的外面,类的里面
实例变量
// 实例变量:从属于对象;如果不自行初始化,则输出这个类型的默认值
//布尔值的默认值是false
//除了基本类型,其余类型的默认值都为null
System name;
int age;
实例变量在方法外面
局部变量
public static void main(String[] args) {
//局部变量,只在对应方法里起作用
int i = 10;//局部变量:必须要声明和初始化值
System.out.println(i);
常量
常量初始化后不能再改变的值,不能变动
常量可以理解为一种特殊的变量,它的值被设定后,在程序运行的过程中不能允许被改变
常量用***“final”*** 定义
//final 常量名 = 值
常量名一般使用大写的的字符
变量的命名规范- 所有变量、方法、类名都要见名知意 让别人能读懂。类成员变量: 首字母小写和驼峰原则 :例: mouthSalary 除了第一个单词外,后面的单词首字母大写。局部变量:首字母小写和驼峰原则常量:大多谢字母和下划线 例:MAX_VALUE类名:首字母大写和驼峰原则 例: GoodMan方法名: 首字母小写和驼峰原则:run(),runRun()
基本运算符
算术运算符: +,- , * , / ,%(取余) , ++ ,–(自增,自减).
赋值运算符:=
关系运算符:> , < , >= , <= , == , !=instanceof
逻辑运算符: && , || ,!
优先级()
算术运算符:
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D 复制当前行到下一行
int a = 10;
int b = 20;
int c = 30;
int d = 40;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(float)b);
}
}
输出结果:
package operator;
public class Dmeo02 {
public static void main(String[] args) {
long a = 123456123132L;
int c = 45;
short b = 2;
double d = 8;
byte e = 2;
System.out.println(a+c+b+d);//long
System.out.println(c+b);//int
System.out.println(c*b*d);//double
System.out.println(c*b*e);//int
}
}
关系运算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果 : 正确 错误 是一个布尔值
// % 取余
int a= 10;
int b= 10;
int c= 20;
int d= 15;
System.out.println(a==b);
System.out.println(a
运算结果:
自增自减:
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算符
int a =1;
int b =a++;//b=a ,a++ 此时b=1 a=2
int c =++a;//a++ ,c=a 此时a=3 c=3
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
输出结果:
逻辑运算符:
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
// 与(&& and) 或(|| or) 非(取反)
boolean a =true;
boolean b =false;
System.out.println("a || b:" +(a||b));//或 一个为真则值为真
System.out.println("a && b:" +(a&&b));//与 一个为假则值为假
System.out.println("!a && b:" +!(a&&b));//如果是真则为假,是假则为真 与(与)的值相反
//短路运算
int c = 5;
boolean d =(c<4)&&(c++ <4);//这里c<4已经为假 代码停止运行直接得出结果false后面c++并没有执行
System.out.println(d);
System.out.println(c);//c++没有执行输出结果仍然为5
}
}
运算结果:
位运算:
package operator;
public class Demo06 {
public static void main(String[] args) {
System.out.println(2<<3);//2的二进制数往左移3位 0001 0000 等于16
}
逻辑运算:a+=b a-=b &字符串连接符
package operator;
public class Dmeo07 {
public static void main(String[] args) {
int a =10;
int b =20;
a+=b;//a+b=a;a=30
a-=b;//a-b=a;30-20=10
System.out.println(a);// 这里输出10
//字符串链接符 + , string
System.out.println(""+a+b);//链接符在前 输出字符串
System.out.println(a+b+"");// 连接符在后 输出的是a+b的值
}
}
三元运算符:
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x?y:z
// 如果x==true,则结果为y,否则结果为z;
int scor = 80;
String type = scor<60?"不及格":"及格";
// if
System.out.println(type);
}
}
输出结果为:



