ilomambo
ilomambo

Reputation: 8350

How to get the primitive from a boxed Object

When using custom callbacks (like android:onClick) the actual method is called using something like

mHandler.invoke(context, View.this);

According to the doc on invoke:

Object java.lang.reflect.Method.invoke(Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException

Returns the result of dynamically invoking this method. Equivalent to receiver.methodName(arg1, arg2, ... , argN).

If the invocation completes normally, the return value itself is returned. If the method is declared to return a primitive type, the return value is boxed. If the return type is void, null is returned.

What does "boxed" mean? If the actual method returns a boolean how can i get it from Object?

Upvotes: 0

Views: 100

Answers (2)

Vishal Pawale
Vishal Pawale

Reputation: 3476

Use booleanValue() on Boolean object, to get boolean primitive

Upvotes: 0

Matt
Matt

Reputation: 186

Boxing, otherwise known as wrapping, is the process of placing a primitive type within an object so that the primitive can be used as a reference object. Wikipedia

The boxed primitive, in your case, would be the Boolean object. I believe you should be able to cast a returned, non-null object straight to a Boolean object or a boolean primitive.

Upvotes: 2

Related Questions