Core Java: Integer division truncates toward zero, and % keeps the dividend's sign

Integer division in Java truncates toward zero rather than flooring, so -7/2 is -3, not -4. The % operator always takes the sign of the dividend, not the divisor.

Code
int a = 7 / 2;
int b = -7 / 2;
int c = 7 % -2;
int d = -7 % 2;
System.out.println("7 / 2 = " + a);
System.out.println("-7 / 2 = " + b);
System.out.println("7 % -2 = " + c);
System.out.println("-7 % 2 = " + d);
Output
7 / 2 = 3
-7 / 2 = -3
7 % -2 = 1
-7 % 2 = -1
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms