Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find return type of a Method in JAVA?

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

like image 229
sunleo Avatar asked Feb 06 '13 13:02

sunleo


People also ask

How do you determine the return type of a method in Java?

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.

Do methods have a return type?

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.

What is type () in Java?

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.

What is the data type of return?

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.


1 Answers

The method getReturnType() returns Class

You can try:

if (testMethod.getReturnType().equals(Integer.TYPE)){ 
      .....;  
}
like image 169
Gabriele Mariotti Avatar answered Oct 11 '22 16:10

Gabriele Mariotti