In a java there are are mainly two types of data types.
- Primitive data types: This data types include a Numeric datatypes , Char datatypes, Boolean datatypes
- Non-primitive data types - such as String, Arrays and Classes
- The most significant Bit is called "sign bit". 0 mans +ve value, 1 means -ve value
- +ve numbers represented directly in the memory where as -ve numbers represnted in 2's componenet form
- byte b =100;
- byte b =127;
- byte datatypes is best suitable if we want to handle data in termsof strreams either from the file or the network
- Most recently used datatypes in java is 'Short'
- short datatypes is best suitable if we are uisng 16-bit processors like 8086 but these processers are completly out dated ans hence correspending short datatypes is also out dated.
- In c language the size of int is varied from platform to platform for 16-bit processers it is 2-bytes but for 32-bit processor.
- The main advantege of this approach is read and write operation we can perform very effiently and performance will be improved. But the main Disadvanteges of this approach is the chang od failing c program is very very high if we changing plaform C program is not considered as rubust.
- But in java the Size of int is always 4-bytes irrespective of any platform. The main advantege of this platforms here Java is considered as robust language
- All the above datatypes (byte, short ,int, long) ment for representing whole values.
- if we want to respresent real numbers compulesary we should go for floating point datatypes.
Floating Point datatypes
- float
- double
|
|
|
|
Float |
double |
|
1.
Size: 4-bytes |
1. Size: 8-bytes |
|
2.
Range: - -3.4e38 to 3.4e38 |
2. Range: - -1.7e308 to 1.7e38 |
|
3.
If we want 5 to 6 decimal places of accuracy then we
should go for float |
3. If we want 14
to 15 decimal places of accuracy then we should go for double |
|
4.
Float follows single precision |
4. Double
follows double precision |
Boolean Datatypes
Size = Not Applicable ( virtual machine dependency)
Range = Not Applicable (but allowed values are true/false)
Ex.
- Boolean one = false
Char Datatypes
In language like C/C++ we can use only ASCI characters and to respresent all ASCI characters 8-bits are enough. hence char size is 1-byte.
But in java we can use unicode characters which covers world wide all alphabets sets. The number of unicode characters is '>256' . hence 1-byte is enough to respresent all characters compulsary we should go for 2-bytes.
Size = 2-bytes
Range = 0 to 65535
Read also.
Summary of primitive data types
| Data Type | Default Value (for fields) |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | '\u0000' |
| String (or any object) | null |
| boolean | false |
