Java mod examples

Both remainder and modulo are two similar operations; they act the same when the numbers are positive but much differently when the numbers are negative. In Java, we can use Math.floorMod() to describe a modulo (or modulus) operation and % operator for the remainder operation. See the result: | rem & +divisor| rem & -divisor …

Read more

Java Trap – Autoboxing and Unboxing

The below program is a typical Java trap, and also a popular Java interview question. It has no compiler error or warning but running very slow. Can you spot the problem? JavaSum.java package com.mkyong; import java.time.Duration; import java.time.Instant; public class JavaSum { public static void main(String[] args) { Instant start = Instant.now(); Long total = …

Read more