JAVA DATA T YPES
CHAPTER - III
STRONGLY T YPED DATA T YPE
• In java, every variable & every expression has some type.
• Each and every data type is clearly defined.
• Every assignment should be checked by the compiler for type compatibility.
• Because of the above reasons, we can conclude Java language is a strongly typed programming
language.
05-02-2023 Ankur Biswas (ANB) 2
IS JAVA A PURE OBJECT-ORIENTED
PROGRAMMING LANGUAGE?
• Java is not considered a pure object-oriented programming language because several object-
oriented programming features are not satisfied by java, like operator overloading, multiple
inheritances, etc. Moreover, we are depending on primitive data types which are non-objects.
• Some examples of purely object-oriented programming languages are:
• Smalltalk
• Ruby
• Scala
• Eiffel
05-02-2023 Ankur Biswas (ANB) 3
DATA T YPES
Primitive
Data Type
(8)
Non-
Numeric
Numeric
Floating
Integer char boolean
point
byte short int long float double
05-02-2023 Ankur Biswas (ANB) 4
PRIMITIVE DATA T YPES
The size of Boolean is Virtual
Machine Dependent
05-02-2023 Ankur Biswas (ANB) 5
NUMERICAL DATA T YPE
• Data Type - byte • Error Types:
byte b = ture;
• Size – 1 Byte – 8 Bits CE: incompatible type
found: boolean
Sign Bit required: byte
MSB 26 25 24 23 22 21 20 byte bt = 128;
CE: possible loss of precision
1 1 1 1 1 1 1 found: int
required: byte
0 → +ive
1 → -ive Note: If the size is out of the range, then possible
Two’s loss of precision error will occur. If non-numeric
• Max Value → +127 (27-1) Complement data is assigned to a numeric variable or vice-
• Min Value → -128 (27) Integer versa, possible loss of precision error will occur.
• Range → -128 to +127
05-02-2023 Ankur Biswas (ANB) 6
NUMERICAL DATA T YPE
• Data Type - short • Error Types:
short s = ture;
• Size – 2 Bytes – 16 Bits CE: incompatible type
found: boolean
Sign Bit required: byte
MSB 214 213 21 20 short bt = 38985;
CE: possible loss of precision
1 1 … 1 1 found: int
required: byte
0 → +ive
1 → -ive Note: If the size is out of the range, then possible
Two’s loss of precision error will occur. If non-numeric
• Max Value → +32767 (215-1) Complement data is assigned to a numeric variable or vice-
• Min Value → -32768 (215) Integer versa, possible loss of precision error will occur.
• Range → -32768 to +32767
05-02-2023 Ankur Biswas (ANB) 7
NUMERICAL DATA T YPE
• Data Type - int • Data Type - long
• Size – 4 Bytes – 32 Bits • Size – 8 Byte – 64 Bits
Sign Bit Sign Bit
MSB 230 229 21 20 MSB 263 262 21 20
1 1 … 1 1 1 1 … 1 1
0 → +ive 0 → +ive
1 → -ive 1 → -ive
• Max Value → +2,14,74,83,647 (231-1) • Max Value → +92,23,37,20,36,85,47,75,807(264-1)
• Min Value → -2,14,74,83,648 (231) • Min Value → -92,23,37,20,36,85,47,75,808 (264)
• Range → -2,14,74,83,648 to +2,14,74,83,647 • Range → -92,23,37,20,36,85,47,75,808 to
+92,23,37,20,36,85,47,75,807
05-02-2023 Ankur Biswas (ANB) 8
FLOATING POINT DATA T YPE
float double
• Used when 5-6 places decimal accuracy is • Used when 14-15 places decimal accuracy is
needed. needed.
• It is a single precision data type • It is a double-precision data type
• The size is 4 bytes • The size is 8 bytes
• Range: -3.4e38 to 3.4e38 • Range: -1.7e308 to 1.7e308
• Ex: 3.4f, 56.69f • Ex: 3.4, 56.69d
• Note: The default data type for decimal values is • Note: The default data type for decimal values is
double. To take float input, explicitly ‘f’ or ‘F” is to double so, explicitly ‘d’ or ‘D” may be added after
be added after the number as a suffix. the number as a suffix but not compulsory.
05-02-2023 Ankur Biswas (ANB) 9
NON-NUMERIC DATA T YPE
• Data Type - boolean
• Size: NA [ Virtual Machine Dependent] though in most cases 1 byte is allocated.
• Range: NA [ but allowed values are true or false]
int x=0; while(1)
if (x) {
{ S.o.p(“Hello”);
S.o.p(“Hello”); }
} CE: Incompatible type
else found: int
{ required: boolean
S.o.p(“Hi”);
} Note: Being strongly typed, only allowed values for
boolean are true & false.
05-02-2023 Ankur Biswas (ANB) 10
NON-NUMERIC DATA T YPE
• Data Type – char
• Size: 2 Bytes – 16 bits
• Range: Character representation of 0 – 255 ASCII (American Standard Code for Information
Interchange) and rest of Unicode representation.
05-02-2023 Ankur Biswas (ANB) 11
THANK YOU
05-02-2023 Ankur Biswas (ANB) 12