Data Types


Why Java is strongly typed programming language ? 

In java every variable and every expression has some type.
Each and every data type is clearly defined.
Every assignment should be checked by compiler for type compatibility.
Because of above reasons we can concluded that java language is strongly typed programming language.

Java is pure Object Oriented programming language or not ?

Java is not considered as pure OOP language because several OOP feature are not satisfied by Java like operator overloading, multiple inheritence etc.
Moreover we are depending on primitive data types, which are non objects.

Primitive data types: 8 

  •  Numeric data types
    1. Integral data types
         * byte, short, int, long
2. Floating point data types
* float, double

  •  Non Numeric data types
                 * char, boolean


Except boolean and char remaining data types are considered as signed data type because we can represent both +ve and -ve numbers.


byte : 
size : 1 byte = 8 bit
max value : all 7 bits = 1 and first bit represents sign. --> +127
min_value : -128
range: -128 to +127
default : 0

eg:  byte b = 10;
byte b = 127;
byte b = 128;   // compile time error  --> possible loss of precision found int but require byte
byte b = 10.5;  // compile time error  --> possible loss of precision found double but require byte
byte b = true;  // compile time error  --> incompatibe type found boolean but require byte
byte b = "durga"; //  incompatibe type found string but require byte

When to use ? 
byte is the best choice if we want to handle data in terms of streams either from the file or from the network.
File supoorted form or network supported form is byte.


-------------short------------------
Most rare used data type
size : 2 byte (16 bits)
range:-2^15 to 2^15 - 1 (-32768 to +32767)
default : 0

ex: short s = 32767;
short s = 32768;  // compile time error  --> possible loss of precision found int but require short
short s = true;   // incompatible types found boolean require short

When to use ? 
best suitable for 16 bit 8085 microprocessor but these are outdated that's why short is rarely used.


-----------int------------------------
Most common used data type is int

size : 4 bytes (32 bit)
range: -2^31 to 2^31 ( -2147483648 to 2147483647)
default : 0

int x = 2147483647;
int x = 2147483648;  //compile time error  --> integer number too large
int s = 2147483648L; //compile time error  --> possible loss of precision found long but require int
int x = true;  //  incompatible types found boolean require int

---------long-----------------------
sometimes int may not enough to hold big values then we should go for long type
1) amount of distance travelled by light in 1000 days. For this we should go for long data type.
long l = 8457851546845153456151;

Number of characters present in a file may exeed int range , Hence the return of length() method is long but not int.
long l = f.length();

Size : 8 bytes
Range: -2^63 to 2^63 - 1
default : 0
Note: All the above data types (byte,short,int,long) meant for representing integral values. If we want to represent floating point values then we should go for floating-point data types.

-----------FLOATING POINT DATA TYPES------------
--> float
--> double

[When to use float data type] [When to use double]
1) 5 to 6 digit accuracy         10 to 15 digit accuracy
2) Single Precision - Less accuracy Double Precision - More Accuracy
3) Size : 4 bytes Size : 8 bytes
4) Range: -3.4e38 to 3.4e38 Range : -1.7e38 to 1.7e38
5) default : 0.0         default : 0.0

----------Non-Integral Data Types
-->boolean
-->char

-------boolean---------
Size : Not Applicable in java (Virtual achine Dependent)
Range: Not Applicable but alllowed values are True/False
default : false
eg boolean b = true;
boolean b = 0;  //compile time error    incompatible types found int required boolean
boolean b = True;  //compile type  cannot find symbol - variable true in class classname
boolean b = "True";// incompatible types found string required boolean

int x = 0;
if(x){ --> compile time error   incompatible type found int required boolean
...
...
}
else{
...
}



-------char------------

Size : 2 byte  (1 byte in C and C++)

Why size is 2 byte in Java? 
Java is  Unicode base language while old are ASCII based.
Old Languages ( C/C++) are ASCII code based and no. of allowed different ASCII code characters are <= 256 , therefore to represent these 256 characters , 8 bits are enough, Hence the size of char in old languages is 1 byte.
But JAVA is unicode based and the number of diiferent unicode characters are > 256 <= 65536, Therefore to represent these many characters 8 bits may not enough , compulsory we should go for 16 bits. Hence the size of char in JAVA is 2 bytes.


Size : 2 bytes
Range: 0 to 65535
default : 0 [0 represents space]


Comments

Popular posts from this blog

Variables (Part-1)

length vs length()

Arrays in Java