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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:21:08+00:00 2026-06-09T14:21:08+00:00

Possible Duplicate: Integer wrapper objects share the same instances only within the value 127?

  • 0

Possible Duplicate:
Integer wrapper objects share the same instances only within the value 127?

I have a question about the memory management in Java.

When I try the following code:

Integer a = 1;
Integer b = 1;
System.out.println(a==b); // this gives "true" 

However,

Integer a = 256;
Integer b = 256;
System.out.println(a==b); //this gives "false"

Why?

Thanks very much.

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

    That is because “autoboxing” uses Integer.valueOf, and Integer.valueOf keeps a cache of Integer objects for small integer values. Here’s what the JLS says:

    “If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.” JLS 5.1.7.

    When you use the == operator to compare a pair of Integer objects, it is actually comparing the object references. So your get true if boxing gave you the same cached Integer object, and false if it didn’t. Note that the JLS guarantees this behaviour for the ranges stated, but it also permits an implementation of the valueOf method to cache a wider range of values.

    The bottom line is that you should use equals(Object) to compare Integer objects … unless you are really trying to test if they are the same object.


    According to what I read, “Integer” should create an “object” in the heap, thus the two objects should be the same.

    If your code explicitly does a new Integer(...), it is guaranteed to create a new Integer object. However, autoboxing uses Integer.valueOf(...), and that is where the caching behaviour is implemented.

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

Sidebar

Related Questions

Possible Duplicate: Integer wrapper objects share the same instances only within the value 127?
Possible Duplicate: Inconsistent behavior on java's == Integer wrapper objects share the same instances
Possible Duplicate: Why is == true for some Integer objects? I have code fragment
Possible Duplicate: Java question about autoboxing and object equality / identity Integer i1 =
Possible Duplicate: Why does this integer division yield 0? I have a C header
Possible Duplicate: Confused about C macro expansion and integer arithmetic A riddle (in C)
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Best way to detect integer overflow in C/C++ i have tried to
Possible Duplicate: How do I check to see if a value is an integer
Possible Duplicate: how to get bit by bit data from a integer value in

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.