Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Function

In Java, functions are known as methods. A method is a block of code that performs a specific task and can be called from other parts of the program. Methods are used for code reusability and organization. Here's how you define and use methods in Java:

Method Declaration:

In Java, a method is declared within a class. The basic syntax of a method declaration looks like this:

access_modifier return_type method_name(parameter_list) {
    // Method body
    // Code to be executed
    return value; // Return statement (if the method returns a value)
}
  • access_modifier: Specifies the visibility of the method (public, private, protected, or package-private).

  • return_type: Specifies the data type of the value the method returns. Use void if the method doesn't return any value.

  • method_name: Specifies the name of the method.

  • parameter_list: Specifies the parameters the method accepts (if any).

  • return value: Specifies the value to be returned by the method (if applicable).

Example of a Method:

public class Example {

    // Method that takes two integers as parameters and returns their sum
    public int addNumbers(int a, int b) {
        int sum = a + b;
        return sum;
    }

    public static void main(String[] args) {
        Example example = new Example();
        int result = example.addNumbers(5, 10);
        System.out.println("Sum: " + result);
    }
}

Explanation:

  1. public int addNumbers(int a, int b) { ... }: This line declares a method named addNumbers with two integer parameters (a and b). The method returns an integer (int). Inside the method, the two parameters are added, and the sum is stored in the sum variable. The method then returns the sum.

  2. Example example = new Example();: Creates an instance of the Example class.

  3. int result = example.addNumbers(5, 10);: Calls the addNumbers method of the example object with the arguments 5 and 10. The returned sum (15 in this case) is stored in the result variable.

  4. System.out.println("Sum: " + result);: Prints the result, which is 15, to the console.

When you run this Java program, it will output:

Sum: 15

This output shows the result of calling the addNumbers method with the arguments 5 and 10. The method calculated the sum and returned the value 15, which was then printed in the main method.

PreviousLoopingNextVariadic Function

Last updated 1 year ago