Member-only story
Tackling the ‘Are the Numbers Equal?’ Java Programming Problem
A Beginner’s Guide to Writing a Function That Compares Two Integers for Equality
Introduction
In this article, we’ll provide a step-by-step guide on how to write a function that returns true when two integers are equal and false otherwise.
Step 1: Define the Function
First, we need to define the function. In Java, a function is a block of code that performs a specific task. We’ll name our function isSameNum
and specify that it takes two integer arguments:
public static boolean isSameNum(int x, int y) {
// code goes here
}
Step 2: Compare the Integers
Next, we need to compare the two integers for equality. In Java, we can use the ==
operator to check if two values are equal:
public static boolean isSameNum(int x, int y) {
boolean isEqual = (x == y);
return isEqual;
}
Step 3: Return the Result
Finally, we need to return the result. In Java, we use the return
keyword to return a value from a function:
public static boolean isSameNum(int x, int y) {
boolean isEqual = (x == y);
return…