Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:07:04+00:00 2026-06-05T17:07:04+00:00

Is there an elegant way in java to check if an int is equal

  • 0

Is there an elegant way in java to check if an int is equal to, or 1 larger/smaller than a value.

For example, if I check x to be around 5. I want to return true on 4, 5 and 6, because 4 and 6 are just one away from 5.

Is there a build in function to do this? Or am I better off writing it like this?

int i = 5;
int j = 5;
if(i == j || i == j-1 || i == j+1)
{
    //pass
}
//or
if(i >= j-1 && i <= j+1)
{
    //also works
}

Of course the above code is ugly and hard to read. So is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:07:05+00:00Added an answer on June 5, 2026 at 5:07 pm

    Find the absolute difference between them with Math.abs

    private boolean close(int i, int j, int closeness){
        return Math.abs(i-j) <= closeness; 
    }
    

    Based on @GregS comment about overflowing if you give Math.abs a difference that will not fit into an integer you will get an overflow value

    Math.abs(Integer.MIN_VALUE - Integer.MAX_VALUE) //gives 1
    

    By casting one of the arguments to a long Math.abs will return a long meaning that the difference will be returned correctly

    Math.abs((long) Integer.MIN_VALUE - Integer.MAX_VALUE) //gives 4294967295
    

    So with this in mind the method will now look like:

    private boolean close(int i, int j, long closeness){
        return Math.abs((long)i-j) <= closeness; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any elegant way to make Java method located within parent class return
Is there an elegant way in Java to code: if (10 < x <
In java, Is there a elegant way to Generate Excel spreadsheet from List?
In Java, is there a short elegant way to combine multiple predicates (Guava Predicate)
In java is there any elegant way to compare two different StringBuilders and copy
Is there an easy and elegant way to convert an unsigned byte value to
In Java, is there an elegant way to detect if an exception occurred prior
Is there a more elegant way of doing this in Java? String value1 =
Is there an elegant way to automatically fire memory warnings to my Java application
Is there an elegant way to do this in MySQL: SELECT (subquery1) AS s1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.