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

  • SEARCH
  • Home
  • 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 8551371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:10:52+00:00 2026-06-11T14:10:52+00:00

Possible Duplicate: Why Java does not see that Integers are equal? I have 2

  • 0

Possible Duplicate:
Why Java does not see that Integers are equal?

I have 2 integers that I assign from the same argument.

One of the integers I decrease the value by 1 then increment the value by 1.

When I compare them again they are not always equal.

This is from my book, can someone please explain, I cannot understand my books explanation.

class Test{

    public static void main(String[] args){
        Integer i = Integer.parseInt(args[0]);
        Integer j = i;
        System.out.println("1:" + i + ", j:" + j);
        i--;
        System.out.println("2:" + i + ", j:" + j);
        i++;
        System.out.println("3:" + i + ", j:" + j);
        System.out.println((i==j));

    }
}

Output:
Input 256 as argument

1:256, j:256
2:255, j:256
3:256, j:256
false

Thank you for your consideration.

  • 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-11T14:10:53+00:00Added an answer on June 11, 2026 at 2:10 pm

    You are comparing two references that are not the same because of the ++– (New Objects Created). The way to compare two Integer objects equals() method. equals() will check Integer’s inner state. Check this code:

        Integer i = 256;
        Integer j=i;
        System.out.println(i==j);         //True  (Because we are pointing the same object)
        i--;
        i++;        
        System.out.println(i==j);         //False (Because reference has changed)
        System.out.println(i.equals(j));  //True  (Because the inner state is the same)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int
Possible Duplicates: Why does Java allow multiple inheritance from interfaces but not from abstract/concrete
Possible Duplicate: Java: Array of primitive data types does not autobox I've been doing
Possible Duplicate: Java generics and array initialization How does one instantiate an array of
Possible Duplicate: Size-limited queue that holds last N elements in Java Does java include
Possible Duplicate: When does Java's Thread.sleep throw InterruptedException? I saw we have to catch
Possible Duplicate: How does JavaScript .prototype work? Coming from Java background, I'm trying to
Possible Duplicate: Java float division precision I'm trying to fix a bug in one
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object

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.