Java doesn't throw an error if you increase a number after its maximum value. If you wish to have this behaviour, you could use the Math.addExact(long x, long y) method from Java 8. This method will throw an ArithmeticException if you pass the Long.MAX_VALUE.

The reason why Java doesn't throw an exception and you receive negative numbers has to do with the way numbers are stored. For a long primitive the first byte is used for indicating the sign of the number (0 -> positive, 1 -> negative), while the rest are used for the numeric value. This means that Long.MAX_VALUE which is the biggest positive value will be stored as 01111...111 (0 followed by 63 bits of 1). Since you add a number to Long.MAX_VALUE you will start receiving negative integers since the sign byte changes to 1. This means you have an numeric overflow, but this error isn't thrown by Java.

Answer from Slimu on Stack Overflow
🌐
Coderanch
coderanch.com › t › 370696 › java › Test-negative-long
Test for negative long? (Java in General forum at Coderanch)
programming forums Java Mobile ... staff, including ... ... Check to see if the long is less than zero. Example long myLong = -5L; System.out.println(myLong < 0); Prints "true" if negative....
🌐
Blogger
javarevisited.blogspot.com › 2013 › 01 › how-to-check-if-number-is-positive-or-negative-java-example.html
How to check if a Number is Positive or Negative in Java? Solution Example
Use relational operator to check if a number is positive or not, if number >=0 means a number is positive if number<0 means number is negative in Java this should work for double and float as well but I haven't tested it for all values.
🌐
Baeldung
baeldung.com › home › java › java numbers › check if a number is positive or negative in java
Check if a Number Is Positive or Negative in Java | Baeldung
January 8, 2024 - Per definition, whether a number is positive or negative depends on the result of the comparison to zero. Therefore, we can use Java’s “greater than (>)” and “less than (<)” operators to solve the problem.
🌐
TutorialKart
tutorialkart.com › java › java-check-if-number-positive-or-negative
Java Program - Check if a Number is Positive or Negative
May 16, 2020 - To check if a number is negative, use comparison operator: less than (<) that accepts the number and zero as operands. If number is less than zero, it returns true, else it returns false.
🌐
Stack Overflow
stackoverflow.com › questions › 71485879 › getting-a-negative-number-for-a-long
java - Getting a negative number for a long - Stack Overflow
Use Long.unsignedCompare(). long a1 = 1; long a2 = -1; if (a2 < a1) { // a2 is less which is what you would expect System.out.println(a2); } if (Long.compareUnsigned(a1,a2) < 0) { // here, a1 is less System.out.println(a1); }
🌐
w3resource
w3resource.com › java-exercises › conditional-statement › java-conditional-statement-exercise-27.php
Java - Check the number is negative, zero, or positive
Write a Java program that reads an integer and check whether it is negative, zero, or positive. ... import java.util.Scanner; public class Exercise27 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input a number: "); int n = in.nextInt(); if (n > 0) { System.out.println("Number is positive"); } else if (n < 0) { System.out.println("Number is negative"); } else { System.out.println("Number is zero"); } } }
🌐
Javatpoint
javatpoint.com › java-program-to-check-if-a-number-is-positive-or-negative
Java Program to Check if a Number is Positive or Negative - Javatpoint
longValue() max() min() mod() modInverse() modPow() negate() nextProbablePrime() not() or() pow() probablePrime() setBit() shiftLeft() shiftRight() signum() subtract() testbit() toByteArray() toString() valueOf() xor() Java BigInteger sqrt() method · Java Boolean class ·
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › java-program-to-check-if-number-is-positive-or-negative
Java Program to check if Number is Positive or Negative
import java.util.Scanner; public class Demo { public static void main(String[] args) { int number; Scanner scan = new Scanner(System.in); System.out.print("Enter the number you want to check:"); number = scan.nextInt(); scan.close(); if(number > 0) { System.out.println(number+" is positive number"); } else if(number < 0) { System.out.println(number+" is negative number"); } else { System.out.println(number+" is neither positive nor negative"); } } }
🌐
Sanfoundry
sanfoundry.com › java-program-check-given-integer-positive-negative
Positive or Negative Number in Java - Sanfoundry
May 23, 2022 - If it is less than zero than it is negative and if it is zero than it is neither poistive nor negative. Here is the source code of the Java Program to Check if a Given Integer is Positive or Negative.
🌐
Programiz
programiz.com › java-programming › examples › positive-negative
Java Program to Check Whether a Number is Positive or Negative
Online Python Online JavaScript ... { public static void main(String[] args) { double number = 12.3; // true if number is less than 0 if (number < 0.0) System.out.println(number + " is a negative number."); // true if number is greater than 0 else if ( number > 0.0) ...
🌐
Vultr Docs
docs.vultr.com › java › examples › check-whether-a-number-is-positive-or-negative
Java Program to Check Whether a Number is Positive or Negative | Vultr Docs
September 27, 2024 - public class NumberCheck { public static void checkNumber(int number) { if (number > 0) { System.out.println(number + " is positive."); } else if (number < 0) { System.out.println(number + " is negative."); } else { System.out.println(number + " is zero."); } } public static void main(String[] args) { checkNumber(10); // Example usage checkNumber(-5); // Example usage checkNumber(0); // Example usage } } Explain Code · This code defines a method checkNumber that prints whether a number is positive, negative, or zero. By passing different values to checkNumber in the main method, different outcomes are demonstrated.
🌐
Newtum
blog.newtum.com › positive-negative-number-java
Positive or Negative Number in Java - Newtum
July 17, 2024 - Enter a number: -3 -3.0 is a negative number. The Integer.signum method in Java returns the sign of the specified integer. It returns -1 if the number is negative, 0 if the number is zero, and 1 if the number is positive.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-check-if-a-given-integer-is-positive-or-negative
Java Program to Check if a Given Integer is Positive or Negative - GeeksforGeeks
January 22, 2026 - Integer.signum() returns 1 for positive numbers, -1 for negative numbers, and 0 if the number is zero. In the main() method, the returned value is stored in result and checked using if-else conditions.
🌐
CodingTechRoom
codingtechroom.com › question › java-long-negative-overflow
How Can a Long Variable in Java Become Negative Without Exceeding Max Value? - CodingTechRoom
In Java, the `long` data type is a 64-bit signed integer capable of representing values from -2^63 to 2^63-1. When performing arithmetic operations, particularly addition, a long variable can overflow and wrap around to a negative value, even if it starts out within the maximum allowable range.
🌐
W3Schools
w3schools.com › java › java_howto_pos_or_neg.asp
Java How To Find Out if a Number is Positive or Negative
Java Examples Java Videos Java ... is a positive number."); } else if (myNum < 0) { System.out.println("The value is a negative number."); } else { System.out.println("The value is 0."); } Try it Yourself »...
🌐
Red-Green-Code
redgreencode.com › home › using integers and longs in java
Using Integers and Longs in Java - Red-Green-Code
October 7, 2015 - If we want to store signed (both positive and negative) integers, then half of the available values will be positive and half will be negative. Java defines a set of Primitive Data Types and the number of bits allocated to each one.
🌐
TutorialsPoint
tutorialspoint.com › Java-program-to-find-if-the-given-number-is-positive-or-negative
Java program to check whether a number is positive or negative
March 13, 2020 - To check whether the specified ... less than 0 is considered a negative number, we can use Java conditional statements like if-else-if blocks or ternary operators....