What is the modulo operator for longs in Java?

非 Y 不嫁゛ 提交于 2019-12-01 02:44:47

The % operator does work for longs. It sounds like you may have forgotten to stick L at the end of a numeric literal, as in 123456789L. Can we see your code?

You can only have an integer up to 2 147 483 647. If you want to go bigger than that, say 3 billion, you must specify it to be a long

class Descartes {
    public static void main(String[] args) {
        long orig = Long.MAX_VALUE;
        long mod = orig % 3000000000; // ERROR 3000000000 too big
        long mod = orig % 3000000000L; // no error, specified as a long with the L
    }
}

Keep in mind that you can use capital OR lowercase L, but it's advisable to use capital, since the lowercase looks remarkably similar to the number 1.

You can also try working with the BigInteger class which has a remainder() method that works similarly to %.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!