In Java, variables and expressions are of 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 conclude that Java is strongly typed programming language.
Java is not considered as pure object-oriented programming language because several OOP’s features are not satisfied by java (like operator overloading, multiple inheritance etc.). Moreover, we are depending on primitive data types in Java which are non-objects.
Except boolean and char remaining data types are considered as signed data types because we can represent both positive and negative numbers.
Size : 1 byte ( 8 bits ) MAX_VALUE : + 127 MIN_VALUE : - 128 Range : - 128 to 127
byte is the best choice if we want to handle data in terms of streams either from the file or from the network (files supported form or network supported form is byte).
Representation of byte data type in memory
byte b = 10 ; //valid
byte b= 127; //valid
byte b =128; //invalid
This is the foremost rarely used data type in java.
short data type is best suitable for 16- bit processors like 8085 but these processors are completely outdated and hence corresponding short data type is also outdated data type.
short s = 32767; //valid
short s = 32768; //invalid
The most commonly and generally used data type in java is int.
int i = 2147483647; //valid
int i = 2147483648; //invalid
Sometimes int may not enough to hold big values then we should go for long type.
Example1 : The amount of distance travelled by light in 1000 days, to held this value int may not enough we should go for long data type.
long l = 1,26,000 x 60 x 60 x 24 x 1000 miles;
Example2 : The number of characters present in a big file may exceed int range hence the return type of length method is long but not int.
long l = f.length( ) ;
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
Size : Not Applicable [ virtual machine(VM) dependent ]. Range : Not Applicable [ Allowed values are : true / false].
boolean b =true; //valid
boolean b = 1; //invalid
Old languages ( like C or C++ ) are ASCII code based under the number of allowed different ASCII coder are less than or equal to 256.
To represent these 256 characters 8 bits are enough hence the size of char in old language 1 bytes.
But java is UNICODE based and the number of different UNICODE characters are less than 256 and less than or equal to 65536. 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.
1 bits -> 2 character [ 0, 1]
2 bits -> 4 character [ 00, 01, 10, 11]
3 bits -> 8 character
4 bits -> 16 character
5 bits -> 32 character
6 bits -> 64 character
7 bits -> 128 character
8 bits -> 256 character
Size : 2 bytes. Range : 0 to 65535.
char ch = ‘a’; //valid
char ch = “a”; //invalid
Summary of java primitive data types :
Note : null is the default value for object reference and we can’t apply for primitives if we are trying to use for primitive then we will get compile time error.
char ch = null ; // invalid
Compile-time error : incompatible types
found : null type
required : char