Introduction
In this lab, you will learn how to check if a number is negative in Java. We will start by writing a simple program using conditional statements to determine if a number is less than zero.
You will then explore how to apply this check to different numeric data types and learn how to include zero in your negative number check, providing a comprehensive understanding of handling negative values in Java.
Check if Number Is Less Than Zero
In this step, we will write a simple Java program to check if a given number is less than zero. This will introduce you to basic conditional statements in Java.
First, make sure you are in the
~/projectdirectory. You can use the commandcd ~/projectin the Terminal if needed.Create a new Java file named
NumberCheck.javain the~/projectdirectory. You can do this by right-clicking in the File Explorer on the left, selecting "New File", and typingNumberCheck.java.Open the
NumberCheck.javafile in the Code Editor and paste the following code:public class NumberCheck { public static void main(String[] args) { int number = -5; // Our number to check if (number < 0) { System.out.println("The number is less than zero."); } } }Let's look at the new parts:
int number = -5;: This line declares a variable namednumberof typeint(integer) and assigns it the value-5. Variables are like containers that hold data.if (number < 0): This is anifstatement. It checks if the condition inside the parentheses (number < 0) is true. If the condition is true, the code inside the curly braces{}is executed.System.out.println("The number is less than zero.");: This line will only be executed if thenumberis indeed less than 0.
Save the file (Ctrl+S or Cmd+S).
Now, compile the program using the
javaccommand in the Terminal:javac NumberCheck.javaIf there are no errors, a
NumberCheck.classfile will be created in the~/projectdirectory.Run the compiled program using the
javacommand:java NumberCheckYou should see the output:
The number is less than zero.This confirms that our
ifstatement correctly identified that the number -5 is less than zero.
Test with Different Numeric Types
In this step, we will explore how our program behaves with different types of numbers, specifically positive numbers and zero. This will help you understand how the if statement works when the condition is false.
Open the
NumberCheck.javafile in the Code Editor.Modify the value of the
numbervariable to a positive number, for example,10. The code should now look like this:public class NumberCheck { public static void main(String[] args) { int number = 10; // Our number to check if (number < 0) { System.out.println("The number is less than zero."); } } }Save the file (Ctrl+S or Cmd+S).
Compile the modified program in the Terminal:
javac NumberCheck.javaAgain, if compilation is successful, you won't see any output.
Run the compiled program:
java NumberCheckThis time, you should see no output. This is because the condition
number < 0(which is10 < 0) is false, so the code inside theifblock is skipped.Now, let's test with zero. Modify the value of the
numbervariable to0:public class NumberCheck { public static void main(String[] args) { int number = 0; // Our number to check if (number < 0) { System.out.println("The number is less than zero."); } } }Save the file.
Compile the program:
javac NumberCheck.javaRun the program:
java NumberCheckAgain, you should see no output. This is because
0 < 0is also false.
This step demonstrates that the code inside the if statement only runs when the condition is strictly true. In the next step, we will modify the condition to include zero in our check for non-positive numbers.
Include Zero in Negative Check
In the previous step, we saw that our program only prints the message if the number is strictly less than zero. Often, we might want to include zero when checking for non-positive numbers (numbers that are less than or equal to zero). In this step, we will modify our condition to achieve this.
Open the
NumberCheck.javafile in the Code Editor.Modify the condition in the
ifstatement fromnumber < 0tonumber <= 0. The<=operator means "less than or equal to".The updated code should look like this:
public class NumberCheck { public static void main(String[] args) { int number = 0; // Our number to check if (number <= 0) { // Changed condition System.out.println("The number is less than or equal to zero."); // Updated message } } }We also updated the message inside the
printlnstatement to be more accurate.Save the file (Ctrl+S or Cmd+S).
Compile the modified program in the Terminal:
javac NumberCheck.javaRun the compiled program:
java NumberCheckThis time, since
numberis0and the condition0 <= 0is true, you should see the output:The number is less than or equal to zero.Let's quickly test with a negative number again. Change the value of
numberback to-5:public class NumberCheck { public static void main(String[] args) { int number = -5; // Our number to check if (number <= 0) { // Changed condition System.out.println("The number is less than or equal to zero."); // Updated message } } }Save the file.
Compile the program:
javac NumberCheck.javaRun the program:
java NumberCheckYou should see the output:
The number is less than or equal to zero.This confirms that our updated condition works for both negative numbers and zero.
You have now successfully used the "less than or equal to" operator (<=) to include zero in your conditional check. This is a common requirement in programming when dealing with ranges of numbers.
Summary
In this lab, we learned how to check if a number is negative in Java. We started by writing a basic Java program using an if statement to determine if an integer is less than zero. This involved creating a Java file, writing the code with a variable and a conditional check, compiling the program using javac, and running it with java to observe the output. This initial step introduced the fundamental concept of using conditional logic (if statements) to evaluate numerical conditions in Java.



