Object

字符串相关

包装类

包装类为基本类型的各种操作提供了便利,与fp中的单子(monad)的概念类似。

Integer.parseInt("100") // 100
Integer.toString(100); // "100"
Integer.toHexString(255); // "ff"
Integer.MAX_VALUE; // 2147483647

// 由于Java没有运算符重载,应当使用equals进行比较

Integer m = 99999;
Integer n = 99999;
m == n; // false
m.equals(n); // true

Java Bean

如果一个Java类正确地实现了gettersetter,比如:

class Clazz {
    Type var;
    Type readOnlyVar;
    int age
    ...
    
    public Type getVar(){...}
    public void setVar(Type value){...}

    public Type getReadOnlyVar(){...} // 只有getter则为只读属性
    
    public boolean isAdult(){ return age > 18;} // 不一定要有对应的成员变量
    ...
}

那么称该类为一个Java Bean,通过使用java.beans.*可以操作Bean

BeanInfo info = Introspector.getBeanInfo(Clazz.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
    System.out.println(pd.getName());
    System.out.println("  " + pd.getReadMethod());
    System.out.println("  " + pd.getWriteMethod()); // 如果为只读属性则返回null
}

BigInteger和BigDecimal

BigInteger i1 = new BigInteger("1234567890");
i1.pow(5); // 2867971860299718107233761438093672048294900000
// 显然不能使用运算符
BigInteger i2 = i1.add(new BigIngeter("1")); // "1234567891" 
long l1 = i1.longValueExact(); // 相比于longValue能保证结果准确(否则抛异常)
float f2 = i2.floatValue(); // Infinity

枚举和记录

请随意转载