Lecture 2.1 - Data Type & Type Casting
Lecture 2.1 - Data Type & Type Casting
Data Types
Primitive Data Types Non Primitive Data Types
char
byte short int long
4
Primitive data types: Primitive data types are predefined types of data, which
are supported by the programming language. For example, integer, character,
and float are all primitive data types. Programmers can use these data types
when creating variables in their programs.
Non-primitive data types: Non-primitive data types are not defined by the
programming language, but are instead created by the programmer. They are
sometimes called "reference variables," or "object references," since they
reference a memory location, which stores the data. In the Java programming
language, non-primitive data types are created, rather than predefined. While
an object may contain any type of data, the information referenced by the
object may still be stored as a primitive data type.
5
int count = 0;
7
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
9
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
10
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
11
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: Let’s 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
Type Casting
Type Casting:
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
Type Casting
Implicit Type Casting
Type Casting
Explicit Type Casting
Type Casting
Example: 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
Type Casting
Example: Explicit Type Casting
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
Type Casting
Some more Type Castings
Type Casting
Some more Type Castings
Explicit Explicit