Lecture 2.1 - Data Type & Type Casting
Lecture 2.1 - Data Type & Type Casting
Course Code: CSC 1205 Course Title: Object Oriented Programing 1 (JAVA)
Data Types
Primitive Data Types Non Primitive Data Types
char
byte short int long
Data Types in Java
Data type summary
Data Type Memory Size It means that if we declare a variable of int type, it
boolean 1 Byte will occupy 4 Bytes of memory.
Data Type Minimum Value Maximum Value Default Value boolean has only
boolean - - false 2 values:
true and false
byte -27 27 -1 0
short -215 215 -1 0 Question is,
How to calculate
int -231 231 -1 0
Value ranges for
long -263 263 -1 0L the other data
char ‘\u0000’ ‘\uFFFF’ ‘\u0000’ types?
-3.4 x 1038 -1.4 x 10-45
float 0.0F Formula:
1.4 x 10 -45
3.4 x 10 38
If number of bits = n
-1.79 x 10308 -4.9 x 10-324
double 0.0 Min Value = -2n-1
4.9 x 10 -324
1.79 x 10 308
Max Value =
2n-1– 1
Data Types in Java
Calculating Value Ranges
Data Type Minimum Value Maximum Value Size of byte is 1Byte [8 bits]
byte -128
-27 2127
7
-1
Known as Sign Bit. Represents
whether the number is Positive or
Lets, declare two byte type variable and draw negative.
the memory representation for them If the value of sign bit is 1, it is a
negative number
byte b1, b2; If the value of sign bit is 0, it is a
positive number
b1 1 0 0 0 0 0 0 0 Equivalent decimal
Rest of the is -128
bits are 0
b2 0 1 1 1 1 1 1 1 Equivalent decimal
Rest of the is 1
bits are 127
The same approach can be followed for short, int and long
Data Types in Java
Calculating Value Ranges
Data Type Minimum Value Maximum Value Size of char is 2 Bytes [16 bits]
char ‘\u0000’ ‘\uFFFF’
Values of char data type are
unsigned
Lets, declare two char type variable and draw
the memory representation for them
c1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 All the
Equivalent bits are 0 0000
Hexadecimal
c2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
All the
Equivalent bits are 1 FFFF
Hexadecimal
Data Types in Java
Wrapper Class
A wrapper class is a java library class whose object wraps or contains a primitive
data type.
Data Type Wrapper Class Wrapper classes contains methods that are required
boolean Boolean for performing some operations regarding their
respective primitive data type.
byte Byte
short Short For Example: Lets say we want to convert a String
value to an integer value or double value. The Integer
int Integer class has a method to convert the String into an
long Long integer and the Double class has a method that
char Character
converts a String value to a double value.
Type Casting is the process of converting the value of a primitive data type to
another primitive data type. Example: Converting an integer value to a double value
and vice versa, Converting a char value to an integer and vice versa etc.
There are two types of type casting:
1. Implicit Type Casting
2. Explicit Type Casting
Implicit Type
Explicit Type Casting:
Casting:
• Converting the value of a larger
smallerprimitive
primitivedata
datatype
typetotoa asmaller
larger primitive data
type.
No possibility
• Possibility of value
of value lossloss during
during the the conversion.
conversion exists.
Type Casting
Implicit Type Casting
Lets, declare one byte type variable, one short type variable and draw the memory
representation for them
byte b1;
short s1;
Assigning the
Equivalent
Equivalent value
binary ofof
120gets
decimal
Assigning b1
ins1 isin120
stored
b1 s1 b1 0 1 1 1 1 0 0 0
s1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0
Lets, declare one short type variable, one byte type variable and draw the memory
representation for them
short s2;
byte b2;
s2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0
Equivalent decimal
binary
Assigning the
130 value is
of-126.
in s2gets stored
s2 in b2in s2 b2 1 0 0 0 0 0 1 0
Explicit Explicit