1

Obviously this is possible (as Class has isPrimitive() method) but how I can do this directly? E.g. reflection api able to cast a primitive type to Class:

import java.lang.reflect.Method;


public class TestClass {
       
        public static void m(){}
        public static void m(Object o){}
        public static void m(int i){}
        public static void m(Integer i){}
       
        public static void main(String[] args){
               
                TestClass tc=new TestClass();
                Method[] mtds=tc.getClass().getDeclaredMethods();
               
                for (int i = 0; i < mtds.length; i++) {
                        System.out.print(mtds[i].getName());
                        Class[] prms=mtds[i].getParameterTypes();
                        for (int j = 0; j < prms.length; j++) {
                                System.out.print("-"+prms[j].getCanonicalName());
                                       
                        }
                        System.out.println();
                }        
        }
}

output is:

m

m-java.lang.Object

m-int

m-java.lang.Integer

main-java.lang.String[]

Integer and int are different so it is not autoboxing. Anything (except creating an utility class with reflection and getParameterTypes() ) do not come at my mind (everything else I've tried failed).

Community
  • 1
  • 1
npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • I don't understand your question. There are class literals for primitives. But given a primitive value (or rather a variable of a primitive type), you already know its type. You wouldn't need to use something similar to `getClass`. – Sotirios Delimanolis Nov 23 '15 at 19:29
  • 1
    What has this to do with autoboxing? Can you tell what is your expected output? Not really clear. – Rohit Jain Nov 23 '15 at 19:31
  • @RohitJain - my first thought was that `int` is wrapped to `Integer` so it will be possible to get the Class object from int.But obviously I was wrong :-) – npocmaka Nov 23 '15 at 21:56

1 Answers1

3

For int you normally do int.class or Integer.TYPE. Similarly for the other primitive types (e.g. double.class).

M A
  • 65,721
  • 13
  • 123
  • 159
  • aah.Great! Didn't know this.Will accept this after 10 minmutes. – npocmaka Nov 23 '15 at 19:30
  • 2
    Even `void` has `void.class` and `Void.TYPE` – Peter Lawrey Nov 23 '15 at 19:32
  • @PeterLawrey - this is pretty interesting.Is there are cases where this can be useful? – npocmaka Nov 23 '15 at 19:41
  • How does and when `int.class` or `Integer.TYPE` be useful ?? If you could throw some light. – hagrawal Nov 23 '15 at 19:45
  • @hagrawal - for me it was useful with reflection as in the example- when I'm searching for a method with exact types of parameters. Though it is interesting how `void` could be useful for something (anyway it is good to know it). – npocmaka Nov 23 '15 at 19:50
  • 1
    @npocmaka http://stackoverflow.com/questions/2352447/what-is-the-need-of-void-class-in-java – M A Nov 23 '15 at 19:56
  • 1
    You can need `Main.class.getMethod("main", String[].class).getReturnType() == void.class` and also `Callable` if you have a callable which doesn't actually return anything. (It might throw a checked exception however so you can't use `Runnable` either) – Peter Lawrey Nov 23 '15 at 20:09