Results 1 to 6 of 6
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default what is the relation between the class of "int" and Integer.class?

    I see the following is OK
    Java Code:
    Class<Integer> c1 = int.class
    I'm wondering what is the relation between the class of "int" and Integer.class?

  2. #2
    @timtoday is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: what is the relation between the class of "int" and Integer.class?

    int is a primitive integer type, while Integer is what is called a wrapper class for the primitive "data" type.

    Every primitive type has its corresponding wrapper class.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: what is the relation between the class of "int" and Integer.class?

    True statement but not really addressing the question.

    All the primitive classes have their own defined class type (e.g. int.class, double.class, etc).

    One particular use of this is in reflection. Here is an example:

    Java Code:
    import java.lang.reflect.Method;
    
    public class ReflectDemo {
       public static void main(String[] args) {
          Foo f = new Foo();
          try {
             Method m = f.getClass().getDeclaredMethod("add", Integer.class, Integer.class);
             int a = (int)m.invoke(f, 10,20);
             System.out.println("Sum = " + a);
    
             m = f.getClass().getDeclaredMethod("add", int.class, int.class);
             a = (int)m.invoke(f,30,40);
             System.out.println("Sum = " + a);
    
          } catch (Exception nme) {
             nme.printStackTrace();
          }
       }
    }
    
    class Foo {
       public int add(Integer a, Integer b) {
          System.out.println("Using Integers");
          return a + b;
       }
       
       public int add(int a, int b) {
          System.out.println("Using ints");
          return a + b;
       }
    }
    Notice that the *.class use in the getDeclaredMethod() is used to find the appropriate method of the overloaded method add. There
    may be other uses of the primitive class types but I believe reflectiive support is the primary one.

    Note: I had come up with an example when I read this thread the other day and thought that I had posted it.
    Apparently not.


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    @timtoday is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: what is the relation between the class of "int" and Integer.class?

    Yes, reflection maybe the use for it, but I think the OP wanted to know how they are related IMHO.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: what is the relation between the class of "int" and Integer.class?

    Perhaps your right. But then I would expect the question to be more like "What's the difference between Integer and int and how are they used."
    Anyway, the OP now has two pieces of information for the price of one :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    @timtoday is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: what is the relation between the class of "int" and Integer.class?


    Yeah you are right...

Similar Threads

  1. Replies: 23
    Last Post: 12-12-2014, 09:24 AM
  2. Replies: 1
    Last Post: 06-17-2013, 05:04 PM
  3. Replies: 6
    Last Post: 02-08-2012, 06:42 PM
  4. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  5. Replies: 6
    Last Post: 05-18-2010, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •