Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

Java Methods and Overloading: Parameters, return and Pass-by-Value

A method is a machine you build once and reuse. Six programs cover parameters and arguments, returning a value, 'missing return statement', early return, why doubleIt(n) leaves n at 10, and overloading print three ways.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

What you will learn

  • Name every part of a method: modifiers, return type, name, parameters, body and return
  • Tell parameters from arguments and return a value to the caller
  • Follow the return rules: every path returns, nothing runs after return
  • Explain why a method changes only its copy, and overload a method correctly

Before you start

  • Java loops: for, while, do-while and for-each

Think of a coffee machine. You build it once, then press the button again and again: a shot, a double, a decaf. Each press repeats the same steps. A method is that machine: a piece of code with a name that you write once and call wherever you need it. Overloading lets one name do a few different jobs.

One method, many calls

package org.studyeasy.s02l13;

public class Ex01_Loop {
    static void loop(int start, int stop) {
        for (int i = start; i <= stop; i++) {
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        loop(1, 3);
        loop(20, 22);
    }
}
1
2
3
20
21
22

A loop repeats code in one place; a method goes a step further, because you can use it from anywhere. loop is the method, and start and stop are its parameters, the slots you declare. 1, 3 and 20, 22 are the arguments, the values you pass in when you call it. main is a method too: the entry point where Java always starts. For now, static just means you can call the method straight from main. Call loop(1) with one argument and javac complains that it needs two ints.

Every part of a method

The method public static int add(int a, int b) { return a + b; } labelled: modifiers public static, return type int, name add, parameters int a and int b, body in braces, and return hands the value back. public static int add (int a, int b) { return a + b; } modifiers return type name parameters body; return hands back the value and exits void as the return type means nothing comes back. Name it with a verb, in camelCase: add, grade, greet.
The shape of a method, from modifiers to return.

A method that returns

package org.studyeasy.s02l13;

public class Ex02_Add {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int total = add(3, 4);
        System.out.println(total);
        System.out.println(add(10, 5) * 2);
    }
}
7
30

static int add promises a whole number back, and return a + b hands it to the caller. total keeps the 7. You can also use the call directly in an expression: add(10, 5) is 15, times 2 is 30. Store the result in String total instead and javac says an int cannot be converted to String.

The rules about return

Every path must return

package org.studyeasy.s02l13;

public class Ex03_Grade {
    static String grade(int score) {
        if (score >= 50) {
            return "Pass";
        }
        return "Fail";
    }

    public static void main(String[] args) {
        System.out.println(grade(72));
        System.out.println(grade(30));
    }
}
Pass
Fail

If the score is under 50, the method still has to give something back. Delete return "Fail"; and javac reports "missing return statement": there's a path where nothing comes back.

return ends the method

package org.studyeasy.s02l13;

public class Ex04_EarlyReturn {
    static void greet(String name) {
        if (name.isEmpty()) {
            return;
        }
        System.out.println("Hi " + name);
    }

    static int square(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        greet("");
        greet("Priya");
        System.out.println(square(5));
    }
}
Hi Priya
25

In a void method, a bare return; quits early, so the empty name prints nothing. And nothing runs after return: add a println below return n * n; and javac says "unreachable statement".

The method works on a copy

package org.studyeasy.s02l13;

public class Ex05_Copy {
    static void doubleIt(int x) {
        x = x * 2;
    }

    static int twice(int x) {
        return x * 2;
    }

    public static void main(String[] args) {
        int n = 10;
        doubleIt(n);
        System.out.println("n = " + n);

        n = twice(n);
        System.out.println("n = " + n);
    }
}
n = 10
n = 20

When you call doubleIt(n), a photocopy of 10 travels into x. Inside, x becomes 20, but the copy is thrown away when the method ends, and n is still 10. To get the result out, return it and store it: n = twice(n) gives 20. Write just twice(n); and the result is discarded again.

Overloading: one name, several jobs

package org.studyeasy.s02l13;

public class Ex06_Overloading {
    static void print(int n) {
        System.out.println("int: " + n);
    }

    static void print(String s) {
        System.out.println("String: " + s);
    }

    static void print(int a, int b) {
        System.out.println("sum: " + (a + b));
    }

    public static void main(String[] args) {
        print(7);
        print("Emma");
        print(3, 4);
    }
}
int: 7
String: Emma
sum: 7

Same name, different parameters, and Java picks the version by what you pass. What counts as different?

PairAllowed?Why
print(int) and print(String)Yesparameter types differ
print(int) and print(int, int)Yesnumber of parameters differs
print(int n) and print(int x)Noparameter names don't count
int half(int) and double half(int)Noreturn type alone isn't enough

Add static double print(int n) to the class and javac says print(int) is already defined.

Recap

  • A method is named, reusable logic: inputs (parameters), steps (body), an optional result (return type).
  • Parameters are declared; arguments are passed.
  • Every path must return; nothing runs after return.
  • The method gets a copy of a primitive argument. Return the result and store it.
  • Overloads differ by parameters, never by return type alone.

Next: where variables live and die, in scope, code blocks and the call stack.

Keep going in Java · 16 of 79

Previous
Java Arrays: Indexes, Default Values, Loops and 2D Grids
Next post
Java Scope, Code Blocks and the Call Stack
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program

Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.

Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA

Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.

Java Hello World with Maven: What the JDK Does and What Maven Adds

javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.

Java Variables, Keywords and Naming Rules

A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.

On this page

  • One method, many calls
  • Every part of a method
  • A method that returns
  • The rules about return
  • Every path must return
  • return ends the method
  • The method works on a copy
  • Overloading: one name, several jobs
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.