变量
由字母数字下划线美元符号组成 不能以数字开头 不能使用java 关键字
数据类型
String 字符串
char 字符型
float 单精度浮点型
double 双精度浮点型
boolean 布尔型
类型自动转换
public class HelloWorld{
public static void main(String[] args) {
double avg1=78.5;
int rise=5;
double avg2=avg1+rise;
System.out.println("考试平均分:"+avg1);
System.out.println("调整后的平均分:"+avg2);
}
}
强制类型转换(int)
public class HelloWorld{
public static void main(String[] args) {
double heightAvg1=176.2;
int heightAvg2=(int)heightAvg1;
System.out.println(heightAvg1);
System.out.println(heightAvg2);
}
}
常量的应用 final
public class HelloWorld{
public static void main(String[] args) {
final String LOVE = "IMOOC";
final double num = 23.6;
final char boy = '男';
final char giil = '女' ;
System.out.println(boy);
System.out.println(giil);
}
}
赋值运算符
public class HelloWorld{
public static void main(String[] args) {
int one = 10 ;
int two = 20 ;
int three = 0 ;
three = one + two;
System.out.println("three = one + two ==> " + three);
three += one;
System.out.println("three += one ==> " + three);
three -= one;
System.out.println("three -= one ==> " + three);
three *= one;
System.out.println("three *= one ==> " + three);
three /= one;
System.out.println("three /= one ==> " + three);
three %= one;
System.out.println("three %= one ==> " + three);
}
}
比较运算符
public class HelloWorld{
public static void main(String[] args) {
int a=16;
double b=9.5;
String str1="hello";
String str2="imooc";
System.out.println("a等于b:" + (a == b));
System.out.println("a大于b:" + (a > b));
System.out.println("a小于等于b:" + (a <= b));
System.out.println("str1等于str2:" + (str1 == str2));
}
}
逻辑运算符
public class HelloWorld {
public static void main(String[] args) {
boolean a = true; // a同意
boolean b = false; // b反对
boolean c = false; // c反对
boolean d = true; // d同意
System.out.println((a&&b) + "未通过");
System.out.println((a||b) + "通过");
System.out.println((!a) + "未通过");
System.out.println((c^d) + "通过");
}
}
条件运算符
public class HelloWorld{
public static void main(String[] args) {
int score=68;
String mark = score > 60 ? "及格" : "不及格";
System.out.println("考试成绩如何:"+mark);
}
}