Data Types
int A = 16;
char x = ‘B’ ;
Session Agenda
1 What is Data Type?
2 Types of Data Type?
3 Type Casting
4 Hands-on-Skills
What is Data Type?
Data type specifies the size and type of values that can be stored in an identifier
Primitive Non-Primitive
Data Types
Primitive Non-Primitive
Boolean Numeric Array String
Character Integral
Integer Floating Point
boolean char byte short int long float double
Data Types Range
✓ Integer data types are used to store numeric values.
Integral ✓ There are four different integer types in Java
Floating
Byte (1 byte) -128 to 127
Short (2 bytes) -32768 to 32767
Character
Int (4 bytes) -2147483648 to 2147483647
Boolean -9,223,372,036,854,775,808
Long (8 bytes) to
9,223,372,036,854,775,807
The Analogy of Data Type Sizes in Java: Visualizing with Glasses
-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
-2147483648
to
2147483647
-32768 to 32767
-128 to 127
Small Glass Medium Glass Large Glass Very Large Glass
(Byte) (Short) (Int) (Long)
The Analogy of Data Type Sizes in Java: Visualizing with Glasses
-2147483648
to
2147483647
-128 to 127
Small Glass Large Glass
(Byte) (Int)
Data Types Range
✓ Float data types are used to store numeric values along with
their decimal value.
Integral ✓ There are two different integer types in Java
Floating Float (4 bytes) +- 3.40282347E +38F
Double (8 bytes) +- 1.79769313486231570E + 308
Character
The float or double data type should never be used for precise values,
Boolean such as currency.
The default value of float is 0.0F
The default value of double is 0.0d
Data Types Range
✓ Character data types are used to store character values.
Integral ✓ Char data type is a single 16-bit unicode character
Floating
Char (2 bytes) 0 to 65,535
Character
Boolean
Data Types & Range
✓ Boolean data types are used to store boolean values.
✓ This data type is used for simple flag that track true or false
Integral conditions
Floating
boolean True / False
Character
Boolean
Hands-on-Skills
Primitive Data Type Demo Java Code
Different Data type
OUTPUT
Instance variable
How to find the size of a primitive data type in Java?
➢ Java has no sizeof operator to find the size of primitive data
types but all Java primitive wrappers except Boolean provide a
SIZE constant in bits that could be divided by eight to get the
size of a data type in bytes. Note that size of primitive types in
Java is always the same. It is not platform dependent.
➢ All primitive data types in Java are signed.
➢ Java does not support unsigned types.
Hands-on-Skills
K.K To Find the Size of Primitive Data Type in Java Code
OUTPUT
K.K Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
In Java, type casting is classified into two types,
Widening Casting (Implicit)
byte -> short -> int -> long -> float -> double
Widening
Narrowing Casting (Explicit)
double -> float -> long -> int -> short -> byte
Narrowing
K.K Widening Conversion
Widening conversion is allowed in the following cases:
byte can be converted to short, int, long, float, or
double
Short can be converted to
float can be converted to double int, long, float, or double
long can be converted to float Char can be converted to
or double int, long, float, or double
int can be converted to long, float, or double
K.K Narrowing Conversion
Narrowing conversion is allowed in the following cases:
Short can be converted to byte or char
Char can be converted to
Double can be converted to byte, byte or short
short, char, int, long, or float
Float can be converted to byte, Int can be converted to
short, char, int, or long byte, short, or char
Long can be converted to byte, short, or char
kundan.kumar011@gmail.com
Hands-on-Skills
K.K Type Casting Demo Java Code
Narrowing type casting
OUTPUT
Thank You