Java Type Casting & Conversion Explained

☕ Learn Java with Me — Day 17 After understanding variables and memory, today we’re learning a very useful concept in Java 💻 👉 Type Casting & Type Conversion This is important not just for learning, but also from an interview and coding perspective 🎯 👉 What is Type Conversion? Type conversion means converting one data type into another. Example: int num = 10; double value = num; Here, int is automatically converted into double. This is called: 👉 Implicit Type Casting / Widening Why? Because Java converts a smaller data type into a bigger one automatically. Example: int → double No data loss 🚀 👉 Explicit Type Casting / Narrowing Sometimes we manually convert a bigger type into a smaller type. Example: double price = 99.99; int amount = (int) price; Output: 99 Here, decimal part gets removed. This is called: 👉 Explicit Type Casting Because we are forcing the conversion manually. 📌 Easy trick to remember: Small → Big = automatic Big → Small = manual ❓ Quick Question: What will be the output? double num = 12.8; int x = (int) num; System.out.println(x); We’re learning deeper — together 🤝 #java #coding #learning #interviewprep #showup #day17 #primitivetypecasting

  • No alternative text description for this image

💡 Answer: 12 Decimal part gets truncated.

Like
Reply

To view or add a comment, sign in

Explore content categories