Java Primitive Data Types

Discussion in 'Resources' started by emericask8ur, Dec 5, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    emericask8ur

    Java Primitive Data Types

    Data type: The type of value that a variable can hold is called data type. When we declare a variable we need to specify the type of value it will hold along with the name of the variable. This tells the compiler that the particular variable will hold certain amount of memory to store values. For example, in the lines of code above num is int type and takes two bytes to hold the integer value, bol is a boolean type and takes one bit to hold a boolean value .
    Data Type | Description | Size | Default Value
    boolean | true or false | 1-bit | false
    char | Unicode Character | 16-bit | \u0000
    byte| Signed Integer |8-bit | (byte) 0
    short |Signed Integer |16-bit | (short) 0
    int | Signed Integer | 32-bit |0
    long |Signed Integer | 64-bit |0L
    float | Real number | 32-bit |0.0f
    double |Real number |64-bit |0.0d
    Thought this was nice to share!
     
  2. Offline

    Malchus

    Be nice if we could get an explanation of what the description/size/default value all mean.​
    Also would be cool if you could give examples and explain what specific variables are used for.​
    Regards.​
     
  3. Offline

    Sleaker

    description - what the data type holds.
    size - memory size of the data type. This should be self explanatory.
    default value - again this should be self-explanatory. this is the default value of the data type when instantiated. primitives in java can not be null, they always are instantiated with their default value if no other value is supplied.


    byte, short, int - All whole number values. generally there's no reason to use byte or short unless you are specifically wanting to conserve memory space and know you don't need to go above a specific number limit.
    Long - whole numbers only, but useful for timing. in fact nearly all time operations are handled with Long Integers.

    Float has the same logic as the byte/short usage. This is for decimal values
    Double - decimal value, just uses more space than a float, but has much higher min/max value and can be much more precise as it uses twice as much data.

    boolean - obv if you only need true/false this is the best way to do it.

    char - not used very often because generally you want Strings. So I don't really know a good use for this.

    Min/Max values:
    Byte: -128 to 127
    Short: -32768 to 32767
    Int: -2147483647 to 2147483647
    Long: − (2^63) to 2^63 − 1

    Double Max: 1.7976931348623157 x 10^308
     
Thread Status:
Not open for further replies.

Share This Page