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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:20:53+00:00 2026-05-26T00:20:53+00:00

I was wondering if java automatically turns a Integer into an int when comparing

  • 0

I was wondering if java automatically turns a Integer into an int when comparing to an int? Or will the == try and compare references on primitives?

Is this always true or do I need to do i.intValue()==2?

Integer i = Integer.valueOf(2);
if (i==2){
//always?
}
  • 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-26T00:20:54+00:00Added an answer on May 26, 2026 at 12:20 am

    Yes, when comparing int using == arguments will be unboxed if necessary.

    Relevant section from the Java Language Specification:

    15.21.1 Numerical Equality Operators == and !=

    If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.

    Note that binary numeric promotion performs value set conversion (§5.1.13) and unboxing conversion (§5.1.8). Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.

    Same applies for <, <=, >, >= etc, as well as +, -, * and so on.

    So,

    System.out.println(Integer.valueOf(17) == 17);
    

    prints true 🙂

    but you can compare two equal strings with == and sometimes get true or fals depending on how the strings were pooled…

    Right, and there is actually a similar situation for Integers as well.

    When boxing (transforming int to Integer) the compiler uses a cache for small values (-128 – 127) and reuses the same objects for the same values, so perhaps a bit surprising, we have the following:

    System.out.println(Integer.valueOf(100) == Integer.valueOf(100)); // prints true
    System.out.println(Integer.valueOf(200) == Integer.valueOf(200)); // prints false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since the release of Adobe AIR I am wondering why Java Web Start has
I'm wondering if a Java library can be called from a VB.net application. (A
I was wondering if in Java I would get any odd behaviour if I
I was wondering if something exists (in Java world) able to take an snapshot
I was wondering about what makes the primary Java compiler (javac by sun) so
I've been wondering about the performance improvements touted in Java SE 6 - is
I was wondering what are the benefits of using anything else but Java for
I'm working in Java with XML and I'm wondering; what's the difference between an
I'm wondering if I'm missing something about Java Beans. I like my objects to
I'm wondering why the assert keyword is so underused in Java? I've almost never

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.