Java基本类型


2019-11-03  19:03:48  by冲冲

1、两个float型相减丢失精度,如何解决?

使用BigDemical装饰器模式

public class Test {
    public static void main(String[] args) {
        float a = 2.030944f;
        float b = 1.001085f;
        System.out.println(a - b);// 1.0298591

        BigDecimal c = new BigDecimal(Float.toString(a));
        BigDecimal d = new BigDecimal(Float.toString(b));

        BigDecimal resultSub = c.subtract(d);       //减法
        BigDecimal resultAdd = resultSub.add(d);    //加法
        BigDecimal resultMul = c.multiply(d);       //乘法
        BigDecimal resultDiv = c.divide(d);         //除法

        System.out.println(resultSub.floatValue());//1.029859
        System.out.println(resultAdd.floatValue());//2.030944
    }
}

2、方法的值传递和对象传递

public class Test {
    public void change(String str, char ch[]) {
        str = "test ok";
        ch[0] = 'g';
    }

    public static void main(String args[]) {
        String str = new String("good");
        char[] ch = {'a', 'b', 'c'};
        Test ex = new Test();
        ex.change(str, ch);
        System.out.print(str + " and " + ch); //good and gbc
    }
}

3、Integer类的==和equals()

public class Test {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        Integer d = 3;
        Integer e = 321;
        Integer f = 321;
        Long g = 3L;
        Long h = 2L;
        // 当Integer处于-128~127,使用内存栈创建值,并将对象指向该值;当不处于该区间,在堆重新new对象。
        System.out.println(c == d);             //true
        System.out.println(e == f);             //false
        // 自动拆箱
        System.out.println(c == (a + b));       //true
        System.out.println(c.equals(a + b));    //true
// 包装类的equals()方法会使用instanceof判断类型 System.out.println(g == (a + b)); //true System.out.println(g.equals(a + b)); //false System.out.println(g.equals(a + h)); //true } }

4、==和equals()

public class Test {
    public static void main(String[] args) {
        Long u = 127L;
        Long v = 127L;
        System.out.println(u == v);       //true
        //[-128,127]在栈内存创建,并指向对象;其余的重新new对象
        u = 128L;
        v = 128L;
        System.out.println(u == v);       //false
        String x = new String("hello");
        String y = "hello";
        System.out.println(x.equals(y));  //true
        System.out.println(x == y);       //false
        x = x.intern();
        System.out.println(x==y);         //true
}

5、多线程的start()和run()

public class Test {
    public static void main(String[] args) {
        Thread t = new Thread() {
            public void run() {
                pong();
            }
        };
        t.run();
        System.out.print("ping");
        //线程要使用start()开启,仅仅调用run()依然在本线程运行,输出结果是pongping
    }

    private static void pong() {
        System.out.print("pong");
    }
}

6、switch

public class Test {
    public static void main(String[] args) {
        System.out.println(getValue(2));
        //输出结果10
        //如果case没有break,则会继续往下执行
    }

    public static int getValue(int i) {
        int Test = 0;
        switch (i) {
            default:
                System.out.println("default");
            case 1:
                Test = Test + i;
            case 2:
                Test = Test + i * 2;
            case 3:
                Test = Test + i * 3;
        }
        return Test;
    }
}

7、变量作用域

public class MeaningOfThis {
    public final int value = 4;

    public void doIt() {
        int value = 6;
        Runnable r = new Runnable() {
            public final int value = 5;

            public void run() {
                int value = 10;
                System.out.println(this.value);
            }
        };
        r.run();
    }

    public static void main(String... args) {
        MeaningOfThis m = new MeaningOfThis();
        m.doIt();
        //输出结果5,因为this指向对象r
    }
}

8、父类和子类的构造方法关系

class Person {
    String name = "No name";

    public Person(String nm) {
        name = nm;
    }
}

class Employee extends Person {
    String empID = "0000";

    public Employee(String id) {
        empID = id;
    }
}

public class Test {
    public static void main(String args[]) {
        Employee e = new Employee("123");
        System.out.println(e.empID);
        //编译报错
    }
}

修改方案1

class Employee extends Person {
    String empID = "0000";

    public Employee(String id) {
        super("Railway Employee");  //父类没有默认的空参构造器,所以必须调用带参构造器
        empID = id;
    }
}

 修改方案2

class Person {
    String name = "No name";

    public Person(String nm) {
        name = nm;
    }

    public Person() {}  //给父类添加不带参数的构造器,子类就可以隐式调用
}

9、基本数据类型溢出

public class Test {
    public static void main(String[] args) {
        int a = Integer.MAX_VALUE;
        long b = a + 1;
        System.out.println(a);//2147483647
        System.out.println(b);//-2147483648
    }
}
01-03 16:03