Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 91 - 2617_JSSPS_C-7
P. 91

    Primitive Data Types

                     Primitive data types are predefined or built-in data types and is named by a reserved keyword. They
                     are also called basic data types. The eight primitive data types supported by the Java programming
                     language are byte, short, int, long, float, double, boolean and char. Each data type occupies a fixed size
                     in memory.

                     Integer Type

                     Integer types can hold whole numbers such as 123, -96, etc. The size of the values that can be stored
                     depends on the integer type that we choose. Valid types are  byte,  short,  int  and  long.
                        byte: It is an 8-bit signed integer. It can store values between -128 and 127 (both extreme values are
                       included). By default it contains 0.

                         It is used to save memory in large arrays because a byte is 4 times smaller than an integer. It can also
                       be used in place of "int" data type.
                        Example: byte a = 10, byte b = -20.
                        short: The short data type is a 16-bit signed integer or 2 bytes. This is greater than byte in terms of
                       size and less than integer. Its range is -32,768 to 32767. By default it contains 0.

                        Example: short s = 10000, short r = -5000.
                                                                                                                      31
                        int:  The int data type is a 32-bit signed integer or 4 bytes, which has a minimum value of -2  and
                                                31 -1
                       a maximum value of (2 ) . The latest version of Java can use the int data type to represent an
                                                                                                              32 -1
                       unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of (2 ) . By default
                       it contains 0.

                        Example: int a = 100000, int b = -200000.
                                                                                                                           63
                        long: The long data type is a 64-bit integer or 8 bytes. The signed long has a minimum value of -2
                                                   63 -1
                       and a maximum value of (2 ) . The latest version of Java can use the long data type to represent an
                                                                                                           64
                       unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2 -1. By default it
                       contains 0L.
                        Example: long a = 100000L, long b = -200000L.

                                      Type                  Size            Default value           Example
                              byte                  1 byte                         0           byte b=10;
                              short                 2 bytes                        0           short s=1234;
                              int                   4 bytes                        0           int i = 123456;

                              long                  8 bytes                       0L           long d=123456L;

                     Float Type

                     Floating point types represents numbers with a fractional part, containing one or more decimals such
                     3.4f or 56.2. There are two types: float and double.
                        float: The float data type is a single-precision 32-bit IEEE 754 floating point or 4 bytes. The float
                       data type can store fractional numbers from 3.4e−038 to 3.4e+038, i.e., values up to 7 decimal digits.
                       Note that you should end the value with an "f".
                        Example: float a=4.64f, float b=34.6789f

                                                                                                   Java Programming     89

| 86 87 88 89 90 91 92 93 94 95 96 |