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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:11:50+00:00 2026-05-26T19:11:50+00:00

The expressions used in the following Java code are nuts and pretty much unacceptable

  • 0

The expressions used in the following Java code are nuts and pretty much unacceptable though I tested them just for a general purpose and obtained some unexpected result. The simple code snippet goes following.

package wrapper;

final public class Main 
{
    public static void main(String[] args) 
    {
        Integer j1 = 127;
        Integer j2 = 127;
        System.out.println(j1==j2); //returns true!!!

        Integer k1 = 128;
        Integer k2 = 128;
        System.out.println(k1==k2); //returns false!!!

        Integer w1 = -128;
        Integer w2 = -128;
        System.out.println(w1==w2); //returns true!!!

        Integer m1 = -129;
        Integer m2 = -129;
        System.out.println(m1==m2); //returns false!!!
    }
}

Integer j1 = 127;
Integer j2 = 127;
System.out.println(j1==j2);

The above code obviously displays true hence, there is no question about it.


Integer k1 = 128;
Integer k2 = 128;
System.out.println(k1==k2);

The above code is expected to display true on the console but surprisingly, it shows false. Why?


Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1==w2); 

This code displays true and no question about it.


Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1==m2);

The above code again displays false though it is expected to return true. Why?

  • 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-26T19:11:50+00:00Added an answer on May 26, 2026 at 7:11 pm

    Integers from the range -128..127 are cached, so j1 and j2 from:

    Integer j1 = 127;
    Integer j2 = 127;
    

    point to the same object.

    Of course, this is a JVM implementation thing, so you should never assume they are cached. For comparing reference types, never use == but use their equals(...) method instead.

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

Sidebar

Related Questions

I'm a relative novice to regular expressions (although I've used them many times successfully).
Can COMMON Table expressions be used to avoid having SQL Server perform the following
I've used both of the following Regular Expressions for testing for a valid email
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
I have the following regular expression which is used to give me the tags
I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I want
It's been several years since I have used regular expressions, and I was hoping
I have not used many lambda expressions before and I ran into a case
I have always used || (two pipes) in OR expressions, both in C# and
The C++ comma operator is used to chain individual expressions, yielding the value of

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.