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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:47:04+00:00 2026-06-05T10:47:04+00:00

I am confused with one trivial thing – passing parameters to the method and

  • 0

I am confused with one trivial thing – passing parameters to the method and changing their values… I’ll better give you some code:

public class Test {
  public static void main(String[] args) {
    Integer val = new Integer(41);
    upd(val);
    System.out.println(val);

    Man man = new Man();
    updMan(man);
    System.out.println(man.name);
  }

  static void upd(Integer val) {
    val = new Integer(42);
  }

  static void updMan(Man man) {
    man.name = "Name";
  }

  static class Man {
    String name;
  }
}

Could you explain why the Integer object I’ve passed is not updated while the Man object is? Aren’t the Integer and Man objects passed by reference (due to their non-primitive nature)?

  • 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-05T10:47:06+00:00Added an answer on June 5, 2026 at 10:47 am

    Because for Integer your are creating a new Object. For Man you just change one of its value, the object man stays the same.

    Consider the following:

    static void updMan(Man man) {
      man = new Man();
      man.name = "Another Man";
    }
    

    This would also not change your initial man.

    –EDIT

    You can “simulate” the mutability of Integer by this:

    static void upd(Integer val) {
        try {
            Field declaredField = val.getClass().getDeclaredField("value");
            declaredField.setAccessible(true);
            declaredField.set(val, 42);
        } catch (Exception e) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm confused which one is better. ASPX: <asp:HyperLink ID=HyperLink1 runat=server>HyperLink</asp:HyperLink> Code: String url =
I'm totally confused which one to use and when, first thing I do when
I'm creating a small prime number program, and am confused about one thing. I
I am learning from code, and I am get confused by one of its
Very confused about this one. This code in views.py works, but only when I'm
I am most confused with this one, so i d better ask the experts!
I'm a bit confused with one thing, look: Let's say we have source tree
I'm little bit confused with one thing about memory stack. From what I know
I'm pretty confused by one of the subtleties of the vimscript extend() function. If
I'm quite new to MySQL and I'm confused with one problem: I have one

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.