0% found this document useful (0 votes)
4 views1 page

Java Type Conversion & Casting Explained

The document explains type casting, type conversion, and automatic type promotion in Java, highlighting that Java is a strongly typed language. It details implicit conversion (widening) and explicit conversion (narrowing), along with the rules for automatic type promotion in expressions. The conclusion emphasizes the importance of these concepts for safe data management in Java.
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)
4 views1 page

Java Type Conversion & Casting Explained

The document explains type casting, type conversion, and automatic type promotion in Java, highlighting that Java is a strongly typed language. It details implicit conversion (widening) and explicit conversion (narrowing), along with the rules for automatic type promotion in expressions. The conclusion emphasizes the importance of these concepts for safe data management in Java.
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

Report: Type Casting, Type Conversion & Automatic Type Promotion in Java

1. Introduction
Java is a strongly typed language. It supports two main ways of converting data:
Type Conversion (Implicit) and Type Casting (Explicit). Java also performs automatic
type promotion inside expressions.

2. Type Conversion (Implicit Conversion)


Implicit conversion happens automatically when a smaller data type is assigned
to a larger type. Also known as widening conversion.
Widening Hierarchy: byte → short → int → long → float → double
Example:
int a = 50;
double b = a;

3. Type Casting (Explicit Conversion)


Type casting is manual and used for narrowing conversion.
Example:
double x = 45.78;
int y = (int)x;

4. Automatic Type Promotion in Expressions


Java promotes smaller types to larger ones when evaluating expressions.
Rules:
• byte, short, char → int
• If long present → result long
• If float present → result float
• If double present → result double

Example:
byte a = 10; byte b = 20;
byte c = (byte)(a + b);

5. Difference Table
Type Conversion: Automatic, widening, safe.
Type Casting: Manual, narrowing, may lose data.

6. Conclusion
Type conversion and type casting help manage data safely in Java.
Automatic type promotion allows mixed-type expressions to execute correctly.

You might also like