🚀 𝗝𝗮𝘃𝗮 𝟴 – 𝗠𝗮𝗷𝗼𝗿 𝗙𝗲𝗮𝘁𝘂𝗿𝗲𝘀 𝗜𝗻𝘁𝗿𝗼𝗱𝘂𝗰𝗲𝗱 | 𝗔𝗜-𝗣𝗼𝘄𝗲𝗿𝗲𝗱 𝗙𝘂𝗹𝗹 𝗦𝘁𝗮𝗰𝗸 𝗝𝗼𝘂𝗿𝗻𝗲𝘆 Java 8 was a game changer in the Java ecosystem. It transformed the way developers write clean, efficient, and modern code. Here’s a quick breakdown of the powerful features introduced in Java 8 👇 🔹 1️⃣ 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲 ✔ Single Abstract Method (SAM) ✔ Foundation for Lambda Expressions ✔ Enables functional programming in Java Example: @FunctionalInterface interface MyFunctionalInterface { void display(); } 🔹 2️⃣ 𝗗𝗲𝗳𝗮𝘂𝗹𝘁 & 𝗦𝘁𝗮𝘁𝗶𝗰 𝗠𝗲𝘁𝗵𝗼𝗱𝘀 ✔ Allows method implementation inside interfaces ✔ Maintains backward compatibility ✔ Reduces breaking changes in APIs Example: interface MyInterface { default void show() { System.out.println("Default Method"); } static void print() { System.out.println("Static Method"); } } 🔹 3️⃣ 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻𝘀 ✔ Simplifies anonymous inner classes ✔ Cleaner & more readable code ✔ Core part of functional programming Example: Runnable r = () -> System.out.println("Running with Lambda"); 🔹 4️⃣ 𝗠𝗲𝘁𝗵𝗼𝗱 𝗥𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲 ✔ Reuse existing methods ✔ Makes code more concise Example: list.forEach(System.out::println); 🔹 5️⃣ 𝗦𝘁𝗿𝗲𝗮𝗺 𝗔𝗣𝗜 ✔ Process collections efficiently ✔ Supports filter, map, reduce operations ✔ Enables parallel processing Example: list.stream() .filter(x -> x > 10) .map(x -> x * 2) .forEach(System.out::println); 🔹 6️⃣ 𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹 𝗖𝗹𝗮𝘀𝘀 ✔ Avoids NullPointerException ✔ Better null handling Example: Optional<String> name = Optional.ofNullable(null); System.out.println(name.orElse("Default Name")); 🔹 7️⃣ 𝗗𝗮𝘁𝗲 & 𝗧𝗶𝗺𝗲 𝗔𝗣𝗜 (𝗷𝗮𝘃𝗮.𝘁𝗶𝗺𝗲) ✔ Thread-safe ✔ More powerful than old Date & Calendar Example: LocalDate today = LocalDate.now(); LocalTime time = LocalTime.now(); ✨ 𝗝𝗮𝘃𝗮 𝟴 𝗶𝗻𝘁𝗿𝗼𝗱𝘂𝗰𝗲𝗱 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗽𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗰𝗼𝗻𝗰𝗲𝗽𝘁𝘀 𝘁𝗵𝗮𝘁 𝗺𝗮𝗱𝗲 𝗝𝗮𝘃𝗮: • More expressive • More concise • More powerful 💡 If you’re learning Full Stack Java, mastering Java 8 is mandatory. #Java #Java8 #FullStackDeveloper #Programming #SoftwareDevelopment #LearningJourney #TechGrowth
Java 8 Features Introduced: Lambda Expressions and Functional Programming
More Relevant Posts
-
𝗗𝗔𝗬 𝟯 – 𝗦𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲 𝗼𝗳 𝗮 𝗝𝗮𝘃𝗮 𝗣𝗿𝗼𝗴𝗿𝗮𝗺 + 𝗝𝗮𝘃𝗮 𝗧𝗼𝗸𝗲𝗻𝘀 𝗜𝗻 𝘁𝗵𝗲 𝗹𝗮𝘀𝘁 𝘁𝘄𝗼 𝗽𝗼𝘀𝘁𝘀 𝘄𝗲 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗼𝗼𝗱: • Why Java is Platform Independent • How JDK, JRE, and JVM work together to run a Java program Now it's time to move from 𝘁𝗵𝗲𝗼𝗿𝘆 → 𝗮𝗰𝘁𝘂𝗮𝗹 𝗰𝗼𝗱𝗲. 𝗟𝗲𝘁’𝘀 𝘄𝗿𝗶𝘁𝗲 𝗮 𝘀𝗶𝗺𝗽𝗹𝗲 𝗝𝗮𝘃𝗮 𝗽𝗿𝗼𝗴𝗿𝗮𝗺. ``` 𝚙𝚞𝚋𝚕𝚒𝚌 𝚌𝚕𝚊𝚜𝚜 𝙷𝚎𝚕𝚕𝚘𝚆𝚘𝚛𝚕𝚍 { 𝚙𝚞𝚋𝚕𝚒𝚌 𝚜𝚝𝚊𝚝𝚒𝚌 𝚟𝚘𝚒𝚍 𝚖𝚊𝚒𝚗(𝚂𝚝𝚛𝚒𝚗𝚐[] 𝚊𝚛𝚐𝚜) { 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝙷𝚎𝚕𝚕𝚘 𝚆𝚘𝚛𝚕𝚍"); } } ``` At first glance this may look confusing. But if we break it down, the structure becomes very simple. 𝗦𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲 𝗼𝗳 𝗮 𝗝𝗮𝘃𝗮 𝗣𝗿𝗼𝗴𝗿𝗮𝗺 Every Java program generally contains: 1️⃣ 𝗖𝗹𝗮𝘀𝘀 𝗗𝗲𝗰𝗹𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝚙𝚞𝚋𝚕𝚒𝚌 𝚌𝚕𝚊𝚜𝚜 𝙷𝚎𝚕𝚕𝚘𝚆𝚘𝚛𝚕𝚍 In Java, everything starts with a class. The filename must match the class name. Example: 𝙷𝚎𝚕𝚕𝚘𝚆𝚘𝚛𝚕𝚍.𝚓𝚊𝚟𝚊 2️⃣ 𝗠𝗮𝗶𝗻 𝗠𝗲𝘁𝗵𝗼𝗱 𝚙𝚞𝚋𝚕𝚒𝚌 𝚜𝚝𝚊𝚝𝚒𝚌 𝚟𝚘𝚒𝚍 𝚖𝚊𝚒𝚗(𝚂𝚝𝚛𝚒𝚗𝚐[] 𝚊𝚛𝚐𝚜) This is the entry point of every Java program. When we run a program, the JVM starts execution from the 𝗺𝗮𝗶𝗻() 𝗺𝗲𝘁𝗵𝗼𝗱. 3️⃣ 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁𝘀 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝙷𝚎𝚕𝚕𝚘 𝚆𝚘𝚛𝚕𝚍"); This statement simply prints output on the console. 𝗡𝗼𝘄 𝗹𝗲𝘁’𝘀 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 𝘀𝗼𝗺𝗲𝘁𝗵𝗶𝗻𝗴 𝘃𝗲𝗿𝘆 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗶𝗻 𝗝𝗮𝘃𝗮. 𝗝𝗮𝘃𝗮 𝗧𝗼𝗸𝗲𝗻𝘀 Tokens are the smallest building blocks of a Java program. Java programs are basically made up of tokens. 𝗧𝗵𝗲𝗿𝗲 𝗮𝗿𝗲 𝗺𝗮𝗶𝗻𝗹𝘆 𝟱 𝘁𝘆𝗽𝗲𝘀: • 𝗞𝗲𝘆𝘄𝗼𝗿𝗱𝘀 → public, class, static, void • 𝗜𝗱𝗲𝗻𝘁𝗶𝗳𝗶𝗲𝗿𝘀 → names of variables, classes, methods • 𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝘀 → actual values (10, "Hello", true) • 𝗦𝗲𝗽𝗮𝗿𝗮𝘁𝗼𝗿𝘀 → { } ( ) [ ] ; , • 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 → + - * / = == Example identifier rules: ✔ Cannot start with a number ✔ Cannot use Java keywords ✔ No spaces allowed ✔ Can use letters, numbers, _ and $ Once you understand structure + tokens, reading Java code becomes much easier. If you look at the Java program again, you’ll now notice: It is simply a combination of tokens working together. Once you understand tokens and structure, reading Java code becomes much easier. 𝗧𝗼𝗺𝗼𝗿𝗿𝗼𝘄 (𝗗𝗮𝘆 𝟰) 𝗪𝗲’𝗹𝗹 𝗴𝗼 𝗱𝗲𝗲𝗽𝗲𝗿 𝗶𝗻𝘁𝗼 𝗼𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗺𝗼𝘀𝘁 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝘁𝗼𝗽𝗶𝗰𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮: • Data Types • Variables • Primitive vs Non-Primitive types • Memory basics behind variables Because before writing real programs, understanding how Java stores data is critical. 𝗦𝗲𝗲 𝘆𝗼𝘂 𝗶𝗻 𝗗𝗮𝘆 𝟰. #Java #BackendDevelopment #Programming #LearnInPublic #Day3
To view or add a comment, sign in
-
#Java strings are length-prefixed objects, not null-terminated sequences, it is a deliberate move towards safer and more predictable string handling, if you were hoping for a hidden \0 at the end, I’m afraid Java decided that was someone else’s problem decades ago. Java strings are not null terminated, they are instances of String, not raw character arrays with a sentinel value tacked on the end, instead of scanning memory until a zero byte shows up, Java stores the length explicitly as part of the object state so when you call length(), the JVM does not go hunting for a terminator like it is 1972, it simply reads a field. This design choice is not just aesthetic, it is architectural, in C-style null terminated strings, determining length is an O(n) operation because you must traverse until \0 appears. Java avoids that entirely, length is O(1), which is exactly what you want in a language that pretends to care about abstraction and performance. Internally, Java strings are backed by arrays, historically char[] and more recently byte[] with a coder flag for compact storage, the crucial detail is that the array is paired with metadata, including length, so the runtime always knows the bounds without relying on a terminator. In Java, the null character \u0000 is just another character, you can place it in the middle of a string and nothing dramatic happens, no truncation, no existential crisis, this alone makes null termination impractical as a contract, since the language explicitly allows that value inside strings. Null terminated strings have a long and rather embarrassing history of buffer overflows and off-by-one errors because developers forget to allocate space for the terminator or fail to write it, java sidesteps the entire category by design, no terminator, no dependency on sentinel correctness, fewer ways to shoot yourself in the foot. There is, however, a trade-off, when interfacing with native code via JNI, you cannot assume Java strings behave like C strings, you must pass both pointer and length explicitly or use conversion helpers, aka, the #JVM will not indulge your nostalgia for null termination. https://lnkd.in/dmKXUtGf
To view or add a comment, sign in
-
☕🧠 GUESS THE JAVA VERSION: ASSERT EDITION Java has evolved a lot over the years. Some features that feel “basic” today were actually introduced later than many developers think. Let’s test your Java history knowledge 👇 🔸 THE QUESTION What is the minimum Java version required to compile this code? class Example { void test(int x) { assert x > 0 : "x must be positive"; assert x < 100; } } Choose the correct answer: ▪️ Java 1.2 ▪️ Java 1.4 ▪️ Java 6 ▪️ Java 16 ▪️ Java 18 Take a moment to think before scrolling 👇 🔸 TLDR ▪️ The assert keyword appeared in Java 1.4 ▪️ It checks conditions during runtime ▪️ If the condition fails, Java throws AssertionError ▪️ Assertions are disabled by default unless enabled with -ea 🔸 THE ANSWER ✅ Java 1.4 🔸 WHY THIS IS THE CORRECT ANSWER Java 1.4 introduced the assert keyword. Assertions allow developers to verify assumptions in the code while the program runs. If the condition is false, Java throws an AssertionError. They are mostly used for: ▪️ checking internal logic ▪️ verifying program invariants ▪️ detecting bugs during development Assertions are not intended for validating user input. 🔸 TWO WAYS TO WRITE ASSERTIONS Java supports two formats: assert condition; and assert condition : message; The second form allows you to provide a message that explains the problem. Example: class Example { static int abs(int x) { int r = (x >= 0) ? x : -x; assert r >= 0 : "abs result must be non-negative"; return r; } } 🔸 IMPORTANT DETAIL Assertions are disabled by default in many runtime environments. To enable them when running a program: java -ea MyProgram If assertions are disabled: ▪️ the condition is not evaluated ▪️ no error is thrown This makes them safe to keep in production code. 🔸 A BIT OF HISTORY The assertion mechanism was introduced in Java 1.4 (JSR-41). The goal was to give developers a simple debugging tool to verify assumptions without adding heavy validation logic everywhere. 🔸 TAKEAWAYS ▪️ Assertions help detect programming mistakes early ▪️ They are designed for internal checks, not user validation ▪️ They can be safely left in code because they can be disabled at runtime ▪️ Knowing when Java features were introduced helps in certifications and legacy code maintenance 💬 Did you know assert appeared only in Java 1.4? Or did you expect it to be older? #Java #JavaDeveloper #JavaProgramming #Programming #SoftwareEngineering #Coding #Developers #TechQuiz #JavaHistory #LearnJava src: https://lnkd.in/exEmMjwC Go further with Java certification: Java👇 https://lnkd.in/eZKYX5hP Spring👇 https://lnkd.in/eADWYpfx SpringBook👇 https://bit.ly/springtify JavaBook👇 https://bit.ly/jroadmap
To view or add a comment, sign in
-
-
🤫 Java wrote this code too. You still didn't notice. (Missed Part 1? It covers: default constructors, super() calls, access modifiers, and field initialization.) -> https://lnkd.in/eWV9t3cD Java keeps omitting things. Let's finish the list. 👇 📋 5. List (and Collection) initial values... are null This one trips up even experienced developers: private List<String> items; That field? It's null. Not an empty list. Null. Java doesn't initialize object references — it just sets them to null and waits for you to do the rest. The fix is obvious, but the mistake is common: private List<String> items = new ArrayList<>(); If you call items.add("x") before initializing → NullPointerException. No mercy. 🧩 6. Interface default methods (since Java 8) Before Java 8, every method in an interface was implicitly: → public → abstract You never had to write those modifiers — they were always there. Since Java 8, interfaces can also declare default methods with an actual implementation. This changed the language significantly, enabling backward-compatible API evolution without breaking existing implementations. The implicit public + abstract still applies to non-default methods. Java just never told you. 🧵 7. Extends Object — always, everywhere Every class you write implicitly extends java.lang.Object. Always. That's why every object in Java has: → toString() → equals() → hashCode() → wait(), notify(), getClass() You didn't inherit from anything? You did. From Object. Silently. 🎯 The pattern here isn't random. Java's designers chose these defaults to promote: → Safe initialization (no garbage memory values) → Consistent object hierarchy (everything is an Object) → Less boilerplate (constructors you didn't need to write) Understanding the invisible layer is what separates someone who writes Java from someone who truly understands it. What other Java default surprised you the most? Drop it in the comments 👇 📊 Oracle Java Language Specification (2024) — https://lnkd.in/e7RvNa37 📊 "Effective Java" — Joshua Bloch (3rd Edition, 2018) 📊 Baeldung — "Java Interfaces" — https://lnkd.in/emxaHmqj #Java #SoftwareEngineering #BackendDevelopment #JavaDeveloper #CleanCode #Programming #TechTips #JVM
To view or add a comment, sign in
-
Want to Level Up Your Java Game? Let's Talk JVM! 🚀 Ever wondered what makes Java tick behind the scenes? 🧐 It’s not just a language; it's an entire ecosystem, and the heart of it all is the Java Virtual Machine (JVM). 💓 Understanding the JVM is like knowing how your car's engine works—it makes you a better driver, or in our case, a better developer! 👩💻👨💻 Think of the JVM as Java’s interpreter and bodyguard. It’s what gives Java its superpower: "Write Once, Run Anywhere." 🌍 Here's a quick, breakdown of how it pulls off the magic. ✨ Your Code's Epic Journey: 1️⃣ Class Loader Subsystem: This is the JVM's "receptionist." It reads your .class files (compiled Java bytecode) and loads them into memory. It also takes care of linking and initialising things so they're ready to go. 🧑✈️ 2️⃣ Runtime Data Areas (Memory): This is where all the action happens! 🏗️ It's divided into key zones: * Method Area: Stores class information and static variables. Think of it as the project blueprint repository. 📂 * Heap: The big arena where all your objects live. 🏟️ This is shared space, and it's where the Garbage Collector does its work. * JVM Stack: Per-thread, private storage for method calls and local variables. Think of it as a personal notebook for each process. 💪 * PC Register: Keeps track of exactly where each thread is in the execution process. The ultimate bookmark! 🔖 * Native Method Stack: Dedicated space for non-Java (native) code. 🌐 3️⃣ Execution Engine: The engine room! 🚂 It takes that bytecode and turns it into real, executable commands. This is where you find the performance boosters: * Interpreter: Executes bytecode line by line, fast for getting things started. ⚡️ * JIT (Just-In-Time) Compiler: The performance wizard. 🧙♂️ It finds the "hot spots" in your code and compiles them directly into native machine code for maximum speed. * Garbage Collector (GC): Your code's janitor. 🧹 It automatically finds and frees up memory occupied by objects you're no longer using, preventing memory leaks and keeping things running smoothly. So, next time you run a Java application, remember the incredibly sophisticated JVM working tirelessly to make it happen! ⚙️ What's your favourite part of JVM architecture? Let me know in the comments! 👇 #Java #JVM #SoftwareEngineering #TechInfluencer #CloudComputing #CodingLife #LearnToCode #JavaDeveloper #TechTutorials #ProgrammerInsights #JVMInternal #PerformanceOptimization
To view or add a comment, sign in
-
-
Two threads. One shared resource. What could possibly go wrong? In Java, when multiple threads try to access or modify the same resource at the same time, the result can be data inconsistency. This situation is known as a race condition, and it is one of the most common problems in multithreaded systems. To control this behavior, Java provides the "synchronized" keyword. The synchronized keyword ensures that only one thread can execute a critical section of code at a time. It works using a mechanism called locks. Where can we apply synchronized? The synchronized keyword can be applied in two places: • Synchronized Method • Synchronized Block I. Synchronized Method When a method is declared as synchronized, the entire method becomes a critical section. Example concept: public synchronized void display() { // critical section } Before executing this method, a thread must acquire the lock of the object. If another thread already holds that lock, the remaining threads must wait until the lock is released. Once execution finishes, the JVM automatically releases the lock. However, synchronizing the entire method is not always efficient. Sometimes only a small portion of the method needs protection. Synchronizing the whole method can increase waiting time for other threads, which affects performance. II. Synchronized Block To avoid unnecessary waiting, Java allows us to synchronize only a specific part of the code. Example concept: synchronized(object) { // critical section } Here, only the code inside the block is synchronized. III. Advantages of synchronized blocks: • Improves performance • Reduces thread waiting time • Allows better control over critical sections Note: The lock mechanism in Java works only with objects and classes, not with primitive data types. This means we cannot pass primitive values like: int, float, double, boolean to a synchronized block. Locks must always be associated with an object reference. In Interview, we may asked questions on: 1. Race Condition When multiple threads operate simultaneously on the same object and cause data inconsistency, it is called a race condition. 2. Object-Level Lock Every object in Java has a unique lock. When a thread executes a synchronized instance method, it must acquire the lock of that object. Different objects → different locks → threads can run simultaneously. 3. Class-Level Lock Every class in Java also has a single class-level lock. This lock is used when a thread executes a static synchronized method. Before executing the method, the thread must acquire the class lock. 4. Can a thread acquire multiple locks simultaneously? Yes. A thread can hold multiple locks at the same time, as long as they belong to different objects. #Day10 #Java #Multithreading #SoftwareEngineering
To view or add a comment, sign in
-
Should JPMS be part of your Java 25 modernization journey? Moving from Java 8 to Java 25 modernizes the platform. But platform modernization does not automatically modernize your architecture. JPMS (Java Modules, introduced in Java 9) is not a version upgrade — it’s a structural decision. And whether it makes sense depends entirely on the nature of your application. It Depends on Your Application Type 1️⃣ Simple Spring Boot Application (REST APIs) • Acts primarily as an HTTP API layer • Serves frontend clients (Angular/React) or external consumers • Does not distribute reusable libraries Then ideally, JPMS may not reap significant benefits. Why? • You expose REST endpoints — not internal classes • External consumers cannot access your internal packages directly • Spring heavily relies on reflection, meaning many packages must be opened This reduces the strict encapsulation advantage JPMS is designed to provide. 2️⃣ JavaFX/Swing-based application or Library If your application is: • A reusable library • A JavaFX/Swing-based application • A platform component consumed by other teams Then JPMS becomes highly valuable. Here, strong and explicit module boundaries truly matter. What Does JPMS Actually Introduce? JPMS enforces strong encapsulation at the module level, beyond traditional package visibility. Before Java 9: If a class was public, any other package on the classpath could access it. Example (Pre-Java 9): package user; public class UserService { public void fetchUser(){ StringUtil util = new StringUtil(); util.print(); }} StringUtil is in another package(utility) package utility; public class StringUtil { public void print(){ } } If StringUtil is public, anyone can use it — even unintended services. There was no module-level restriction. After Java 9 - Using Modules Now we define two modules: 1> service 2> util Each must contain a module-info.java file service (module-info.java) module service { requires util; } util (module-info.java) Option 1: Expose to Everyone module util { exports utility; } This behaves similarly to Java 8. Option 2: Restrict Access module util { exports utility to service; } Now only the service module can access the utility package. This is true module-level encapsulation enforced by the compiler. Final Thought Upgrading to Java 25 does not automatically mean you should adopt JPMS. JPMS is powerful — but its value depends on context. I’m curious to hear from the community: • Has anyone adopted JPMS as part of a modernization effort? • Did you implement it in a standard REST-based backend? • Did it deliver tangible benefits (stronger boundaries, cleaner dependencies)? • Or did Spring’s reflection-heavy nature reduce its practical value? Looking forward to learning from real-world experiences. #Java #JPMS #SoftwareArchitecture #BackendDevelopment #SpringBoot #Java25
To view or add a comment, sign in
-
🚀 Day 6/100 — Methods in Java 🔧 As programs grow bigger, repeating the same code again and again becomes messy. This is where methods help. A method is a reusable block of code that performs a specific task. Instead of rewriting logic multiple times, you write it once and call it whenever needed. This follows the DRY principle — Don't Repeat Yourself. 🔹 Basic Method Syntax returnType methodName(parameters){ // method body } Example: public static void greet(){ System.out.println("Hello Java!"); } Calling the method: greet(); Output: Hello Java! 🔹 Method with Parameters Parameters allow methods to work with different inputs. Example: public static void add(int a, int b){ int sum = a + b; System.out.println(sum); } add(5, 3); Output: 8 🔹 Method with Return Value Sometimes a method needs to return a result. Example: public static int square(int num){ return num * num; } int result = square(4); System.out.println(result); Output: 16 🔹 Method Overloading Java allows multiple methods with the same name but different parameters. This is called method overloading. Java automatically chooses the correct method based on the arguments. Example: public static int add(int a, int b){ return a + b; } public static int add(int a, int b, int c){ return a + b + c; } Usage: System.out.println(add(5,3)); // calls first method System.out.println(add(5,3,2)); // calls second method 🔴 Live Example — Max of 3 Numbers public static int max(int a, int b, int c){ if(a >= b && a >= c){ return a; } else if(b >= a && b >= c){ return b; } else{ return c; } } public static void main(String[] args){ int result = max(10, 25, 15); System.out.println("Maximum number is: " + result); } Output: Maximum number is: 25 🎯 Challenge: Write a method to find the maximum of 3 numbers and test it with different inputs. Drop your solution in the comments 👇 #Java #CoreJava #100DaysOfCode #JavaMethods #ProgrammingJourney
To view or add a comment, sign in
-
-
Standard Signature of main() Method in Java In every programming language there must be an entry point of execution from where program execution begins. In C/C++, the entry point is the main() function, which is invoked by the Operating System. OS expects 0 as exit status indicating successful program execution so the return type of main is commonly int. In Java, the entry point is also the main() method, but it is invoked by the Java Virtual Machine (JVM) instead of the OS. Since the JVM handles execution internally, there is no need to return a status code, therefore the return type of the main method is always void. In Java, every method belongs to a class, so the main method must be defined inside a class. Example: class Main { void main() { // code } } However, this method cannot be executed by the JVM because it is not accessible outside the class. To allow the JVM to access it, the method must be declared public. class Main { public void main() { // code } } In Java, methods normally belong to objects and are invoked using an object reference. If the main method were not static, the JVM would have to create an object of the class before calling it. Since main is the entry point of every program, this would add unnecessary overhead. To allow the JVM to invoke the method without creating an object, the main method is declared static. class Main { public static void main() { // code } } But this method still cannot receive data from the command line arguments. To accept input from the command line during program execution, the main method takes a parameter which is an array of strings. Each element of this array represents one argument passed from the command line. Final standard signature of the main method: class Main { public static void main(String[] args) { // code } } Here: public → allows the JVM to access the method static → allows the JVM to call the method without creating an object void → no return value required String[] args → receives command line arguments However, for a beginner writing "public static void main(String[] args)" is overwhelming. So Java developer decided to introduce simplified syntax for new comer to maintain language acceptance and popularity among all. In newer Java versions, we can write a simpler program like: void main() { System.out.println("Hello"); } Introduced in JDK 21 and finally accepted in JDK 25 (2025). The compiler automatically wraps this into a class behind the scenes. However, this feature is mainly designed for learning and small scripts, while the traditional main method remains the standard approach used in real applications. Grateful to my mentor Syed Zabi Ulla for explaining these concepts so clearly and helping me build a strong foundation in programming. #OOP #Java #Programming #ComputerScience #LearningJourney #SoftwareDevelopment
To view or add a comment, sign in
-
🧵 Thread: Understanding Threads in Java – Simplified After all these years in Java development, one concept that repeatedly appears in real-world systems is Threads & Concurrency. Let’s break it down simply 👇 --- 🔹 1️⃣ What is a Thread? A Thread is the smallest unit of execution inside a process. Think of a program like a restaurant kitchen. 👩🍳 One chef cooking everything = Slow 👨🍳👩🍳 Multiple chefs cooking different dishes = Faster That’s exactly what threads do in a program. Process (Application) | +-------------------+ | | | Thread1 Thread2 Thread3 (Task) (Task) (Task) Multiple tasks run simultaneously inside the same application. --- 🔹 2️⃣ Why do we need Threads? Threads help us: ✅ Improve performance ✅ Utilize CPU efficiently ✅ Handle multiple users in web applications ✅ Run background tasks Example in a backend system: User Request → Thread Pool | +---------+----------+ | | Thread A Thread B Process Payment Send Notification Instead of waiting for one task to finish, the system handles tasks concurrently. --- 🔹 3️⃣ How Java Creates Threads Two common ways: 1. Extending Thread class MyThread extends Thread { public void run() { System.out.println("Thread running"); } } 2. Implementing Runnable (Preferred) class MyTask implements Runnable { public void run() { System.out.println("Task running"); } } --- 🔹 4️⃣ Real-world Example Imagine an e-commerce system Customer Order Placed | +---------------------------+ | | | Thread 1 Thread 2 Thread 3 Process Order Send Email Update Inventory Without threads → Everything runs one by one With threads → Tasks run in parallel --- 🔹 5️⃣ But there's a catch ⚠️ Multiple threads accessing the same data can cause problems like: ❌ Race Conditions ❌ Data inconsistency ❌ Deadlocks That’s why Java provides tools like: 🔐 synchronized 🔐 Locks 🔐 Concurrent Collections 🔐 Executor Framework --- 💡 Key takeaway Threads improve performance — but proper synchronization is the key to safe concurrency. --- If you're preparing for Java interviews or building scalable systems, understanding threads is fundamental. In my next post I’ll explain: 🧵 How Thread Pools work internally (Executor Framework) --- #Java #Multithreading #BackendEngineering #SoftwareArchitecture #JavaDeveloper #LearningInPublic
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