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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:44:59+00:00 2026-06-11T20:44:59+00:00

I am a java novice and so confused by the following example. Is it

  • 0

I am a java novice and so confused by the following example. Is it okay to think that “==” sign will compare the values between Integers and “autoboxed” Integers from int, and compare reference address between Integers?

What about doubles and 0/0?

import edu.princeton.cs.introcs.*;

public class Autoboxing {

    public static void cmp(Integer first, Integer second) {
        if (first < second)
            StdOut.printf("%d < %d\n", first, second);
        else if (first == second)
            StdOut.printf("%d == %d\n", first, second);
        else if (first > second)
            StdOut.printf("%d > %d\n", first, second);
        else
            StdOut.printf("%d and %d are incomparable\n", first, second);
    }

    public static void main(String[] args) {
        cmp(new Integer(42), 43);
        cmp(new Integer(42), new Integer(42));
        cmp(43, 43);
        cmp(142, 142);

        Integer a0 = 1000;
        int b0 = 1000;
        Integer c0 = 1000;
        StdOut.println("a0==b0?" + (a0==b0));
        StdOut.println("a0==c0?" + (a0==c0));
        StdOut.println("b0==c0?" + (b0==c0));

        double x1 = 0.0, y1 = -0.0;
        Double a1 = x1, b1 = y1;
        StdOut.println(x1 == y1);
        StdOut.println(a1.equals(b1));

        double x2 = 0.0/0.0, y2 = 0.0/0.0;
        Double a2 = x2, b2 = y2;
        StdOut.println(x2 != y2);
        StdOut.println(!a2.equals(b2));
    }

}

The result is:

42 < 43
42 and 42 are incomparable
43 == 43
142 and 142 are incomparable
=====
a0==b0?true
a0==c0?false
b0==c0?true
=====
true
false
=====
true
false
  • 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-11T20:45:00+00:00Added an answer on June 11, 2026 at 8:45 pm

    Unboxing will be happing when arithmetic operators, comparison operators appear.

    eg:

    Integer a = 10;
    a = a+10; //1.unboxing a to int 2.calculate a+10 3.boxing 20 to Integer.
    System.out.print(a > 10); //1.unboxing a to int 2. compare
    

    But when == appear, it depends.

    If boxing type appear on both side, it will compare the reference.But if base type appear on one side, and the other side is a boxing type, the boxing type will unboxing to base type.

    eg:

    Integer a = new Integer(129);
    Integer b = new Integer(129);
    System.out.println(a == b); // compare reference return false
    System.out.println(a == 129); // a will unboxing and compare 129 == 129 return true
    

    PS: In Java.lang.Integer Cache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive) as required by JLS.
    See source code

    So:

    Integer a = 127;
    Integer b = 127; //cached, the same as b a==b return ture
    
    Integer c = 129;
    Integer d = 129; // not cached, c==d return false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am requesting links to sources/material that will help me with the following problem
As a very novice Java programmer, I probably should not mess with that kind
I am a novice to java. Im trying to write a two-dementional array that
I am a novice programmer in (Java/C++/C#) and I also know Python. I am
I'm relatively novice when it comes to C++ as I was weened on Java
am working on a java(tomcat) app. that sometimes writes to stdout. But I notice
JAVA : is there a difference between the two references p && pp? PrintStream
Java noob question: Consider the following C array and initializer code: struct { int
Java Internationalization Rules . It says to replace s1.compareTo(s)==0 with Collator.compare(s1,s2)<0 why <0 ?
I am a fairly novice Java programmer and I am currently working on a

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.