Results 1 to 4 of 4

Thread: Short/Integer

  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default Short/Integer

    java.lang.Number has following subclasses:
    BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short

    I am exploring datatypes and I am confused when to use Short and when to use Integer. It appears to me that both can save same values.

    Java Code:
    		Short s = 22;
    		Integer i = 23;
    Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Yes, up to the max value for the short data type. Then you have to use int up until you reach its max value. Then you have to use long.
    Java Code:
    System.out.printf("max short value = %d%n" +
                              "max int value   = %d%n" +
                              "max long value  = %d%n",
                               Short.MAX_VALUE, Integer.MAX_VALUE,
                               Long.MAX_VALUE);

  3. #3
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Great. That was helpful.
    So using Integer for values less than 32767 is waste of memory.

  4. #4
    comando2929 is offline Member
    Join Date
    Dec 2007
    Location
    So Cal
    Posts
    4
    Rep Power
    0

    Default

    If all your doing is storing and looking up the data, a short or byte is just fine. However, if you do any other processing with a non int type, it must be converted to an int which causes a slow down in calculations. This is further explained at.

    Java Primative Speed
    Last edited by comando2929; 12-06-2007 at 09:36 PM. Reason: add signiture

Similar Threads

  1. Integer Usage
    By bluegreen7hi in forum New To Java
    Replies: 11
    Last Post: 01-24-2008, 08:54 AM
  2. why doesn't this short applet work?
    By kim85 in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:43 PM
  3. VeryLong Integer.. help
    By hey in forum New To Java
    Replies: 4
    Last Post: 12-14-2007, 09:48 PM
  4. Replies: 0
    Last Post: 12-04-2007, 11:11 AM
  5. Integer vs int
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 09:13 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
  •