Are primitives objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred

    Are primitives objects?

    Are primitives objects?

    int i = 3;
    System.out.prin tln(i.getClass( ));

    doesn't compile. Get an error message
    "int can't be dereferenced"

    But yet the docs for class Object say:
    Class Object is the root of the class hierarchy. Every class has Object as
    a superclass.



  • Igor L

    #2
    Re: Are primitives objects?

    No, primitives are not objects. Yet, if you need an object (for example to
    put it in a hashtable or something) you can do the following:

    int i = 3;
    Integer object_i = new Integer(i);

    now you have an object and you can do

    System.out.prin tln(object_i.ge tClass());

    Every primitive type has it's own corresponding object type.
    int -> Integer, double -> Double, etc.

    See documentation on this classes.



    "Fred" <Fred@isp.com > wrote in message news:OssLb.3817 $na.5853@attbi_ s04...[color=blue]
    > Are primitives objects?
    >
    > int i = 3;
    > System.out.prin tln(i.getClass( ));
    >
    > doesn't compile. Get an error message
    > "int can't be dereferenced"
    >
    > But yet the docs for class Object say:
    > Class Object is the root of the class hierarchy. Every class has Object[/color]
    as[color=blue]
    > a superclass.
    >
    >
    >[/color]


    Comment

    • Fred

      #3
      Re: Are primitives objects?

      Thanks Igor L.

      Wrapper classes are indeed handy for the primitives.
      The way I got to this question was from looking at the docs for the Class
      class,
      and the getName method in particular. It says

      The primitive Java types (boolean, byte, char, short, int, long, float, and
      double), and the keyword void are also represented as Class objects
      If I do something like

      String str = "Hello";
      System.out.prin tln( str.getClass(). getName() );

      then java.lang.Strin g does indeed print out. But

      int i = 3;
      System.out.prin tln( i.getClass().ge tName() );

      fails to compile because primitives can't be dereferenced as they aren't
      references to objects.


      From the docs for Class, method getName;
      getName
      public String getName()Return s the name of the entity (class, interface,
      array class, primitive type, or void) represented by this Class object, as a
      String.
      If this class object represents a reference type that is not an array type
      then the binary name of the class is returned, as specified by the Java
      Language Specification, Second Edition. If this class object represents a
      primitive type or void, then the name returned is the name determined by the
      following table. The encoding of element type names is as follows:

      B byte
      C char
      D double
      F float
      I int
      J long
      Lclassname; class or interface
      S short
      Z boolean
      V void

      Question:
      How does one use this with primitives? Or how should System.out.prin tln(
      ......getName() );
      be structured in order to have it print out an "I" , or "J", or
      Ljava.lang.stri ng ?

      "Igor L" <patak_dacha@ya hoo.com.hk> wrote in message
      news:btlnr8$kg4 $1@sunce.iskon. hr...[color=blue]
      > No, primitives are not objects. Yet, if you need an object (for example to
      > put it in a hashtable or something) you can do the following:
      >
      > int i = 3;
      > Integer object_i = new Integer(i);
      >
      > now you have an object and you can do
      >
      > System.out.prin tln(object_i.ge tClass());
      >
      > Every primitive type has it's own corresponding object type.
      > int -> Integer, double -> Double, etc.
      >
      > See documentation on this classes.
      >
      >
      >
      > "Fred" <Fred@isp.com > wrote in message[/color]
      news:OssLb.3817 $na.5853@attbi_ s04...[color=blue][color=green]
      > > Are primitives objects?
      > >
      > > int i = 3;
      > > System.out.prin tln(i.getClass( ));
      > >
      > > doesn't compile. Get an error message
      > > "int can't be dereferenced"
      > >
      > > But yet the docs for class Object say:
      > > Class Object is the root of the class hierarchy. Every class has Object[/color]
      > as[color=green]
      > > a superclass.
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Raymond DeCampo

        #4
        Re: Are primitives objects?

        Fred wrote:[color=blue]
        > Thanks Igor L.
        >
        > Wrapper classes are indeed handy for the primitives.
        > The way I got to this question was from looking at the docs for the Class
        > class,
        > and the getName method in particular. It says
        >
        > The primitive Java types (boolean, byte, char, short, int, long, float, and
        > double), and the keyword void are also represented as Class objects
        > If I do something like
        >
        > String str = "Hello";
        > System.out.prin tln( str.getClass(). getName() );
        >
        > then java.lang.Strin g does indeed print out. But
        >
        > int i = 3;
        > System.out.prin tln( i.getClass().ge tName() );
        >
        > fails to compile because primitives can't be dereferenced as they aren't
        > references to objects.
        >
        >
        > From the docs for Class, method getName;
        > getName
        > public String getName()Return s the name of the entity (class, interface,
        > array class, primitive type, or void) represented by this Class object, as a
        > String.
        > If this class object represents a reference type that is not an array type
        > then the binary name of the class is returned, as specified by the Java
        > Language Specification, Second Edition. If this class object represents a
        > primitive type or void, then the name returned is the name determined by the
        > following table. The encoding of element type names is as follows:
        >
        > B byte
        > C char
        > D double
        > F float
        > I int
        > J long
        > Lclassname; class or interface
        > S short
        > Z boolean
        > V void
        >
        > Question:
        > How does one use this with primitives? Or how should System.out.prin tln(
        > .....getName() );
        > be structured in order to have it print out an "I" , or "J", or
        > Ljava.lang.stri ng ?
        >[/color]

        All of the wrapper classes have a static final Class variable named TYPE
        that holds the "Class instance representing the primitive type." So
        you could do:

        System.out.prin tln(Integer.TYP E.getName());

        which would print:

        int

        Which brings me to my second pint, it appears you have misread (and
        misquoted) the documentation for Class.getName() . The chart you posted
        above only applies to array types.

        Consider the following:

        public class ClassGetName
        {
        public static void main(String args[])
        {
        String str = "";
        int intArr[] = new int[5];
        String strArr[] = new String[5];
        System.out.prin tln("Integer.TY PE.getName(): "
        + Integer.TYPE.ge tName());
        System.out.prin tln("str.getCla ss().getName(): "
        + str.getClass(). getName());
        System.out.prin tln("intArr.get Class().getName (): "
        + intArr.getClass ().getName());
        System.out.prin tln("strArr.get Class().getName (): "
        + strArr.getClass ().getName());
        }
        }

        The output is:

        [~/foo]$ java -cp . ClassGetName
        Integer.TYPE.ge tName(): int
        str.getClass(). getName(): java.lang.Strin g
        intArr.getClass ().getName(): [I
        strArr.getClass ().getName(): [Ljava.lang.Stri ng;

        Ray

        Comment

        • Igor L

          #5
          Re: Are primitives objects?


          "Fred" <Fred@isp.com > wrote in message
          news:qlzLb.6384 $5V2.10893@attb i_s53...[color=blue]
          > Thanks Igor L.
          >
          > Wrapper classes are indeed handy for the primitives.
          > The way I got to this question was from looking at the docs for the Class
          > class,
          > and the getName method in particular. It says
          >
          > The primitive Java types (boolean, byte, char, short, int, long, float,[/color]
          and[color=blue]
          > double), and the keyword void are also represented as Class objects
          > If I do something like
          >
          > String str = "Hello";
          > System.out.prin tln( str.getClass(). getName() );
          >
          > then java.lang.Strin g does indeed print out. But
          >
          > int i = 3;
          > System.out.prin tln( i.getClass().ge tName() );
          >
          > fails to compile because primitives can't be dereferenced as they aren't
          > references to objects.
          >
          >
          > From the docs for Class, method getName;
          > getName
          > public String getName()Return s the name of the entity (class, interface,
          > array class, primitive type, or void) represented by this Class object, as[/color]
          a[color=blue]
          > String.
          > If this class object represents a reference type that is not an array type
          > then the binary name of the class is returned, as specified by the Java
          > Language Specification, Second Edition. If this class object represents a
          > primitive type or void, then the name returned is the name determined by[/color]
          the[color=blue]
          > following table. The encoding of element type names is as follows:
          >
          > B byte
          > C char
          > D double
          > F float
          > I int
          > J long
          > Lclassname; class or interface
          > S short
          > Z boolean
          > V void
          >
          > Question:
          > How does one use this with primitives? Or how should System.out.prin tln(
          > .....getName() );
          > be structured in order to have it print out an "I" , or "J", or
          > Ljava.lang.stri ng ?[/color]

          does it have to be getName() method? create a new method (static?) in some
          helper class which would call getName method, than look in some table and
          return B,C,D,F....
          it seems a reasonable solution to me, maybe it isn't, depends on the
          structure of your code.

          [color=blue]
          >
          > "Igor L" <patak_dacha@ya hoo.com.hk> wrote in message
          > news:btlnr8$kg4 $1@sunce.iskon. hr...[color=green]
          > > No, primitives are not objects. Yet, if you need an object (for example[/color][/color]
          to[color=blue][color=green]
          > > put it in a hashtable or something) you can do the following:
          > >
          > > int i = 3;
          > > Integer object_i = new Integer(i);
          > >
          > > now you have an object and you can do
          > >
          > > System.out.prin tln(object_i.ge tClass());
          > >
          > > Every primitive type has it's own corresponding object type.
          > > int -> Integer, double -> Double, etc.
          > >
          > > See documentation on this classes.
          > >
          > >
          > >
          > > "Fred" <Fred@isp.com > wrote in message[/color]
          > news:OssLb.3817 $na.5853@attbi_ s04...[color=green][color=darkred]
          > > > Are primitives objects?
          > > >
          > > > int i = 3;
          > > > System.out.prin tln(i.getClass( ));
          > > >
          > > > doesn't compile. Get an error message
          > > > "int can't be dereferenced"
          > > >
          > > > But yet the docs for class Object say:
          > > > Class Object is the root of the class hierarchy. Every class has[/color][/color][/color]
          Object[color=blue][color=green]
          > > as[color=darkred]
          > > > a superclass.
          > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Fred

            #6
            Re: Are primitives objects?

            Thanks Ray and Igor L.

            Reading the doc again, it does say it is only for classes which rules out
            primitives, so using something like
            int i = 3;
            i.getName() isn't supported. (or correct)


            "Raymond DeCampo" <rdecampo@spa m-I-am-not.twcny.rr.co m> wrote in message
            news:pRzLb.1313 52$JW3.38966@tw ister.nyroc.rr. com...[color=blue]
            > Fred wrote:[color=green]
            > > Thanks Igor L.
            > >
            > > Wrapper classes are indeed handy for the primitives.
            > > The way I got to this question was from looking at the docs for the[/color][/color]
            Class[color=blue][color=green]
            > > class,
            > > and the getName method in particular. It says
            > >
            > > The primitive Java types (boolean, byte, char, short, int, long, float,[/color][/color]
            and[color=blue][color=green]
            > > double), and the keyword void are also represented as Class objects
            > > If I do something like
            > >
            > > String str = "Hello";
            > > System.out.prin tln( str.getClass(). getName() );
            > >
            > > then java.lang.Strin g does indeed print out. But
            > >
            > > int i = 3;
            > > System.out.prin tln( i.getClass().ge tName() );
            > >
            > > fails to compile because primitives can't be dereferenced as they aren't
            > > references to objects.
            > >
            > >
            > > From the docs for Class, method getName;
            > > getName
            > > public String getName()Return s the name of the entity (class, interface,
            > > array class, primitive type, or void) represented by this Class object,[/color][/color]
            as a[color=blue][color=green]
            > > String.
            > > If this class object represents a reference type that is not an array[/color][/color]
            type[color=blue][color=green]
            > > then the binary name of the class is returned, as specified by the Java
            > > Language Specification, Second Edition. If this class object represents[/color][/color]
            a[color=blue][color=green]
            > > primitive type or void, then the name returned is the name determined by[/color][/color]
            the[color=blue][color=green]
            > > following table. The encoding of element type names is as follows:
            > >
            > > B byte
            > > C char
            > > D double
            > > F float
            > > I int
            > > J long
            > > Lclassname; class or interface
            > > S short
            > > Z boolean
            > > V void
            > >
            > > Question:
            > > How does one use this with primitives? Or how should[/color][/color]
            System.out.prin tln([color=blue][color=green]
            > > .....getName() );
            > > be structured in order to have it print out an "I" , or "J", or
            > > Ljava.lang.stri ng ?
            > >[/color]
            >
            > All of the wrapper classes have a static final Class variable named TYPE
            > that holds the "Class instance representing the primitive type." So
            > you could do:
            >
            > System.out.prin tln(Integer.TYP E.getName());
            >
            > which would print:
            >
            > int
            >
            > Which brings me to my second pint, it appears you have misread (and
            > misquoted) the documentation for Class.getName() . The chart you posted
            > above only applies to array types.
            >
            > Consider the following:
            >
            > public class ClassGetName
            > {
            > public static void main(String args[])
            > {
            > String str = "";
            > int intArr[] = new int[5];
            > String strArr[] = new String[5];
            > System.out.prin tln("Integer.TY PE.getName(): "
            > + Integer.TYPE.ge tName());
            > System.out.prin tln("str.getCla ss().getName(): "
            > + str.getClass(). getName());
            > System.out.prin tln("intArr.get Class().getName (): "
            > + intArr.getClass ().getName());
            > System.out.prin tln("strArr.get Class().getName (): "
            > + strArr.getClass ().getName());
            > }
            > }
            >
            > The output is:
            >
            > [~/foo]$ java -cp . ClassGetName
            > Integer.TYPE.ge tName(): int
            > str.getClass(). getName(): java.lang.Strin g
            > intArr.getClass ().getName(): [I
            > strArr.getClass ().getName(): [Ljava.lang.Stri ng;
            >
            > Ray
            >[/color]


            Comment

            Working...