6 Replies Latest reply: Mar 5, 2007 5:42 AM by Puce RSS

    JAVA primitive data types - Signed/ unsigned etc.

    807599
      1. Are the following data types signed or unsigned in JAVA?

      byte, short, int, long, float, double

      2. I have tried using "Integer" in place of "int" and the program runs perfectly. Why is this so when "Integer" is not a reserved word in JAVA?

      Thanks.
        • 1. Re: JAVA primitive data types - Signed/ unsigned etc.
          807599
          Integer is a wrapper class and is part of the core Java API. What you saw was the result of auto-boxing. You weren't actually working with the primitive int, but with Integer objects which happen to wrap int's
          • 2. Re: JAVA primitive data types - Signed/ unsigned etc.
            807599
            Thanks.

            Could you elaborate on "wrapper class" and "Integer objects" (objects with class type "Integer"?) that work on ints.

            Thanks.
            • 3. Re: JAVA primitive data types - Signed/ unsigned etc.
              807599
              And to answer your first question, byte, short, int, long, float and double are all signed.

              As a part of the core of the language, Sun developed a series of 'collections' classes; Vector, ArrayList, etc. All of these classes, in the days before generics, were able to store instances of the Object class or anything that derived from it. Therefore, to store an int, a float or a double value into one of these collections classes, it was necessary to wrap it up in an instance of one of those classes that derived from Number. These are the wrapper classes that the previous poster alluded to. Typically, the wrapper class has a similar name to the primitive data type but with a capital latter; int - Integer, double - Double, etc.
              • 4. Re: JAVA primitive data types - Signed/ unsigned etc.
                807599
                Thanks, your explanation was very clear.

                Could you write a short set of codes to show me how is this "wrapping" done? And how do we use these "wrapped" variables ie. putting them into the collections eg: ArrayList.

                Thanks.
                • 5. Re: JAVA primitive data types - Signed/ unsigned etc.
                  807599
                  List list=new ArrayList<Integer>();
                  int i=5;
                  //list.add(i);    //wont work because can't add int to Collection
                  Integer j=new Integer(i);    //warp it into object
                  list.add(j);
                  Correct me if anything wrong =)
                  • 6. Re: JAVA primitive data types - Signed/ unsigned etc.
                    Puce
                    List list=new ArrayList<Integer>();
                    int i=5;
                    //list.add(i);    //wont work because can't add int
                    to Collection
                    Integer j=new Integer(i);    //warp it into object
                    list.add(j);
                    Correct me if anything wrong =)
                    At least this was the case before Java v5.0 (though you couldn't use generics there). Now:
                    List<Integer> list=new ArrayList<Integer>();
                    int i=5;
                    list.add(i);    //this will be automatically replaced with list.add(new Integer(i)); this feature is called auto-boxing
                    -Puce