DATA TYPES:
In Java every variable and every expression has some type.Each & every data type is clearly defined.
Every assignment should be checked by compiler for type compatibility.
Because of above reasons we can conclude 'strongly Typed' programming language.
Java is not considered as pure Object oriented Programming language because several oop features are not satisfied by java like operator overloading and multiple Inheritance etc.,
Moreover we are depending on primitive data types which are non Objects.
PRIMITIVE DATA TYPES(8):
1.Numeric Data Types
i)Integral Data Types
a)byte
b)short
c)int
d)long
ii)Floating point Data Types
a)float
b)double
2.Non Numeric Data Types
i)char
ii)boolean
Except boolean and char remaining data types considered as signed data types because we can represent both positive and negative numbers.
SIZES:
1 byte=8 bits
Example:
For example take " xabcdefg(01010011)"
In above example 'x' and '0' is the most significant bit(MSB) and we called it as a 'sign bit'(i.e.,0 means +ve, 1 means -ve) and remaining 7 bits represents Value.
The maximum value we can represent with 8 bits is +127(01111111).
Q)How '01111111' become 127 ?
The maximum value we can represent with 8 bits is +127(01111111).
Q)How '01111111' become 127 ?
A)we calculate the value of 01111111 is from right to left as 2^6+2^5 +2^4+2^3+2^2+2^1+2^0=127
Maximum negative value is -128 this possible with two's complement.
So Range is -128 to +127
Example:
byte x=128;
we get compile time error as possible loss of precision
found :int
required:byte
byte y=true;
we get compile time error as incompatible types
found :boolean
required:byte
byte c="praveen"
we get compile time error as incompatible types
found:java.lang.string
required :byte
Maximum negative value is -128 this possible with two's complement.
So Range is -128 to +127
Example:
byte x=128;
we get compile time error as possible loss of precision
found :int
required:byte
byte y=true;
we get compile time error as incompatible types
found :boolean
required:byte
byte c="praveen"
we get compile time error as incompatible types
found:java.lang.string
required :byte
short:
It is the most rarely used data type in java.
Size:2 bytes(16 bits)
Range:-(2^15) to (2^15)-1
-32768 to 32767
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 out dated data type.
int:
The most commonly used data type in java is 'int'.
size:4 bytes(32 bits)
Range:-(2^31) to (2^31)-1
-2147483648 to 2147483647
Example:
int x=2147483648;
compile error:Integer number is too large
int x=2147483648l;
compile error:possible loss of precision
found:long
required:int
long:
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();
size:8 bytes
range:-(2^63) to (2^63)-1
Note:All the above data types( byte,short,int,long) meant for representing integral values, if you want to represent floating point values then we should go for floating point data types.
No comments:
Post a Comment
If you Like my blog Spread it and help friends for whom this blog is useful for their career.