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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:12:48+00:00 2026-05-23T13:12:48+00:00

I’ve searched for answers here and every thread I found were in fact fragments

  • 0

I’ve searched for answers here and every thread I found were in fact “fragments” of what I seek.

I’d like to find a better way than this :

~ EDIT: OOPS ! I meant to use primitive Wrapper classes in the first place, but I was thinking of using primitive types when calling the method at that very moment~
Thank you for noticing it 🙂

@Override
public void setValue(Object value) {

    if (value instanceof String) {

    } else if (value instanceof Integer) { // and not 'int'

    } else if (value instanceof Long) { // and not 'long'

    }
}

// The usage that made me confused in the first place :
int i = 42;
setValue(i);

Notice the @Override annotation: this is an implementation of an interface method. This method would accept different types based on the implementation so I don’t want to create three disctinct methods using different parameter types.

In this example, this is a textbox that only accepts digits and nothing else, so it can only be represented by a String (which is validated by a Regular Expression ^[0-9]*$), a long and an int.

I’d also like it to – maybe, eventually – accept custom (and simple) DTO’s that are more like POJOs, but if this particularity makes everything else complicated I’ve got something else in mind so don’t worry too much about this one.

As I said, different implementations of this interface could accept totally different types.

* I am obviously not asking a way to switchcase Integers, Longs and String (which cannot be switchcased, yet. Not until Java7 that is), I want to switchcase the instanceofs *

After looking at

  • java: boolean instanceOf Boolean?
  • switch instanceof?
  • Autoboxing : http://leepoint.net/notes-java/data/basic_types/autoboxing.html

My implementation obviously works, but I just feel there’s a better way.
I am wondering if there is a cleaner method than doing what I did, what do you suggest and why?

Thank you for your time.
Sincerely, Dominic Brissette.

EDIT: usage with primitive types and Autoboxing

public static void main(String[] args) {
    int i = 42;

    System.out.println(autoboxing(i));
}

public static boolean autoboxing(Object o) {
    return o instanceof Integer;
}

Outputs true because in the end, myInt instanceof Integer is kinda true ..!

  • 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-23T13:12:49+00:00Added an answer on May 23, 2026 at 1:12 pm

    One thing you could do is wrap your primitive in an interface, and this way any value type implementing this interface could be passed as a value.

    public interface ValueInterface {
        processValue();
    }
    

    Then the set value method would look like:

    public void setValue(ValueInterface value) {
       value.processValue();
    }
    

    Now you’re delegating the work to the actual object who knows how to act on itself.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.