Results 1 to 9 of 9
  1. #1
    spongepatrick is offline Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0

    Default Java variables question

    Hi

    So I'm reading this page on [Moderator edit: link removed]

    okay so it says that java int short and byte variables are the same thing. They take whole numbers. But what is the point of byte and short to even exist if int covers it all? Is the short and byte just for fun?
    Last edited by DarrylBurke; 03-15-2015 at 10:03 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Java variables question

    No, they are not 'the same thing' -- reda the definitions in the JLS: Chapter*4.*Types, Values, and Variables

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Java variables question

    They are all integral types (as explained in Darryl's link). It makes sense to have multiple types for integers with larger and smaller ranges so that storing and processing them can be as efficient as possible. If a type can store a wide range of integer values it will take up more space on your disk drive, take longer to read and write to the drive or across the network, and arithmetic involving the values will more complicated.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java variables question

    Quote Originally Posted by pbrockway2 View Post
    They are all integral types (as explained in Darryl's link). It makes sense to have multiple types for integers with larger and smaller ranges so that storing and processing them can be as efficient as possible. If a type can store a wide range of integer values it will take up more space on your disk drive, take longer to read and write to the drive or across the network, and arithmetic involving the values will more complicated.
    That's advice for computers of 15 years ago, nowadays a 1 byte data type is still likely to consume up a whole 4 or 8 bytes of memory due to memory alignment. Disc storage is then very much dependent on the file system used.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Java variables question

    Thanks, gimbal2. I didn't read my own post carefully enough and it was sloppily worded.

    The various types of integer value are, completely, a language issue and, as such, what difference they make (or whether they make any at all) depend on the implementation of the language. The JVM mandates some things but many others are whatever they are. At best the language design allows for efficiencies that might be made. (And not will be, as I misstated.)

    And I didn't intend to offer advice ("use this type rather than that..."). The advice I *would* offer is to keep it simple (double is good!) and follow the lead of the standard API (if something returns a char, that's probably for a reason - and the reason is unlikely to be related to efficiency).

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

    Default Re: Java variables question

    Quote Originally Posted by gimbal2 View Post
    That's advice for computers of 15 years ago, nowadays a 1 byte data type is still likely to consume up a whole 4 or 8 bytes of memory due to memory alignment. Disc storage is then very much dependent on the file system used.
    It is still important. Remember you can have arrays of those data types. So even though the array may be aligned on a 32bit or 64bit boundary, each array element only takes up as much space as the type represents.

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

  7. #7
    spongepatrick is offline Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0

    Default Re: Java variables question

    The page I was talking about was - here

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Java variables question

    First, (last), and prejudiced impressions are that the page linked to is cr@ppy. Walls of unindented code, and bad orthography using capitals for Java keywords (on a page talking about types!). There is better. Google provides the links. The advisable discretion you supply yourself.

    ---

    For penance I read up on Java primitive types and why you might select one over another. But I'm a primitive type myself and will stick with the advice in my second post. I am currently doing a Coursera course on digital signal processing and the details of jpeg and mp3 encoding (and the advantage of judiciously reducing precision) was what was going through my mind when I first replied.

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

    Default Re: Java variables question

    I might also add that in some cases it might be more efficient to use say, ints over bytes, shorts, or longs. One example that comes to mind is the for loop. There are special bytecodes involving ints that make for loops more efficient (or at least use fewer bytecodes).

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

Similar Threads

  1. Quick question about initializing variables
    By TheRockIng in forum New To Java
    Replies: 1
    Last Post: 03-07-2015, 06:30 AM
  2. Question about declaring variables
    By salmontres in forum New To Java
    Replies: 2
    Last Post: 12-18-2011, 12:58 AM
  3. Replies: 3
    Last Post: 08-07-2011, 02:31 AM
  4. Replies: 17
    Last Post: 07-25-2010, 07:20 PM
  5. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 03:46 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
  •