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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:23:15+00:00 2026-05-27T09:23:15+00:00

Possible Duplicate: Why don't Generics support primitive types? I want to make one function

  • 0

Possible Duplicate:
Why don't Generics support primitive types?

I want to make one function for int, short and long. I have this in 3 almost identically function.

    private <T extends Number > T scanTInteger() throws IOException, NumberFormatException
{
    T number = 0;

But i don’t knew how to do generic type like int, short, long. For Object it is easy.
Is there is method for that.
EDIT
Ok i knew how to do that with out generic function. I will use only long and max size of primitive types and at the end convert it. If someone find how to do with generic i will be happy 🙂

  • 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-27T09:23:16+00:00Added an answer on May 27, 2026 at 9:23 am

    You could create your own class which wrapped any one of these three types, and then handle them within that class’ code. You would not have any of the language-specific automated handling, autoboxing and the like, but that would give you what you appear to be asking for.

    However, it seems to me the primary reason for making one function is to write some piece of logic one time instead of three. I suggest looking at having different methods for each data type, and then having those three methods each call one method that contains your logic. It can be (should be) private, and therefore hide whatever handling you do (before and/or after your central logic) to accommodate all three types, calling them will be easy from any point, since the primitives don’t have to be inserted into the wrapper, and having 3 one-line methods instead of one generic method is not very much trouble for either a maintainer or a user.

    In other words, is the use of generics really worth it in your case? They’re really meant for use with classes, not primitives.

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

Sidebar

Related Questions

Possible Duplicate: Why don't Generics support primitive types? Why we cannot use double as
Possible Duplicate: How to get PNG transparency working in browsers that don't natively support
Possible Duplicate: How to write a basic swap function in Java Hi. I don't
Possible Duplicate: Why don't people indent C++ access specifiers/case statements? I have a syntax
Possible Duplicate: don't work 'fadeOut' If there is 'replaceWith'.? I want after delete row(s)
Possible Duplicate: SQL - find records from one table which don't exist in another
Possible Duplicate: function overloading in C ANSI C doesn't permit function overloading (I don't
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the

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.