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 6053735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:04:28+00:00 2026-05-23T08:04:28+00:00

My program includes the following code segment, which do sth based on whether two

  • 0

My program includes the following code segment, which do sth based on whether two variables, temp.get(j1) and temp(j2) are equivalent or not.

  for (int j1 =0; j1<2;j1++)
  {
      for (int j2 =0; j2<2;j2++)
      {
       System.out.println("j1="+j1+"j2="+j2+" "+temp1.get(j1)+"----"+temp2.get(j2));
       int xyz = temp1.get(j1)-temp2.get(j2);
       System.out.println("the difference is "+ xyz);

       if (temp1.get(j1)==temp2.get(j2))
       {
           System.out.println("find match");
       }
      }
  }

The program prints out sth like

j1=0j2=0 7698380—-7698380

the difference is 0

Even the two values, temp1.get(j1) and temp2.get(j2) do overlap, the “if” part just did not go through. If I change the if (temp1.get(j1)==temp2.get(j2)) to
if (xyz == 0)

Then the result will look like

j1=0j2=0 7698380—-7698380

the difference is 0

find match
`

I think the two conditions for controlling the if loop should be the same, why the result if so different? Thanks a lot for the answer.

  • 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-05-23T08:04:28+00:00Added an answer on May 23, 2026 at 8:04 am

    Hm, does temp1.get(j1) return an Integer instead of an int? If so, then the Integers in question are not identical (i.e. not ==). Even though they are equal from the point of view of equals().

    EDIT:

    One way to solve this issue is to make temp1 and temp2 return an int instead of an Integer. You can use simple autoboxing (or, autounboxing in this case) here.

    Say for example that your class is SithLord and your integer property is jediKills. Currently your class looks like this:

    public class SithLord {
        private Integer jediKills;
        public Integer getJediKills() { return jediKills; }
    }
    

    All you have to do is change it to this:

    public class SithLord {
        private Integer jediKills;
        public int getJediKills() { return (jediKills == null ? 0 : jediKills); }
    }
    

    And Java will automatically cast the variable to an int for you. (Note that I’m assuming here that a null Integer is a zero. That assumption may or may not work for your class. But for the class above it does, as a Sith lord would certainly advertise his kills.)

    Alternatively you can do this:

    public class SithLord {
        private int jediKills;
        public int getJediKills() { return jediKills; }
    }
    

    Either way the point is that the class needs to return an int if you want to use the == operator here.

    As to why this may have worked in the past for you, I can only speculate since I haven’t seen the code. But it’s safe to say that == would be true iff the references on either side of the == are one and the same. That can happen if you’re exercising that sort of control over the objects. But if you autobox primitive types you give that up, because Java will create new wrappers around the primitive types for you each time you autobox. And because they’re new, they won’t be the same reference.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand a program, which includes the following definition for a
I have the following c program which launches a Gtk Program on ubuntu: #include
I wrote a program which includes writing and reading from database. When I run
Using the following code snippet as an illustration to my question: // #includes and
I have the following code that executes a C++ program and outputs it :
I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>
I compiled the following code: // Triangle.cpp // Our first OpenGL program that will
The following code catches the SIGINT signal only once and then interrupts (program exists):
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,

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.