1 2 Previous Next 15 Replies Latest reply: Mar 28, 2008 4:49 PM by 807591 RSS

    primitives and wrappers

    Gen.Java
      int i=10;
      Integer I=50;
      System.out.println(Integer.class.isInstance(i)); // prints true
      System.out.println(int.class.isInstance(I)); // prints false
      System.out.println(Integer.class.isAssignableFrom(int.class)); // prints false
      System.out.println(int.class.isAssignableFrom(Integer.class)); // prints false
      The problem:
      I have two classes (not objects) at runtime. One is supposed to be for a primitive (ex. int.class) and the other is for its wrapper (Integer.class). I need to validate that both classes ara compatible (assignable to each other). The only statement above that returns true I cannot use because it accepts an object as a parameter which I don't have.

      Thank you
        • 1. Re: primitives and wrappers
          807591
          dear autoboxing
          • 2. Re: primitives and wrappers
            Gen.Java
            calvino_ind wrote:
            dear autoboxing
            I have two classes at runtime that could be assignable or not. For example, int.class and String.class which are not assignable. I need to check if they are assignable to each other. autoboxing makes the assignment possible (of course as long as they are compatible), this is a different issue.
            • 3. Re: primitives and wrappers
              807591
              i was kinda ironical, dude :)

              "I need to check if they are assignable to each other."

              well, isAssignableFrom in class Class is meant for that, no?
              • 4. Re: primitives and wrappers
                mlk
                I do not think there is, you could make a Map containing the valid combinations.
                • 5. Re: primitives and wrappers
                  Gen.Java
                  mlk wrote:
                  I do not think there is, you could make a Map containing the valid combinations.
                  I already did that, but I was hoping that there is an easier way.
                  • 6. Re: primitives and wrappers
                    807591
                    Theres no int.class, the equivalent is Integer.TYPE (and so on for all primitives).
                    • 7. Re: primitives and wrappers
                      807591
                      int i;
                      // isInstance takes an Object, so this works (compiles) only with autoboxing: 
                      System.out.println(Integer.class.isInstance(i));
                      • 8. Re: primitives and wrappers
                        807591
                        Theres no int.class
                        $ java testVersion
                        int
                        public class testVersion {
                        public static void main(String args[]) throws Exception {
                        // is there int.class? 
                        Class clazz = int.class;
                        System.out.println(clazz);
                        }
                        }
                        • 9. Re: primitives and wrappers
                          Gen.Java
                          malcolmmc wrote:
                          Theres no int.class, the equivalent is Integer.TYPE (and so on for all primitives).
                          You are right but how could this solve my problem. or this was just a comment ?
                          • 10. Re: primitives and wrappers
                            807591
                            If there were int.class, this example would compile and work:
                            C:\doku>java testVersion
                            false
                            true
                            false
                            true
                            false
                            false
                            public class testVersion
                            {
                              public static void main( String args[] ) throws Exception
                              {
                                System.out.println( okay( int.class, Short.class ) );
                                System.out.println( okay( int.class, Integer.class ) );
                                System.out.println( okay( int.class, Long.class ) );
                            
                                System.out.println( okay( short.class, Short.class ) );
                                System.out.println( okay( short.class, Integer.class ) );
                                System.out.println( okay( short.class, Long.class ) );
                              }
                            
                              static boolean okay( Class pri, Class obj )
                              {
                                java.lang.reflect.Constructor[]     ct = obj.getConstructors();
                            
                                for ( int i = 0; i < ct.length; i++ )
                                {
                                  Class[]   params = ct[ i ].getParameterTypes();
                            
                                  if ( 1 == params.length && params[ 0 ] == pri )
                                  {
                                    return true;
                                  }
                                }
                                return false;
                              }
                            
                            }
                            • 11. Re: primitives and wrappers
                              Gen.Java
                              BIJ001 ... you are an excellent observer and I thank you for your effort and time. However, there are some issues about your code:

                              1. okay() needs to check which parameter is the wrapper and which is the primitive because I get two classes and I don't know which is the primitive and which is the wrapper. Also they both could be primitives or wrappers. Also they could neither be a primitive nor wrapper. I think this is easy to do.

                              2. How would you be sure that there isn't a class out there that just happen to have a constructor with one parameter of a primitive type. This would make this class compatible with the primitive which is not.

                              3. Future versions of Java may not guarentee that this obsevation is true.
                              • 12. Re: primitives and wrappers
                                807591
                                If we want to cope with the primitive types and their wrapper classes, meticulous studying of the API could help us further:

                                The primitive wrappers are the classes in java.lang which extend java.lang.Number.
                                boolean isPrimitive()
                                // Determines if the specified Class object represents a primitive type.
                                getPackage().getName() tells if it is the "java.lang" package.
                                • 13. Re: primitives and wrappers
                                  796440
                                  Gen.Java wrote:
                                  mlk wrote:
                                  I do not think there is, you could make a Map containing the valid combinations.
                                  I already did that, but I was hoping that there is an easier way.
                                  'Fraid not.
                                  • 14. Re: primitives and wrappers
                                    796440
                                    malcolmmc wrote:
                                    Theres no int.class,
                                    Yes, there is. Also boolean.class, double.class, etc.
                                    1 2 Previous Next