Could anyone help me find the return type of a method in JAVA. I tried this. But unfortunately it doesn't work. Please guide me.
Method testMethod = master.getClass().getMethod("getCnt");
if(!"int".equals(testMethod.getReturnType()))
{
System.out.println("not int ::" + testMethod.getReturnType());
}
Output :
not int ::int
The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer. The variable receiving the value returned by a method must also be compatible with the return type specified for the method.
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value.
Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types.
A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string.
The method getReturnType()
returns Class
You can try:
if (testMethod.getReturnType().equals(Integer.TYPE)){
.....;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With