Thursday 12 January 2017

DataTypes in Java with Size


                                             


 Java defines 8 simple types of data : byte , short , int , long , char , float , double , boolean. These can be categorized into 4 groups:

- Integers This group includes byte , short , int , long , which are whole valued signed number. 

- Floating Point Number This group includes float and double , which represent numbers with fractional precision.

- Characters This group includes char , which represent symbols in a character set , like letters and numbers.

- Boolean This group includes boolean , which is a special type of representing true / false values.


Integers 

Java defines 4 Integer types : Byte , Short , Int , Long . All of these are signed positive and negative values. Java does not support unsigned , positive only integers.

The width of an integer type should not be thought of as the amount of storage it consumes , but rather as the behavior  it defines for variables and expressions of that type.

-Byte 

The smallest integer type is byte. The variables of type byte are especially useful when you're working with a stream of data from a network or file.

              Width                                  Range
        
                8 bits                                   -128 to 127

-Short

Short is probably the least used java datatype , since it is defined as having its high byte first . It is mostly applicable to 16-bit computers , which are scarce these days.

            Width                                  Range

             16 bits                                -32,768 to 32,767

-Int

Int is the most commonly used integer type. The variable of type int are commonly employed to control loops and to index arrays.

The int type is most versatile and efficient type and is used most of time when you want tot create a number for counting or indexing arrays or doing integer maths.

           Width                                     Range

           32 bits                              -2,147,483,648 to 2,147,483,647

-Long 

Long is signed bit and is useful in those occasions where an int type is  not large enough to hold the desired value. It is useful when big , whole numbers are needed.

          Width                                  Range

          64 bits                      -9,223,372,036,854,775,808    to                                                             9,223,372,036,854,775,807    

Floating Point Type

Floating point number , also known as real numbers , are used when evaluating expressions that require fractional precision. There are 2 types of floating point types , float and double.

-Float

The type float specifies a single precision value.Single precision is faster on processors and takes half as much space as double precision. Float is useful when you need a fractional component but do not require a large degree of precision.

              Width                                  Range 

               32 bits                                 1.4e-045 to 3.4e+038

-double

The type double has double precision. Double precision is actually faster than single precision on some modern processor that have been optimized for high speed mathematical calculation.

             Width                                    Range 

            64 bits                                    4.9e-324 to 1.8e+308

Characters

In java , char is used to store characters. In java , char is of 16 bits.
the first 8 bits are used to represent ASCII characters and rest 8 bits is used for ISO-Latin-1 that includes various character sets such as Latin , Arabic , Hebrew , Katakana and many more.


Boolean

Java has simple type , called boolean  , for logical values. It can have only one of the possible values  , true or false. It is returnes by all relational operators , such as a<b.

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *