Data Types in java
Data Types in java
Data types:
Every variable has a type, every expression has a type and all types are strictly define more over every assignment should he checked by the compiler by the type compatibility therefore 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 in java which are non objects.
Diagram:
1. Numeric data types(used to represent numbers) is again categorized into 2 types:
11. Integral data types
12. Floating point data types
11. Integral data types is categorized into 4 types:
111. byte
112. short
113. int
114. long
12. Floating point data types is categorized into 2 types:
121. float
122. double
2. char data types(used to represent characters)
3. boolean data types(used to represent numbers)
Except Boolean and char all other data types are considered as signed data types because we can represent both "-ve" and"+ve" numbers.
Integral data types :
Byte:
Size: 1 byte (8 bits)
Max value: +127
Min value: -128
Range:-128 to 127
Example:
x 1 1 1 1 1 1 1
Here x is signbit
0----------(+ve) 2^6*1+2^5*1+2^4*1+2^3 *1+2^2 *1+2^1 *1+2^0+1
64*1+ 32*1+ 16*1+ 8*1+ 4*1+ 2*1+ 1*1
1----------(-ve) +127
- The most significant bit acts as sign bit. "0" means "+ve" number and "1"
means "-ve" number.
- "+ve" numbers will be indicated directly in the memory whereas "-ve"
numbers will be represented in 2's complement form.
Example:
byte bt=10:
byte bt2=130; //C.E:possible loss of precision
byte bt=10.5; //C.E:possible loas of precision
byte bt=true; //C.E:incompatible types
byte bt="ashok" ; //C.E:incompatible types.
byte data type mostly suits if we are handling data in terms of streams either from the file or from the network.
Short:
Short is the datatype that is used rarely in our program.
Size: 2 bytes
Range: -32768 to 32767
Example:
short sh=130;
short sh=32768; //C.E:possible loss of precision
short sh=true: //C.E:incompatible types
Short data types in java is mostly suit 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:
Int is most commonly used data type in java.
Size: 4 bytes
Range:-2147483648 to 2147483647
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 use long data type in our program.
Example:
To hold the number of characters present in a big file int may not enough hence the return
type of length() method is long.
long lg=f.length (); //f is a file
Size: 8 bytes
Range: -2^63 to 2^63-1
Important 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 use floating point data types.
Floating Point Data types:
1. Float
1. If we want to 5 to 6 decimal places of
accuracy then we should go for float.
3. Range: -3.4e38 to 3.4e38.
4. double follows double precision.
2. double
1. If you want to 14 to 15 decimal places of accuracy then we should go for double.
2. Size:4 bytes.
3. Range: -1.7e308 to 1.7e308.
4. float follows single 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 bl=true;
boolean bl=True; //C.E: cannot find symbol
boolean bl="True"; //C.E: incompatible types
boolean bl=0; //C.E:incompatible types
Char data type:
In old languages like C & C++ are ASCII code based the no.Of ASCI code characters are < 256 to represent these 256 characters 8- bits enough hence char size in old languages 1 byte.
In java we are permitted to use any worldwide alphabets character and java is Unicode based and number 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 thl=97:
char th2=65536; //C.E:possible loss of precision
Important Note: The default value for the object reference is "null".
"Work for it more than you hope for it."
Happy Reading.Happy Learning!!! See you in next post :)
Post a Comment