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

  • Home
  • SEARCH
  • 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 5938695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:43:20+00:00 2026-05-22T15:43:20+00:00

My question is: why do primitive types have to be wrapped in an object,

  • 0

My question is: why do primitive types have to be wrapped in an object, when it is also a possibility to have the compiler set things right for you?

  1. calls to a primitive value x’s methods can be translated from x.call() to X.call(x) – this is what I try to illustrate below.
  2. generics aren’t kept around at runtime (right?), so it isn’t the case that you need to access class information at runtime – you could simply replace every instance of Integer by int and rewrite method calls as above, and end up with executable code.

So basically, what I’m asking is: what am I missing here?


I’ve been wondering about this for some time: Why can’t the Java compiler translate…

int a = 482;
int b = 12;
System.out.println((a + b).toHexString());

…to the following code…

int a = 482;
int b = 12;
System.out.prinln(Ints.toHexString(a + b));

…and thus remove the entire need for boxing and unboxing?

(Thus, compile method calls to (static) function calls, and keep a single instance of Int.class around in case it’s needed – e.g. after a call of Ints.getClass(_)?)


Comment added for clarity:

@Adam: No, I do not think it causes
boxing/unboxing. The example attempts
to illustrate how primitives could be
treated as objects in the language,
but as primitives by the compiler.
This would remove the (slight)
overhead in runtime, and a confusing
thing in the language. Therefore the
intended question was exactly: why
didn’t the extremely talented compiler
developers pick this solution? Because if they didn’t, there
must be a clear impossibility that I’m
not seeing. And I’d like to know what
it is. – Pepijn

  • 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-22T15:43:21+00:00Added an answer on May 22, 2026 at 3:43 pm

    I presume you meant Integer.toHexString, not Ints.toHexString. The latter is not part of java.lang and the compiler would have no way to know anything about it.

    In theory, the compiler could translate calls of (a + b).toHexString() to Integer.toHexString(a + b), if the Java language specifies such a translation. (For example, auto-boxing of an int a to an Integer is specified to be Integer.valueOf(a).)

    I guess the Java language maintainers decided that that is too “magical” for Java programmers—in all versions of Java, primitive types do not have methods or fields. Java, in general, is designed to avoid syntactic sugar—that is why it’s generally more verbose than most other languages.

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

Sidebar

Related Questions

My question is about memory use and objects in actionscript 2. If I have
I need to solve the following question which i can't get to work by
I've found several jQuery syntaxes for nullifying the enter on a form. First one:
Every time that I want to do a Layout, I'm getting a black layout
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,
I would like to remove/delete a migration file. How would I go about doing

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.