GH1995 / articles

blog
https://gh1995.github.io
2 stars 0 forks source link

Java核心类 #24

Open GH1995 opened 4 years ago

GH1995 commented 4 years ago

String

image image

GH1995 commented 4 years ago

StringBuilder

public class Main {
    public static void main(String[] args) {
        Adder adder = new Adder();
        adder.add(3)
             .add(5)
             .inc()
             .add(10);
        System.out.println(adder.value());
    }
}

image

GH1995 commented 4 years ago

StringJoiner

分隔符拼接数组

public class Main {
    public static void main(String[] args) {
        String[] names = {"Bob", "Alice", "Grace"};
        var sj = new StringJoiner(", ");
        for (String name : names) {
            sj.add(name);
        }
        System.out.println(sj.toString());
    }
}
public class Main {
    public static void main(String[] args) {
        String[] names = {"Bob", "Alice", "Grace"};
        var sj = new StringJoiner(", ", "Hello ", "!");
        for (String name : names) {
            sj.add(name);
        }
        System.out.println(sj.toString());
    }
}

image

GH1995 commented 4 years ago

JavaBean

image

public class Main {
    public static void main(String[] args) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(Person.class);
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            System.out.println(pd.getName());
            System.out.println("  " + pd.getReadMethod());
            System.out.println("  " + pd.getWriteMethod());
        }
    }
}
GH1995 commented 4 years ago

枚举类

image

GH1995 commented 4 years ago

常用工具类

Math.abs(-100); // 100

Random r = new Random();
r.nextInt(); // 2071575453,每次都不一样

SecureRandom sr = new SecureRandom();
System.out.println(sr.nextInt(100));