变量命名规则

  • 所有变量、方法、类名:见名知意
  • 类成员变量、局部变量、方法名:首字母小写加驼峰规则
  • 常量:大写字母和下划线
  • 类名:首字母大写加驼峰规则

基本数据类型

// 八进制
int a1 = 010;  // a1=8
// 十六进制
int a2 = 0x10; //a2 = 16
// 二进制
int a3 = 0b10; //a3 = 2

System.out.println(10_000_000); //10000000

important:不要使用浮点数进行比较,有误差

// 浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用equals来判断。 ???
float i1 = .1f;
double d1 = .1;
System.out.println(i1==d1); // false
float i1 = 465464564654564645f;
float i2 = i1+1;
System.out.println(i2==i1); // true
char c1 = 'm';
char c2 = '中';
System.out.println((int)c1); // 强制转换 109(unicode编码)
System.out.println((int)c2); // 20013
System.out.println('\u0109'); // a   u0000-uFFFF

字符串

String a = new String("a");
String b = new String("a");
System.out.println(a.equals(b) ); //true
System.out.println(a==b); //false

String c = "a";
String d = "a";
System.out.println(c == d); //true
System.out.println(c.equals(d) ); //true
int a = 1000000000;
int b = 200;
System.out.println(a*b); // 溢出
System.out.println(a*(long)b); // 转换b,结果变成long。
System.out.println((long)a*b); // ******转换的是a,结果是long。不会溢出
System.out.println((long)(a*b)); // 是对(a*b)转换,溢出

int c = 400;
System.out.println((b/c)); // 0
System.out.println((double)b/c); // 或者b/(double)c 结果才是 0.5

对象

没有初始化的时候会设置为默认值;

  • byte、int、short、long: 0
  • char: '\u0000'对应的字符
  • float、double: 0.0
  • boolean: false
  • 其他非基本数据类型会设为null
public class A{
    String name;
    int age;
    public static void main(String[] args){
        A a = new A();
        System.out.print(a.name) // null
        System.out.print(a.age) // 0
    }
}

修饰符

final

使用final关键词修饰的变量一经初始化就不能更改

final int a = 1;

static

static 用于类成员变量

public class A{
    static String name;
    static int age;
    public static void function{
    }
}