Java基础
- 常用关键字
- 类型转换
从低到高
//强制转换 (类型)变量名 高–低
//自动转换 低–高
注意点:
1.不能把布尔值进行转换
2.转换的时候可能存在溢出问题
public class Demo {
//类变量 static
static double salary = 2500;
String name;
int age;
//实例变量:从属于对象:如果不自信初始化,这个类型的默认值是 0 0.0
//布尔值:默认是false
//除了基本类型,其余的默认值都是null
public static void main(String[] args) {
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo();
Demo demo = new Demo();
System.out.println(demo.age);
System.out.println(demo.name);
System.out.println(salary);
}
}
public class Demo {
//修饰符,不存在先后顺序
//final定义常量,一般大写常量
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
public class Demo {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
System.out.println(a);
int b = a++; //执行完这行代码后,先给b赋值,再自增
System.out.println(a);
System.out.println(b);
int c = ++a;//执行完这行代码后,先自增,再给c赋值
System.out.println(a);
System.out.println(c);
}
}
public class Demo {
public static void main(String[] args) {
int a =10;
int b =20;
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
public class Demo {
public static void main(String[] args) {
// x ? y : z
// 如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score <60 ? "不及格" : "及格";
System.out.println(type);
}
}