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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:16:31+00:00 2026-05-15T22:16:31+00:00

What is the reason why Wrapper classes (like Integer, Double, etc.) don’t have a

  • 0

What is the reason why Wrapper classes (like Integer, Double, etc.) don’t have a setter for their inner primitive value ?

I am asking this because that kind of functionality would have simplified calculus, and have made the Java language a little more flexible .

Let me give you some examples.

1) Let’s take the following example:

Integer x = new Integer(5);
x++;

The previous code behind the scenes is performing autoboxing . Something like:

int x_tmp = x.intValue();
x_tmp++;
x = new Integer(x_tmp); // Yes that's a new memory allocation

Because of this problem doing calculus on Wrapper is slower than performing on plain primitive types. With a setter it would’ve been more easy to increment the inner value, without allocating another object on the heap.

2) Another issue that is bugging me is that is impossible in Java to write a swap function like I can do in C (using pointers) or in C++ (pointers or references).

If i write void swap(Integer x, Integer y) I cannot acces the inner value because, and It is going to be impossible for me to swap the values.

PS:
A friend of mine suggested that i should consider the bigger picture, and think in terms of concurrency and type immutability.

So do you have an explanation for this ?
Thanks!

  • 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-15T22:16:32+00:00Added an answer on May 15, 2026 at 10:16 pm

    1) With a setter, the wrapper types would be mutable. Immutability is a good thing in many ways… threading, general understandability of the code etc. Personally I think it’s a shame that Calendar and Date are mutable, for example.

    In fact, your expansion of x++; isn’t quite right – it uses Integer.valueOf which doesn’t always create a new value. For example:

    Integer x = 5;
    x++;
    Integer y = 5;
    y++;
    
    // This prints true    
    System.out.println(x == y); // Compare references
    

    Only a limited range of Integer values are cached like this (the spec defines what values must behave this way, but allows for a wider range if the JRE wishes to do so)… but it does mean that it won’t always be creating a new object.

    2) Yes, Java doesn’t have pass by reference. Frankly I very rarely find that to be a problem. How often do you really need to swap the values of variables?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a generic wrapper class for a bunch of classes we have defined
I'm creating a primitive type wrapper, which can use boost::lexical_cast for setting its value
The reason I ask is, I have a web application written in Ruby (1.8.7-p357)
The reason I need the inner class to be non static is because I
Hi I am having problems with some css classes in joomla. I have two
We currently have quite a few classes in a project, and each of those
I have a page with a wrapper div which contains a background image and
What I want to do is something like this: $(.chat-wrapper).on(click, .chat-wrapper li, function(){ $(this.name).hide();
I have a base class for data access classes. This class implements IDisposable. This
For some reason I don't know why it says Error: validator.serialize is not 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.