Great! Here's a clean, focused Day 11 Java learning note for you — about Data Type Conversion. I’ll cover:

  • ✅ What it is
  • ❓ Why we need it
  • 🕐 When we use it
  • 🔧 How to use it
  • 🔄 All conversion paths (byte → short → int → long → float → double, etc.)

You can directly use this for your daily log or even turn it into a blog/PDF later!


📘 Day 11 — Java Data Type Conversion


🔹 What is Data Type Conversion in Java?

Data Type Conversion means converting one data type into another.

Java supports:

  • Widening Conversion → done automatically
  • Narrowing Conversion → needs manual casting

Why Do We Need It?

  • To store values in compatible types
  • To perform operations between different data types
  • To avoid errors during assignment or calculations
  • To support method overloading, user input, etc.

🕐 When Do We Use It?

  • During mathematical operations
  • While assigning variables of different types
  • When reading input (like from Scanner)
  • In functions/methods that require specific types

🔧 How to Use It?

1. Widening Conversion (Automatic)

No cast needed — Java does it for you.

byte b = 10;
short s = b;   // byte → short
int i = s;     // short → int
long l = i;    // int → long
float f = l;   // long → float
double d = f;  // float → double

System.out.println(d);  // Output: 10.0

2. Narrowing Conversion (Manual)

You must cast manually. Risk of data loss.

double d = 99.99;
float f = (float) d;
long l = (long) f;
int i = (int) l;
short s = (short) i;
byte b = (byte) s;

System.out.println(b);  // Output: May lose precision

🔄 Full Widening Conversion Chart

From To
byte short, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double
char int, long, float, double

✅ All above conversions are automatic (widening).


🔄 Full Narrowing Conversion Chart

From To Needs Casting?
double float, long, int, short, byte ✅ Yes
float long, int, short, byte ✅ Yes
long int, short, byte ✅ Yes
int short, byte ✅ Yes
short byte ✅ Yes
int char ✅ Yes

✅ Final Tip:

Always prefer widening conversion when possible.

Use narrowing only when needed, and expect data loss or overflow.