简体   繁体   中英

Is there native type-safe wrapping and unboxing for Object?

My inquiry is on wrapping, using the Object class as my wrapper.

My goal is to take the primitive int (or a wrapper of int) and unbox it from a generic Object.

The only problem is that I will be working with mixed types at runtime, so I am unable to generically type the return type of the method. T var1 != (T) var2

What I can do right now has excessive overhead because I cannot assume the Object wraps a value of type int, so I parse it.

Object int_wrapped_in_object = 1;

Integer.valueOf(String.valueOf(int_wrapped_in_object));

//I could cast from Object to int with:
//(int) int_wrapped_in_object
//but this will always anticipate a classcastexception in case the value was wrapped with another type, like String

This works, but I would ideally like to skip a step of parsing and just unwrap box for the integer value.

Integer wrapper class does not support valueOf(Object o) , likely because Object does not implement the relative cast. It (Object) supports toString() . But not toInt(). It is very peculiar because Object indeed can wrap a primitive int. Why that is the case is above my level of knowledge, so I can only work with what I have. For all I know there may even be native support for unwrapping an Object as an int.

Please help me find a solution that involves using minimal parsing and introducing as few exceptions to get the primitive int value.

Misconception on your end:

Object int_wrapped_in_object = 1;

The type of int_wrapped_in_object is Integer , not Object! The compiler already boxes for you!

In other words: there is no way that the java compiler would "box" an int into something that is "only" Object .

Thus a simple int_wrapped_in_object instanceof Integer is all you need to figure if that Object is actually an Integer. And then you can simply cast !

Your value gets wrapped to an Integer and then the view to it is reduced by casting it to Object .

You can later safely cast it back if you check the type with instanceof . Then you can unwrap it to int with the Integer#intValue method.

// Indirect conversion int -> Integer and then reduced view to Object
Object int_wrapped_in_object = 5;

if (int_wrapped_in_object instanceof Integer) {
    // Checked cast as the object indeed is an Integer but with reduced view
    Integer intAsInteger = (Integer) int_wrapped_in_object;

    // Retrieve the int value from the Integer
    // (you could also do an implicit unwrapping here) like
    // int value = intAsInteger;
    int value = intAsInteger.intValue();

    System.out.println("The value is: " + value);
} else {
    throw new IllegalStateException("Value is no Integer but it should.");
}

Note that the line

Object int_wrapped_in_object = 5;

gets indirectly transformed to

Object int_wrapped_in_object = (Object) Integer.valueOf(5);

If you need to do such conversions often then just create an utility method which does that for you.


Note that the object itself stays Integer and gets not converted to an Object , that is not how casting works, only its view is reduced. You can check that with

System.out.println(int_wrapped_in_object.getClass())

It will print class java.lang.Integer and not class java.lang.Object . So the type of an object always stays the same, even after casting. You only reduce the view to it which has advantages like higher modularity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM