DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Is This the Correct Object?

Is This the Correct Object?

MVB Dan Newton shows off a new issue he came across at work. Even the pros can get stumped sometimes.

Dan Newton user avatar by
Dan Newton
·
Mar. 14, 17 · Java Zone · Tutorial
Like (4)
Save
Tweet
8.55K Views

Join the DZone community and get the full member experience.

Join For Free

This is something I came across at work that wasn’t working as I expected. After a little playing around it seemed pretty obvious but even after a few years of working with Java this wasn’t a situation I came across before.

Now what happened? Its way easier to show you an example first.

public class AssigningObjects {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = a;
        Integer c = 2;
        a = 3;
        a = c;
        System.out.println("a = " + a); // prints 2
        System.out.println("b = " + b); // prints 1
        System.out.println("c = " + c); // prints 2

        c = 4;
        System.out.println("a = " + a); // prints 2
        System.out.println("c = " + c); // prints 4

        c = null;
        System.out.println("a = " + a); // prints 2
        System.out.println("c = " + c); // prints null
    }
}

So the problem I had… I thought that if b = a and a = c then b = c, so by changing c‘s value b will also change. But this is not the case. What actually happens is a refers to an underlying object (which is the Integer 1) and by assigning b to a, b now also refers to the same object. When a is assigned to c the underlying object a refers to has now changed, but this does not affect b which will still have the value 1 as the object it is pointing to has not changed.

Anyway this is just something small that I found interesting as I never actually ran into this situation before and when I told my colleague I wanted to tell him something interesting he told me to go away… so I told you instead!

Object (computer science)

Published at DZone with permission of Dan Newton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Gain Competitive Advantage in Software Development With Innovative Technology?
  • Getting Started Building on the NEAR Network with Infura
  • Implementing HIPAA Technical Safeguards in Your API Platform
  • API Security Weekly: Issue 165

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo