🚀 Day 3/100 – Java Practice Challenge Continuing my #100DaysOfCode journey with another important core Java concept. 🔹 Topics Covered: Generics in Java Understanding type safety, reusability, and avoiding runtime errors. 💻 Practice Code: 🔸 Generic Class Example class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } 🔸 Usage Box intBox = new Box<>(); intBox.set(10); Box strBox = new Box<>(); strBox.set("Hello"); System.out.println(intBox.get()); // 10 System.out.println(strBox.get()); // Hello 📌 Key Learning: ✔ Generics provide compile-time type safety ✔ Avoid ClassCastException ✔ Help write reusable and clean code ⚠️ Important: • Use <?> for unknown types (wildcards) • Use for bounded types • Generics work only with objects, not primitives 🔥 Interview Insight: Generics use type erasure — type information is removed at runtime Widely used in collections like List, Map<K, V> 👉 Without Generics: List list = new ArrayList(); list.add("Java"); list.add(10); // No compile-time error ❌ #100DaysOfCode #Java #JavaDeveloper #Generics #CodingJourney #LearningInPublic #Programming
Java Generics Explained: Type Safety and Reusability
More Relevant Posts
-
I recently explored a subtle but important concept in Java constructor execution order. Many developers assume constructors simply initialize values, but the actual lifecycle is more complex. In this article, I explain: • The real order of object creation • Why overridden methods can behave unexpectedly • A common bug caused by partial initialization This concept is especially useful for interviews and writing safer object-oriented code. Medium Link: https://lnkd.in/gtRhpdfP #Java #OOP #SoftwareDevelopment #Programming
To view or add a comment, sign in
-
Next Step in My Java Journey: Understanding the Java ClassLoader While learning how Java works internally, I discovered something very interesting — ClassLoaders. Whenever we run a Java program, the JVM needs to load the ".class" files into memory before executing them. This task is handled by the ClassLoader subsystem. But here's the interesting part: Java doesn't use just one class loader — it uses three main ClassLoaders. 🔹 Bootstrap ClassLoader Loads core Java classes like "java.lang", "java.util", etc. These are the fundamental classes required for every Java program. 🔹 Extension ClassLoader Loads classes from the Java extension libraries. 🔹 Application ClassLoader Loads the classes that we write in our Java applications. 📌 How it works When we run a program: "Hello.class" → Application ClassLoader → JVM loads it → Program executes 💡 Interesting fact Java uses a mechanism called Parent Delegation Model, where a class loader first asks its parent to load the class before loading it itself. This improves security and avoids duplicate class loading. Learning these internal concepts makes Java even more fascinating. #Java #JVM #ClassLoader #Programming #SoftwareDevelopment #LearnJava #DeveloperJourney
To view or add a comment, sign in
-
-
Day 11/100 – Java Practice Challenge 🚀 Continuing my #100DaysOfCode journey with another important Java concept. 🔹 Topic Covered: Compile-time vs Runtime Polymorphism 💻 Practice Code: 🔸 Compile-time Polymorphism (Method Overloading) class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } 🔸 Runtime Polymorphism (Method Overriding) class Animal { void sound() { System.out.println("Animal sound"); } } class Cat extends Animal { @Override void sound() { System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { // Compile-time Calculator c = new Calculator(); System.out.println(c.add(10, 20)); System.out.println(c.add(10, 20, 30)); // Runtime Animal a = new Cat(); a.sound(); } } 📌 Key Learnings: ✔️ Compile-time → method decided at compile time ✔️ Runtime → method decided at runtime ✔️ Overloading vs Overriding difference 🎯 Focus: Understanding how Java resolves method calls 🔥 Interview Insight: Difference between compile-time and runtime polymorphism is one of the most frequently asked Java interview questions. #Java #100DaysOfCode #MethodOverloading #MethodOverriding #Polymorphism #JavaDeveloper #Programming #LearningInPublic
To view or add a comment, sign in
-
🚀Core Java Journey – Exploring Strings in Java! Yesterday, I dived deep into one of the most important topics in Java — Strings 💻 Here’s what I learned: 🔹 String is a sequence of characters (not a primitive data type) 🔹 It is a class in Java and belongs to java.lang package 🔹 Strings are immutable (once created, they cannot be changed) 🔹 Difference between: 👉 String str = "value"; (stored in String Constant Pool) 👉 String str = new String("value"); (creates new object in heap) 🔹 Concept of String Constant Pool (SCP) for memory optimization 🔹 Why String class is final (for security, immutability, and performance) 🔹 Common classes used with String: ✔️ StringBuilder ✔️ StringBuffer ✔️ StringTokenizer 💡 One interesting thing I understood is how Java manages memory using Heap Area and SCP — really fascinating! Every day I’m getting more clarity and confidence in Java basics 🔥 #Java #CoreJava #LearningJourney #Programming #StudentLife #JavaDeveloper #Coding
To view or add a comment, sign in
-
-
** Constructor Overloading in Java — One concept, multiple ways to initialize! -->Ever wondered how a single class can be created in multiple ways? That's the power of Constructor Overloading in Java. ** What is it? -->Defining multiple constructors in the same class with different parameter lists. Java picks the right one based on the arguments you pass. ✅ 3 Steps: 1️⃣ Define constructors with different signatures 2️⃣ Create objects — Java auto-selects the right constructor 3️⃣ Use this() for constructor chaining to avoid repetition 🔑 Key Rules: • Same name as the class • Differ in number, type, or order of parameters • No return type • this() must be the first statement Constructor overloading = flexible, clean, reusable code. Master it and object creation becomes effortless! 💡 #Java #OOP #Programming #ConstructorOverloading #JavaDeveloper #CodeNewbie #LearnJava #SoftwareDevelopment
To view or add a comment, sign in
-
-
📘 Day 30 & 31 – Java Concepts: Static & Inheritance Over the past two days, I strengthened my understanding of important Java concepts like Static Members and Inheritance, which are essential for writing efficient and reusable code. 🔹 Static Concepts • Static members belong to the class, not objects • Static methods cannot directly access instance variables • Static blocks execute once when the class is loaded • Used mainly for initialization of static variables 🔹 Execution Flow • Static variables & static blocks run first when the class loads • Instance block executes after object creation • Constructor runs after instance block 🔹 Inheritance • Mechanism where one class acquires properties of another • Achieved using the "extends" keyword • Promotes code reusability and reduces development time 🔹 Key Rules • Private members are not inherited • Supports single and multilevel inheritance • Multiple inheritance is not allowed in Java (avoids ambiguity) • Cyclic inheritance is not permitted 🔹 Types of Inheritance • Single • Multilevel • Hierarchical • Hybrid (achieved using interfaces) 💡 Key Takeaway: Understanding static behavior and inheritance helps in building structured, maintainable, and scalable Java applications. #Java #OOP #Programming #LearningJourney #Coding #Developers #TechSkills
To view or add a comment, sign in
-
-
Hello Connections, Post 18 — Java Fundamentals A-Z This one makes your code 10x cleaner. Most developers avoid it. 😱 Can you spot the difference? 👇 // ❌ Before Java 8 — verbose and painful! List<String> names = Arrays.asList( "Charlie", "Alice", "Bob" ); Collections.sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return a.compareTo(b); } }); 8 lines. Just to sort a list. 😬 // ✅ With Lambda — clean and powerful! Collections.sort(names, (a, b) -> a.compareTo(b)); // ✅ Done! // Even cleaner with method reference! names.sort(String::compareTo); // ✅ One liner! // Real example! transactions.stream() .filter(t -> t.getAmount() > 10000) // Lambda! .forEach(t -> System.out.println(t)); // Lambda! Lambda = anonymous function // Structure of a Lambda (parameters) -> expression // Examples () -> System.out.println("Hello") // No params (n) -> n * 2 // One param (a, b) -> a + b // Two params (a, b) -> { // Block body int sum = a + b; return sum; } Post 18 Summary: 🔴 Unlearned → Writing verbose anonymous classes for simple operations 🟢 Relearned → Lambda = concise anonymous function — write less do more! 🤯 Biggest surprise → Replaced 50 lines of transaction processing code with 5 lines using Lambdas! Have you started using Lambdas? Drop a λ below! #Java #JavaFundamentals #BackendDevelopment #LearningInPublic #SDE2 Follow along for more! 👇
To view or add a comment, sign in
-
-
Most Java developers use int and Integer without thinking twice. But these two are not the same thing, and not knowing the difference can cause real bugs in your code. Primitive types like string, int, double, and boolean are simple and fast. They store values directly in memory and cannot be null. Wrapper classes like Integer, Double, and Boolean are full objects. They can be null, they work inside collections like lists and maps, and they come with useful built-in methods. The four key differences every Java developer should know are nullability, collection support, utility methods, and performance. Primitives win on speed and memory. Wrapper classes win on flexibility. Java also does something called autoboxing and unboxing. Autoboxing is when Java automatically converts a primitive into its wrapper class. Unboxing is the opposite, converting a wrapper class back into a primitive. This sounds helpful, and most of the time it is. But when a wrapper class is null and Java tries to unbox it, your program will crash with a NullPointerException. This is one of the most common and confusing bugs that Java beginners and even experienced developers run into. The golden rule is simple. Use primitives by default. Switch to wrapper classes only when you need null support, collections, or utility methods. I wrote a full breakdown covering all of this in detail, with examples. https://lnkd.in/gnX6ZEMw #Java #JavaDeveloper #Programming #SoftwareDevelopment #Backend #CodingTips #CleanCode #100DaysOfCode
To view or add a comment, sign in
-
-
Day 8 of Java Series ☕💻 Today we dive into one of the most important real-world concepts in Java — Exception Handling 🚨 👉 Exception Handling is used to handle runtime errors so that the normal flow of the program can be maintained. 🧠 What is an Exception? An Exception is an unwanted event that occurs during program execution and disrupts the normal flow of the program. ⚙️ Types of Exceptions: Checked Exceptions (Compile-time) Example: IOException, SQLException Unchecked Exceptions (Runtime) Example: ArithmeticException, NullPointerException Errors Example: StackOverflowError, OutOfMemoryError 🛠️ Exception Handling Keywords: try → Code that may throw exception catch → Handles the exception finally → Always executes (cleanup code) throw → Used to explicitly throw exception throws → Declares exceptions 💻 Example Code: Java Copy code public class Main { public static void main(String[] args) { try { int a = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); } finally { System.out.println("Execution Completed"); } } } ⚡ Custom Exception: You can create your own exception by extending Exception class. Java Copy code class MyException extends Exception { MyException(String msg) { super(msg); } } 🎯 Why Exception Handling is Important? ✔ Prevents program crash ✔ Maintains normal flow ✔ Improves debugging ✔ Makes code robust 🚀 Pro Tip: Always catch specific exceptions instead of generic ones for better debugging! 📢 Hashtags: #Java #ExceptionHandling #JavaSeries #Programming #CodingLife #LearnJava #Developers #Tech
To view or add a comment, sign in
-
-
TOPIC: Serialization and Deserialization in Java: 🔶 Serialization (Left Side) Converting a Java object into a byte stream (sequence of bytes) 👉 What happens: You start with a Java Object (like a class instance with data). Using classes like ObjectOutputStream, Java converts that object into binary data (0s and 1s). This data is stored in a file or sent over a network. 👉 Why we use it: Save object state into a file (persistence) Send objects over network (like in distributed systems) 👉 In short: ➡️ Object → Byte Stream 🔷 Deserialization (Right Side) Converting byte stream back into a Java object 👉 What happens: You take the serialized data (binary file). Using ObjectInputStream, Java reconstructs it back into the original object. 👉 Why we use it: Read saved data from file Receive objects from network 👉 In short: ➡️ Byte Stream → Object 🔁 Middle Flow (Connection) The arrows show that: Serialization sends data out Deserialization brings it back 💡 Simple Real-Life Example Serialization = Packing your items into a box 📦 Deserialization = Unpacking the box back into usable items 🎁 ⚠️ Important Points Class must implement Serializable interface ObjectOutputStream → for serialization ObjectInputStream → for deserialization If a class is not serializable → NotSerializableException occurs #java #Codegnan #Serialization #DeSerialization My gratitude towards my mentor #AnandKumarBuddarapu #SakethKallepu #UppugundlaSairam
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
Carry on