0

I have a java.lang.Object return type from a function. I want to verify whatever Object value returned is of numeric type (double or long or int or byte or Double or Long or Byte or Float or Double ....) and if it's true want to convert into a Integer wrapper reference type. Also if the Object instance holds a String value I want it to be stored in a String reference.

1
  • 3
    Do you have a question ? Commented Aug 21, 2012 at 7:27

4 Answers 4

4

Have a Object return type from a function. I want to verify whatever Object value returned is of numeric type(double or long or int or byte or Double or Long or Byte or Float or Double ....)

if (obj instanceof Number)
    ...


if it's true want to convert into a Integer wrapper reference type

if ...
    val = (Integer) ((Number) obj).intValue();


Also If the Object instance holds a String value i want it to be stored in a String reference.

...
else if (obj instanceof String)
    val = obj;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the info. Notice Valueof function take string as argument. when it is called with a int value does java implicitly convert to string or do i need to toString of the intvalue? If i need to identify a number is with decimal digits is it sufficient to check instanceof float or Double ?
Thanks a lot for the info. Not sure whether my earlier query was clear. How do i identify whether the number is with decimal digits? for example how do i identify number with decimal digit 12.23f, 0.789
Try parsing it using Double.valueOf. If it throws an exception it's not a double.
0

A method that returns Object cannot return primitive types like double, long, or int.

You can check for the actual returned type using instanceof:

if (object instanceof Number){
    // want to convert into a Integer wrapper reference type
    object = ((Number)object).intValue();  // might lose precision
}

You can assign to a String variable by type-casting

if (object instanceof String){
   stringVariable = (String)object;
}

1 Comment

Thanks a lot for the info. Notice Valueof function take string as argument. when it is called with a int value does java implicitly convert to string or do i need to toString of the intvalue? If i need to identify a number is with decimal digits is it sufficient to check instanceof float or Double ?
0

Although you probably have a serious design problem, in order to achieve what you want you can use instanceof operator or getClass() method:

Object o = myFunction();
if(o instanceof Integer) { //or if o.getClass() == Integer.class if you want 
                       only objects of that specific class, not the superclasses
   Integer integer = (Integer) o;
   int i = integer.intValue();
}
   //do your job with the integer
if(o instanceof String)
   //String job

1 Comment

Thanks a lot for the info. If i need to identify a number is with decimal digits is it sufficient to check instanceof float or Double ?
0

You can do something like :

Object obj = getProcessedObject();
if(obj instanceof Number) {
    // convert into a Integer wrapper reference type
Integer ref1 = ((Number)obj).intValue();
}
if(obj instanceof String) {
// process object for String
String ref = (String)obj;
}

1 Comment

Thanks a lot for the info. Notice Valueof function take string as argument. when it is called with a int value does java implicitly convert to string or do i need to toString of the intvalue? If i need to identify a number is with decimal digits is it sufficient to check instanceof float or Double ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.