Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By PhHein

Thread: Primitive Data Types

  1. #1
    Join Date
    Jun 2013
    Location
    United States
    Posts
    7
    Rep Power
    0

    Post Primitive Data Types

    I understand that the Java programming language is statically-typed, which means that all variables must first be declared before they can be used. Easy enough, right? Anyways, there are some things that I still do not understand about a variable's data type. For example, everything. What is the difference between a byte and short data type? I've read through the Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics), but I do not understand what the website is trying to convey. I apologize for asking a question that may be simple in the first place, but if I am unable to learn the difference between primitive data types, then I will not be able to continue with the Java course. As always, all replies are appreciated. Thank you in advance for your help.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Primitive Data Types

    A byte is 8 bit signed number and short is 16 bit signed number
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Jun 2013
    Location
    United States
    Posts
    7
    Rep Power
    0

    Post Re: Primitive Data Types

    Quote Originally Posted by PhHein View Post
    A byte is 8 bit signed number and short is 16 bit signed number
    Thank you very much for responding to my thread, PhHein. I regret to inform you that I am not the sharpest tool in the shed, and that I have another question to ask of you. When do you know which primitive data type to choose when using a certain integer?
    Last edited by MarkLearningJava; 06-28-2013 at 11:40 AM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Primitive Data Types

    Normally you always use int as long as you're below 2^31-1. As the tutorial says you can save some memory when using huge amounts of numbers being below 2^7-1 or 2^15-1.
    The other use of byte and short is API methods returning those, or using them as parameters.
    Last edited by PhHein; 06-28-2013 at 12:18 PM.
    MarkLearningJava likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default Re: Primitive Data Types

    The types byte, char, short and int also take 4 bytes when stored on a stack (as local values or parameters) or when they are a (static or non-static) class member. Only as array elements they take 1 or 2 bytes of memory in a particular JVM implementation. The reason for this is speed: all class objects are multiples of 4 bytes. Except for doubles and longs, all primitive types take exactly one slot of 4 bytes. This increases the size of the data somewhat but it speeds up the code tremendously.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Primitive Data Types
    By CuppaCoffee in forum New To Java
    Replies: 6
    Last Post: 05-08-2013, 10:49 PM
  2. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 03:27 PM
  3. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 05:43 AM
  4. primitive Data types
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 08:29 PM
  5. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 07:29 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
  •