0% found this document useful (0 votes)
2 views

Lesson 2- Java Data Types

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types, and detailing their ranges and sizes. It also explains type casting, including widening and narrowing conversions, along with practical examples. Additionally, it highlights the lack of a sizeof operator in Java and the fixed sizes of primitive data types.

Uploaded by

Kundan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson 2- Java Data Types

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types, and detailing their ranges and sizes. It also explains type casting, including widening and narrowing conversions, along with practical examples. Additionally, it highlights the lack of a sizeof operator in Java and the fixed sizes of primitive data types.

Uploaded by

Kundan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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

[email protected]
Hands-on-Skills
K.K Type Casting Demo Java Code
Narrowing type casting

OUTPUT
Thank You

You might also like