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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:04:16+00:00 2026-06-12T10:04:16+00:00

Hi I read this question on Stack overflow, and tried to do an example.

  • 0

Hi I read this question on Stack overflow, and tried to do an example.

I had the below code:

public static void main(String[] args){
     int i = 5;
     Integer I = new Integer(5);

     increasePrimitive(i);
     increaseObject(I);

     System.out.println(i); //Prints 5 - correct
     System.out.println(I); //Still prints 5
     System.out.println(increaseObject2(I)); //Still prints 5

}

public static void increasePrimitive(int n){
     n++;
}

public static void increaseObject(Integer n){
     n++;
}

public static int increaseObject2(Integer n){
         return n++; 
}

Does the increaseObject print 5 because the value of reference is changing inside that function? Am I right?
I am confused why the increasedObject2 prints 5 and not 6.

Can anyone please explain?

  • 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-12T10:04:17+00:00Added an answer on June 12, 2026 at 10:04 am

    The problem is return n++; where n is returned and then only incremented. It works as expected if you change it to return ++n; or return n+1;

    But still what you are trying to test does not work with Integer because it is immutable. You should try with something like –

    class MyInteger {
         public int value; //this is just for testing, usually it should be private
    
         public MyInteger(int value) {
             this.value = value;
         }
    }
    

    which is mutable.

    Then you can pass around the reference to an instance of that class and mutate it (change the value of value in that instance) from the invoked method.

    Change the method –

    public static void increaseObject(MyInteger n){
         n.value++;
    }
    

    and call it –

    MyInteger i = new MyInteger(5);    
    increaseObject(i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I've already asked similar question on this issue on stack overflow already, but I
I read all topics related to this question in stackoverflow and whole internet and
Read about the issue in this stackoverflow question . Still have the same issue
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
I read this question but the answer does not perform a refactoring of the
Having read this question Immutable or not immutable? and reading answers to my previous
I have read this question and the simple and clear answer but it's not
I have read this question but it's not quite what I was looking for.
You can read this question where I ask about the best architecture for 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.