MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
  • Unit 1: Using Objects and Methods
    • Part 1: Fundamentals
    • Part 2: Operations and Documentation
      • Casting and Range of Variables
      • Compound Assignment Operators
      • Application Program Interface (API) and Libraries
      • Docummentation with Comments
      • Method Signatures
      • Unit 1 Part 2 Slides
    • Part 3: Objects and Classes
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections

Unit 1.9: Method Signatures and Procedural Abstraction

Methods and Procedural Abstraction

A method is a named block of code that performs a specific task when it is called.

Code Blocks
  • Braces { } group statements into logical units. Blocks define the scope of variables and the boundaries of methods, classes, and control structures.

What is a Block of Code? In Java, a block of code is any section of code that is enclosed in curly braces { }. These braces group multiple statements together into a single unit. Common constructs that use blocks of code include:

  • Classes: The entire definition of a class is a block.
  • Methods: The body of a method is a block.
  • Conditionals: The code executed if an if statement is true is a block.
  • Loops: The code that repeats in a for or while loop is a block.

Task: Identifying different types of code blocks.

public class BlockExample { // Start of Class Block
    
    public void myMethod() { // Start of Method Block
        int x = 10;
        
        if (x > 5) { // Start of Conditional Block. 
            System.out.println("x is greater than 5");
        } // End of Conditional Block. More on this later
        
    } // End of Method Block
    
} // End of Class Block

Procedural abstraction allows a programmer to use a method without needing to know the details of how it's implemented. This is a foundational concept in computer science. You only need to know what the method does, what information it needs (parameters), and what it returns (if anything). This is often referred to as the method's "contract."

Parameters and Method Signatures

What is a Parameter?

A parameter is a variable declared in the header of a method or constructor. It acts as a placeholder for a value that will be provided when the method is called.

  • Purpose: Parameters allow values, or arguments, to be passed into a method so they can be used inside the method's body.
  • Scope: Parameters can only be used within the body of the method where they are declared.

How Parameters Work?

Think of a parameter as a "variable name" defined in the method's blueprint, and an argument as the "actual value" you plug into that variable when you run the method.

Task: Distinguishing between parameters and arguments.

// 'radius' is the parameter (the placeholder)
public void calculateCircleArea(double radius) {
    double area = 3.14 * radius * radius;
    System.out.println("Area: " + area);
}

// When calling the method, '5.0' is the argument (the actual value)
calculateCircleArea(5.0);

Method Signatures vs. Method Header

It is common to confuse the method signature with the method header.

  • Method Header: Includes the modifiers, return type, method name, and parameter list.
    • Example: public int calculateSum(int a, int b)
  • Method Signature: Consists ONLY of the method name and the ordered list of parameter types.
    • Example: calculateSum(int, int)

Crucial Distinction: The return type and parameter names are NOT part of the signature. This is why you cannot have two methods with the same name and parameters but different return types.

Example 1:

public int calculateSum(int a, int b)
  • Name: calculateSum
  • Parameter Types: (int, int)
  • Signature: calculateSum(int, int)

Example 2:

// The signature for this method is: addTwoNumbers(int, int)
public static int addTwoNumbers(int a, int b) 
{ // a and b are parameters
   return a + b;
}

Method Execution Flow

When a method is called, the program's sequential flow is interrupted.

  1. Control jumps from the calling line to the first line inside the called method.
  2. The code inside the method body is executed line by line.
  3. Once the method finishes (or hits a return statement), control jumps back to the point right after the original method call and continues.

Task: Tracing method execution flow.

public class Tutorial 
{
   public static void main(String[] args) 
   {
       System.out.println("Hello");
       displayGreetings(); // Flow jumps to the displayGreetings method
       System.out.println("Goodbye"); // Execution resumes here after the method call
   }
   
   public static void displayGreetings() 
   {
       System.out.println("Have a good day!");
   }
}

Execution Flow Diagram: Method call flow diagram

Output:

Hello
Have a good day!
Goodbye

Parameters and Arguments

  • Parameter: The variable declared in the method header (the "placeholder" for a value).
  • Argument: The actual value or expression passed to the method when it is called.

Example:

public class NumberDoubler 
{
   public static void main(String[] args) 
   {
       // '10' is the argument passed to the method
       int doubledNumber = doubleTheNumber(10); 
       System.out.println("Doubled number is " + doubledNumber);
   }
   
   // 'a' is the parameter
   public static int doubleTheNumber(int a)
   {
       System.out.println("Received number: " + a + ". Doubling it now...");
       return 2 * a;
   }
}

Call by Value: In Java, all arguments are passed using "call by value."

Call by Value
  • Java only uses call-by-value. A copy of the value is passed to the method. If you modify a parameter of a primitive type inside a method, the original variable used as the argument remains unchanged.

This means a copy of the argument's value is passed to the method's parameter. Any changes made to the parameter variable inside the method do not affect the original argument variable outside the method.

Void Methods

A void method performs an action but does not return a value to the caller.

  • Because it returns nothing, it cannot be used as part of an expression (e.g., int x = myVoidMethod(); would cause a compiler error).
  • The System.out.println() method is a common example of a void method.

Task: Implementing a void method.

public static void displayGreetings() 
{
   System.out.println("Have a good day!");
}

Non-void Method

A non-void method uses the return keyword to send back a single value of a specific type.

  • The return statement immediately exits the method.
  • The returned value must be "used" by the caller—for example, stored in a variable, printed, or used in a calculation.
  • Example: Math.sqrt(25.0) is a non-void method that returns a double.

Task: Implementing a non-void method that returns an integer.

public static int doubleTheNumber(int a)
{
   return 2 * a; // Returns an int value
}

Method Overloading

Methods are overloaded when multiple methods in the same class have the same name but different signatures (i.e., a different number or different types of parameters).

Method Overloading
  • Overloading allows a class to have multiple methods with the same name, provided their parameter lists are unique. This provides flexibility to handle different types of input using a single, intuitive method name.

This allows you to define methods that perform similar tasks on different types of data.

Task: Implementing overloaded methods.

// Overloaded methods for printing information
public void printInfo(String name) 
{ 
    System.out.println("Name: " + name); 
}

public void printInfo(int age) 
{ 
    System.out.println("Age: " + age); 
}

public void printInfo(String name, int age) 
{ 
    System.out.println("Name: " + name + ", Age: " + age); 
}

The compiler knows which version of printInfo to call based on the arguments you provide.

Recap: Method Declaration Components

A method declaration should have five components, in order:

  1. Optional modifiers—such as public, static, and others you will learn about later.

  2. Mandatory return type—the data type of the value returned by the method, or void if the method does not return a value.

  3. Mandatory method name—use a verb form and follow Java identifier naming conventions.

  4. Optional list of parameters in parentheses; a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.

  5. Mandatory method body, containing optional statements enclosed between left and right curly braces, which form the method code block.

The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a method body (even if empty) between curly braces, {}.

Privacy Policy | Terms & Conditions