☕ 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
Java Type Casting & Conversion Explained
More Relevant Posts
-
Day 59 of Sharing What I’ve Learned 🚀 Comparator in Java — Custom Sorting Made Easy After learning how Map stores key-value pairs, I explored something powerful — controlling how data is sorted using Comparator. 👉 Default sorting is useful… but real-world problems need custom logic 🔹 What is Comparator? Comparator is an interface in Java used to define custom sorting logic. 👉 It allows us to sort objects based on our own rules 🔹 How does Comparator work? 👉 We override compare(a, b) method 👉 Returns: ✔ Negative → a comes before b ✔ Zero → both are equal ✔ Positive → a comes after b 🔹 Why use Comparator? ✔ Custom sorting logic ✔ Multiple sorting options ✔ Works without modifying the original class 🔹 Real-World Use Cases 👉 Sorting students by marks 👉 Sorting employees by salary 👉 Sorting products by price or rating 🔹 Key Features ✔ External sorting logic (separate from class) ✔ Can create multiple comparators ✔ Works with Collections.sort() & Arrays.sort() 🔹 Comparator vs Comparable 👉 Comparable = default/internal sorting 👉 Comparator = custom/external sorting 🔹 When should we use Comparator? 👉 Use it when: ✔ You need multiple ways to sort data ✔ You don’t want to modify the class ✔ You need dynamic sorting logic 🔹 When NOT to use? ❌ When only one natural sorting is enough ❌ When simple primitive sorting is required 🔹 Key Insight 💡 Sorting is not just arranging data… 👉 It’s about organizing data based on needs 🔹 Day 59 Realization 🎯 Good developers don’t just sort data… 👉 They decide how it should be sorted #Java #Comparator #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day59 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
Day 37 of learning Java! Today I learned about Lambda Expressions, Functional Interfaces & Comparator 🔹 Why Lambdas? ✔ Java shifted towards functional programming ✔ Now we can pass behavior (functions) directly ✔ Earlier problem: • Needed full classes just for small logic • Too much boilerplate 🔹 Functional Interface ✔ Interface with only ONE abstract method ✔ Why important? → Enables lambda expressions ✔ Short way to write functions Forms: • (a, b) → a + b • x → x * x • () → some action • Multi-line → use {} 🔹 Key Concept: Target Typing ✔ Java automatically understands: • parameter types • return type → Based on context 🔹 Comparator Interface ✔ Used for custom sorting ✔ Unlike Comparable: • Comparable → fixed logic • Comparator → flexible logic ✔ Example use: • Sort by name • Sort by marks • Sort by roll number 🔹 Lambdas with Comparator ✔ Cleaner sorting logic ✔ No need for anonymous classes → Makes code shorter & readable 🔹 Old vs New Approach Old: • Anonymous classes • Verbose New (Lambda): • Concise • Readable • Functional style special thanks to Aditya Tandon Rohit Negi sir
To view or add a comment, sign in
-
-
Day 12 of Learning Java Today I learned something small in Java that actually plays a big role in programming — Type Casting. At first, I thought it was complicated. But the idea is actually simple. Sometimes in programming, we need to convert one data type into another. For example, converting an `int` into a `double`. That process is called Type Casting. Java mainly has two types of type casting: - Implicit Casting (Widening) This happens automatically when converting a smaller data type into a larger one. Example: `int → double` - Explicit Casting (Narrowing) This is done manually when converting a larger type into a smaller one. Example: `double → int` Simple example: int num = 10; double result = num; // implicit casting double price = 19.99; int rounded = (int) price; // explicit casting What I’m realizing while learning Java is that even small concepts build the foundation of programming logic. Slowly learning. Step by step. #JavaLearning #LearningInPublic #CodingJourney #JavaProgramming #WomenInTech
To view or add a comment, sign in
-
I’m learning Java — and sharing only what actually sticks. 🚀 This week: Data Types, Conditionals & Loops And honestly… a few things surprised me 👇 ---------------------------------------------------------------------------- 🔹 Not everything in Java stores “data” Some things just store addresses 🔹 A simple type conversion can silently break your logic (No error. Just wrong output.) 🔹 if-else vs switch isn’t just syntax It actually affects readability + design decisions 🔹 All loops look similar… But choosing the wrong one = bad code I’ve broken all of this down with simple examples in the slides 👇 (Explained the way I wish I learned it the first time) Still figuring things out as I go — but that’s the fun part 😊. #Java #JavaLearning #LearningInPublic #Developers #CodingJourney
To view or add a comment, sign in
-
Day 41 of Learning Java: Method Overriding If method overloading was about flexibility,method overriding is about customization. What is Method Overriding? It’s when a subclass provides its own implementation of a method that is already defined in the parent class. Same method name. Same parameters. But different behavior. 🔹 Simple example- class Parent { void watchTV() { System.out.println("Watching News/Serial"); } } class Child extends Parent { @Override void watchTV() { System.out.println("Watching Music/Sports"); } } Same method → different output depending on the object. • Parent defines a general behavior • Child modifies it based on its own need • This helps in writing more flexible and reusable code 🔹 Key points to remember • Method signature must be the same • Happens during runtime (runtime polymorphism) • Inheritance is required 👉 You cannot override: static methods private methods final methods 🔹 One important concept Parent ref = new Child(); ref.watchTV(); Even though the reference is of Parent, the method of Child gets executed. 👉 This is called dynamic method dispatch 🔹 About @Override It’s not mandatory, but it helps: ✔ Avoid mistakes ✔ Makes code more readable ✔ Ensures you’re actually overriding #Java #OOP #MethodOverriding #LearningInPublic #Programming#sql #branding
To view or add a comment, sign in
-
-
Day 60 of Sharing What I’ve Learned 🚀 Comparable in Java — Defining Natural Sorting After learning how Comparator gives us custom sorting flexibility, I explored the foundation of sorting in Java — Comparable. 👉 Before customizing sorting… we should understand the default behavior 🔹 What is Comparable? Comparable is an interface in Java used to define natural (default) sorting of objects. 👉 It is implemented inside the class itself 🔹 How does Comparable work? 👉 We override compareTo() method 👉 Returns: ✔ Negative → current object comes before another ✔ Zero → both are equal ✔ Positive → current object comes after another 🔹 Why use Comparable? ✔ Defines default sorting behavior ✔ Simple and clean for single sorting logic ✔ Automatically used by sorting methods 🔹 Real-World Use Cases 👉 Sorting students by ID 👉 Sorting employees by name 👉 Sorting products by default price 🔹 Key Features ✔ Internal sorting logic (inside class) ✔ Only one sorting logic per class ✔ Works with Collections.sort() & Arrays.sort() 🔹 Comparable vs Comparator 👉 Comparable = internal / natural sorting 👉 Comparator = external / custom sorting 🔹 When should we use Comparable? 👉 Use it when: ✔ You need a single default sorting ✔ Sorting logic is fixed ✔ Natural ordering makes sense 🔹 When NOT to use? ❌ When multiple sorting logics are needed ❌ When sorting logic may change dynamically 🔹 Key Insight 💡 Before customizing everything… 👉 Always define a strong default behavior 🔹 Day 60 Realization 🎯 Great developers don’t just customize sorting… 👉 They design a meaningful default order first #Java #Comparable #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day60 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
🚀 Mastering Time & Space Complexity in Java DSA When I started learning Data Structures & Algorithms in Java, the biggest mindset shift wasn’t coding… it was thinking in complexity. 📌 Time Complexity (⏱️) It tells how fast your code runs as input grows. O(1) → Constant (Best 👍) O(log n) → Logarithmic O(n) → Linear O(n log n) → Efficient sorting O(n²) → Slow (avoid when possible ⚠️) 📌 Space Complexity (💾) It tells how much memory your code uses. Efficient programs don’t just run fast — they also use less memory. 💡 Key Learnings: ✔️ Always analyze before optimizing ✔️ Nested loops ≠ always bad, but be careful ✔️ Trade-offs exist between time & space ✔️ Practice problems to build intuition 🔥 Current Focus: Improving problem-solving by writing optimized Java solutions and analyzing their complexity. Consistency > Motivation 💯 #Java #DSA #CodingJourney #TimeComplexity #SpaceComplexity #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 **Day 6 of My DSA Journey in Java** Today’s focus was on one of the most important building blocks of programming — **Data Types & Type Casting in Java**. 🔹 **What I Learned:** * A **data type** defines what kind of data a variable can store and how much memory it uses. * Explored **primitive data types**: * Numeric → `byte`, `short`, `int`, `long`, `float`, `double` * Non-numeric → `char`, `boolean` * Understood how **characters work with ASCII values**, and how they behave during calculations. 🔹 **Type Casting Concepts:** * **Implicit Casting (Widening):** Automatic conversion from smaller to larger data types (safe). * **Explicit Casting (Narrowing):** Manual conversion from larger to smaller types (can cause data loss). 💻 Practiced these concepts with hands-on coding in IntelliJ IDEA, which helped me clearly understand how Java handles data internally. 📌 **Key Takeaway:** A strong grasp of data types and type casting is essential because it directly impacts memory usage, performance, and accuracy in programs. #Java #DSA #LearningJourney #ProgrammingBasics #100DaysOfCode #CodingLife
To view or add a comment, sign in
-
Why I Stopped Using Scanner in Competitive Programming (Java) While solving problems in Java, I noticed something frustrating — even when my logic was correct, I was still getting TLE (Time Limit Exceeded) on some problems. After digging deeper, I realized the issue wasn’t my algorithm… it was my input method. 💡 The Problem with Scanner Java’s Scanner is very convenient, but it comes with a cost: It uses regex parsing internally Performs extra processing for tokenizing input Slower compared to other input methods 👉 This makes it inefficient for handling large inputs (like 10⁵ or 10⁶ values), which are very common in competitive programming. ⚡ The Better Approach: Fast I/O I switched to using: BufferedReader StringTokenizer These are much faster because they: Read input in bulk Avoid unnecessary parsing overhead Give better performance in tight time constraints 🛠️ What I Learned ✔️ Correct logic is not enough — performance matters ✔️ Input/output handling can impact your results ✔️ Choosing the right tools is part of problem-solving 🔥 Key Takeaway in competitive programming, even small optimizations like faster input methods can make a big difference between AC and TLE. 💻 Advice to Beginners If you’re using Java for competitive coding: Use Scanner only for small inputs Switch to fast I/O for serious problems Practice with efficient templates Always learning and improving ⚡ #CompetitiveProgramming #Java #DSA #CodingJourney #PerformanceMatters #LearnAndGrow
To view or add a comment, sign in
-
☕ Learn Java with Me — Day 7 7 days ago, we were just thinking about starting. Overthinking. Waiting for the “right time”. Today? We showed up for 7 days straight. No big results. No perfect code. But we learned: → Variables & Data Types → Operators → If-Else → Loops → Methods And today, we tried something practical: 👉 Taking user input in Java Example: import java.util.Scanner; Scanner sc = new Scanner(System.in); System.out.println("Enter your name:"); String name = sc.nextLine(); System.out.println("Hello " + name); This made things feel real. Now it’s not just logic — we can interact with users too. More importantly: → We stopped waiting → We started doing → We stayed consistent From confused → a little more confident. Still beginners. But not at Day 1 anymore. And that matters. Week 2 starts tomorrow 🚀 Comment “STARTED” if you’re learning with me 👇 #java #coding #learning #consistency #ITstudent #showup
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
💡 Answer: 12 Decimal part gets truncated.