Java Data types

                                       Data Types

Every variable has a type, every expression has a type and all types are strictly define more over every assignment should be checked by the compiler by the type compatibility hence java language is considered as strongly typed programming language.

Java is pure object oriented programming or not?

Java is not considered as pure object oriented programming language because several oops features (like multiple inheritance, operator overloading) are not supported by java moreover we are depending on primitive data types which are non objects.

Diagram:

Except Boolean and char all remaining data types are considered as signed data types because we can represent both “+ve” and”-ve” numbers.

Integral data types :

Byte:

Size: 1byte (8bits)

Maxvalue: +127

Minvalue:-128

The most significant bit acts as sign bit. “0” means “+ve” number and “1” means “–ve” number.

“+ve” numbers will be represented directly in the memory whereas “–ve” numbers will be represented in 2’s complement form.

Example:

byte b=10;

byte b2=130;//C.E:possible loss of precision found : int

required : byte

byte b=10.5;//C.E:possible loss of precision byte b=true;//C.E:incompatible types byte b=”ashok”;//C.E:incompatible types

found : java.lang.String

required : byte

byte data type is best suitable if we are handling data in terms of streams either from the file or from the network.

Short:

The most rarely used data type in java is short.

Size: 2 bytes

Range: -32768 to 32767(-215  to 215-1)

Example:

short s=130;

short s=32768;//C.E:possible loss of precision short s=true;//C.E:incompatible types

Short data type is best suitable for 16 bit processors like 8086 but these processors are completely outdated and hence the corresponding short data type is also out data type.

Int:

This is most commonly used data type in java.


   
   
Size: 4 bytes to 231-1)
Range:-2147483648 to 2147483647 (-231

Example:

int i=130;

int i=10.5;//C.E:possible loss of precision int i=true;//C.E:incompatible types

long:

Whenever int is not enough to hold big values then we should go for long data type.

Example:

To hold the no. Of characters present in a big file int may not enough hence the return type of length() method is long.

long l=f.length();//f is a file

Size: 8 bytes

Range:-263  to 263-1

Note: All the above data types (byte, short, int and long) can be used to represent whole numbers. If we want to represent real numbers then we should go for floating point data types.

Floating Point Data types:

         
  Float   double  
         
  If we want to 5 to 6 decimal places of   If we want to 14 to 15 decimal places of  
  accuracy then we should go for float.   accuracy then we should go for double.  
         
  Size:4 bytes.   Size:8 bytes.  
         
  Range:-3.4e38 to 3.4e38.   -1.7e308 to1.7e308.  
         
  float follows single precision.   double follows double precision.  
         

boolean data type:

Size: Not applicable (virtual machine dependent)

Range: Not applicable but allowed values are true or false.

Which of the following boolean declarations are valid?

Example 1:

boolean b=true;

boolean b=True;//C.E:cannot find symbol

boolean b=”True”;//C.E:incompatible types

boolean b=0;//C.E:incompatible types

Example 2:

Char data type:

In old languages like C & C++ are ASCII code based the no.Of ASCII code characters are < 256 to represent these 256 characters 8 – bits enough hence char size in old languages 1 byte.

In java we are allowed to use any worldwide alphabets character and java is Unicode based and no.Of unicode characters are > 256 and <= 65536 to represent all these characters one byte is not enough compulsory we should go for 2 bytes.

Size: 2 bytes

Range: 0 to 65535

Example:

char ch1=97;

char ch2=65536;//C.E:possible loss of precision

Summary of java primitive data type:

                         
    data   Size   Range   Corresponding   Default value    
    type       Wrapper class      
                     
                       
    byte   1 byte   -27 to 27-1(-128 to 127)   Byte   0    
                         
    short   2 bytes   -215 to 215-1 (-32768 to   Short   0    
        32767)        
                       
                       
    int   4 bytes   -231 to 231-1 (-2147483648   Integer   0    
            to 2147483647)            
                       
    long   8 bytes   -263 to 263-1   Long   0    
                       
    float   4 bytes   -3.4e38 to 3.4e38   Float   0.0    
                       
    double   8 bytes   -1.7e308 to 1.7e308   Double   0.0    
                         
    boolean   Not   Not applicable(but   Boolean   false    
      applicable   allowed values true|false)        
                       
    char   2 bytes   0 to 65535   Character   0(represents    
            blank space)