java编程技巧

欢迎提出建议指出错误互相交流。

1.统计对象数量,比如统计一共发射了多少颗子弹。

    

public class Bullet {

    public static int count = 0;

    public Bullet() {
        count++;
    }

}

2.判断一个浮点数小数位有没有值。

public boolean isInt(float f) {
        return f == (int) f;
    }

3.聪明你的一定想到了,获取一个浮点数的小数部分

public static float getZeroPoint(float f) {
        return f - (int) f;
    }

4.判断奇偶

int i = (int) (Math.random() * 10);
        System.out.println("i is  " + i + " , " + ((i & 1) == 0 ? "偶" : "奇"));

 5.判断n是否是2的正整数次幂(2的正整数次幂应该是最高位是1,其他位都是0)

public boolean is2n(int n) {
        return n <= 0 ? false : (n & (n - 1)) == 0;
    }

 6.判断某字符串(单字符)是否是中文

str.matches("[\u4e00-\u9fa5]")
原文地址:https://www.cnblogs.com/xirtam/p/3145373.html