🚀 Exploring Method Overloading with Varargs in Java Today, I worked on an interesting Java problem that helped me strengthen my understanding of Compile-Time Polymorphism and Varargs (Variable Arguments). 🔹 Problem Statement: Design a VarargsCalculator class with overloaded methods to calculate the sum of: Multiple integers Multiple double values 🔹 Key Concepts Used: ✔ Method Overloading ✔ Varargs (int..., double...) ✔ Switch-case for dynamic method selection ✔ For-each loop for efficient iteration 🔹 Learning Outcome: This problem clearly demonstrates how Java allows the same method name to handle different types and number of inputs, depending on the parameters passed. It also shows how varargs simplify handling multiple inputs without explicitly creating arrays. 💡 Key Insight: Varargs in Java internally behave like arrays, making code more flexible and readable. 🔹 Sample Output: Sum of integers: 10 Sum of doubles: 7.0 📌 Practicing such problems is helping me improve my problem-solving skills and deepen my understanding of core Java concepts. #Java #CoreJava #MethodOverloading #Varargs #Programming #Coding #100DaysOfCode #DeveloperJourney #JavaDeveloper #LearningInPublic
Java Varargs Calculator with Method Overloading
More Relevant Posts
-
Day 65 of #100DaysOfLeetCode 💻✅ Solved “Majority Frequency Group” problem in Java. Approach: • Created a frequency array of size 26 for all characters • Counted occurrences of each character in the string • For each unique frequency, counted how many characters share that frequency • Selected the frequency with the highest group size • In case of tie, chose the higher frequency • Built the result string with characters having the selected frequency Performance: ✓ Runtime: 5 ms (Beats 57.84% submissions) ✓ Memory: 44.51 MB (Beats 72.88% submissions) Key Learning: ✓ Practiced frequency grouping techniques ✓ Learned how to handle tie-breaking conditions ✓ Strengthened logic building with nested loops and conditions Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #Hashing #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 10/100 – Java Practice Challenge 🚀 Continuing my #100DaysOfCode journey with another important Java concept. 🔹 Topic Covered: Polymorphism Polymorphism means “many forms” — the same method behaves differently depending on the object. 💻 Practice Code: 🔸 Example Program class Animal { void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal a = new Dog(); // Upcasting a.sound(); // Runtime polymorphism } } 📌 Key Learnings: ✔️ Same method → different behavior ✔️ Achieved using method overriding ✔️ Based on object type (runtime) 🎯 Focus: Understanding dynamic behavior using inheritance and method overriding 🔥 Interview Insight: Polymorphism is a core OOP concept and widely used in real-world applications. #Java #100DaysOfCode #Polymorphism #OOP #JavaDeveloper #Programming #LearningInPublic
To view or add a comment, sign in
-
Day 73 of #90DaysDSAChallenge Solved LeetCode 451: Sort Characters By Frequency Learned an important Java design concept today. Problem Overview: The task was to sort characters in a string based on descending frequency. What confused me initially: Why create a separate Freq class instead of just using HashMap and PriorityQueue directly? Key Learning: PriorityQueue stores one complete object at a time. For this problem, each item needs two pieces of data together: Character Frequency Example: Instead of storing: e and 2 separately We package them as: Freq('e', 2) That custom class acts like a container holding both values in one object, so PriorityQueue can compare and sort them correctly. Why this matters: This taught me that custom classes in Java are often not about complexity, they simply bundle related data into one manageable unit. Alternative approach: We can also use Map.Entry<Character, Integer> instead of creating a custom class, but building Freq makes the logic easier to understand while learning. Today’s takeaway: Not every class is for business logic — sometimes it exists just to package data cleanly. #Java #90DaysDSAChallenge #LeetCode #PriorityQueue #HashMap #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Multithreading in Java finally clicked for me when I stopped memorizing it… and started visualizing it. 🧠 Here’s the simplest way to understand it: Imagine your application is doing only ONE task at a time. ➡️ Slow ➡️ Blocking ➡️ Poor performance Now introduce multithreading 👇 Multiple tasks run simultaneously: ✔ One thread handles API requests ✔ One processes data ✔ One writes logs Result? Faster and more efficient applications 🚀 But here’s what I learned the hard way: Multithreading is powerful… but dangerous if not handled properly. Common issues I faced: Race conditions Deadlocks Unexpected bugs What helped me: ✔ Proper synchronization ✔ Understanding thread lifecycle ✔ Using ExecutorService instead of manual threads Lesson: Multithreading is not just about speed — it’s about control and correctness. 💬 Have you faced any tricky bugs with multithreading? #Java #Multithreading #BackendDevelopment #SoftwareEngineering #Coding
To view or add a comment, sign in
-
📘 Day 25 – Unlocking the Magic of Java Casting Today I dove deep into non-primitive type casting in Java and had that haha moment! 💡 ✨ Upcasting – Treating a subclass object as a superclass reference. It makes my code cleaner, flexible, and ready for change. ⚡ Downcasting – Converting back safely to a subclass. Done wrong, it throws ClassCastException, but done right, it’s pure power. 🛡 instanceof operator – My safety net! It checks object type before casting, keeping runtime errors away. Seeing objects flow up and down the hierarchy revealed the true beauty of polymorphism, code that’s adaptable, maintainable, and future-proof. 💬 What really clicked: Java isn’t just about syntax; it’s about managing relationships between objects smartly. This makes every line of code safer, cleaner, and smarter. #Java #OOP #Polymorphism #Upcasting #Downcasting #ClassCastException #InstanceOf #DailyLearning #CodeBetter #ProgrammingJourney #DevLife
To view or add a comment, sign in
-
Solved LeetCode 17 – Letter Combinations of a Phone Number using backtracking in Java. Approach: Mapped each digit (2–9) to its corresponding characters using a simple array for O(1) access. Then used backtracking to build combinations digit by digit. For every digit: Pick each possible character Append → explore next digit → backtrack Key idea: Treat it like a tree of choices, where each level represents a digit and branches represent possible letters. Key learnings: Backtracking = build → explore → undo StringBuilder helps avoid unnecessary string creation Problems like this are about systematic exploration of choices Time Complexity: O(4^n * n) Space Complexity: O(n) recursion stack + output Consistent DSA practice is strengthening pattern recognition day by day. #Java #DSA #Backtracking #LeetCode #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝗧𝗶𝗽 - 𝗦𝘁𝗿𝗶𝗻𝗴 𝐔𝐬𝐚𝐠𝐞 🔥 💎 𝗧𝗵𝗿𝗲𝗲 𝗪𝗮𝘆𝘀 𝘁𝗼 𝗖𝗵𝗲𝗰𝗸 𝗦𝘁𝗿𝗶𝗻𝗴 𝗣𝗿𝗲𝗳𝗶𝘅/𝗦𝘂𝗳𝗳𝗶𝘅 👍 𝗨𝘀𝗶𝗻𝗴 𝘀𝘁𝗮𝗿𝘁𝘀𝗪𝗶𝘁𝗵 𝗮𝗻𝗱 𝗲𝗻𝗱𝘀𝗪𝗶𝘁𝗵 𝗺𝗲𝘁𝗵𝗼𝗱𝘀 The most readable and straightforward approach provided by the String class. These methods clearly express your intent and are optimized for prefix and suffix validation in production code. 💡 𝗨𝘀𝗶𝗻𝗴 𝗰𝗵𝗮𝗿𝗔𝘁 𝘄𝗶𝘁𝗵 𝗹𝗲𝗻𝗴𝘁𝗵 𝗰𝗵𝗲𝗰𝗸 Direct character access using the charAt() method combined with length() for index calculation. This approach offers fine-grained control when checking specific character positions at string boundaries. 🔥 𝗨𝘀𝗶𝗻𝗴 𝗿𝗲𝗴𝗲𝘅 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗺𝗮𝘁𝗰𝗵𝗶𝗻𝗴 The powerful Pattern and Matcher API from java.util.regex package. Perfect for complex validation scenarios where you need flexible pattern matching beyond simple prefix/suffix checks. 🤔 Which one do you prefer? #java #springboot #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
🔹 Title: Solving “Plus Minus” Problem in Java 📊 🔹 Description: Today I solved the Plus Minus problem, where the goal is to calculate the ratios of positive, negative, and zero values in an array. The challenge was not just counting the values, but also formatting the output correctly to 6 decimal places. 💡 Approach: Traverse the array and count positives, negatives, and zeros Divide each count by the total number of elements Print results using precise formatting 🔹 What I learned: ✔ Importance of output formatting ✔ Handling edge cases (like zeros) ✔ Writing clean and efficient Java code Consistency in practicing such problems really strengthens core programming skills. 🚀 #Java #Coding #ProblemSolving #Programming #DataStructures
To view or add a comment, sign in
-
-
Day 45-What I Learned In a Day (JAVA) Today I explored some important concepts related to static in Java: 🔹 Static Method Learned how static methods belong to the class and can be called without creating an object. 🔹 Static Initializer (Static Block) Understood how a static block is executed only once when the class is loaded. 🔹 Single-Line Static Initializer Explored how we can initialize static variables in a single line. 🔹 Multi-Line Static Initializer Learned how to use static blocks for complex initialization logic. Practiced 👇 #Java #Programming #LearningJourney #Coding #100DaysOfCode
To view or add a comment, sign in
-
📒 Day 29 of Java | Binding says, “I know who to call,” while static methods in method hiding are like twins — same name, different behavior. Today I learned about binding in Java, which refers to the process of linking a method call with its method body. 👉 There are two types of binding: ➲ Static Binding: The method call is resolved at compile time. It is used for method overloading, private methods, final methods, and static methods. ➲ Dynamic Binding: The method call is resolved at runtime. It is mainly used for method overriding and supports runtime polymorphism. 👉 I also learned about method hiding in Java. When a subclass defines a static method with the same signature as a static method in the parent class, it is called method hiding. These concepts made Java’s method handling feel even more interesting. #Java #CoreJava #Programming #Coding #ObjectOrientedProgramming #LearnInPublic #100DaysOfCode #DeveloperJourney #SoftwareDeveloper #BuildInPublic #StudentDeveloper #TechStudents
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