Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Java tutorial – 3 Java methods

Posted on 06/01/202306/08/2023 by user

Methods in Java

In Java, a method is a block of code that only runs when it is called, you can put some parameters as input, and it returns the value as outputs or void. 

  • When a parameter is passed to the method, it is called an argument. 
  • The void keyword indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method.

To call a method in Java, write the method’s name followed by two parentheses () and a semicolon. 

public class Main {
  static int myMethod(int x) {
    return 5 + x;
  }

  public static void main(String[] args) {
    System.out.println(myMethod(3));
  }
}
// Outputs 8 (5 + 3)

Methods are typically defined within a class and can have various modifiers such as public, private, static, final, etc.

  • Public: A method marked as public can be accessed from anywhere, including any class in any package.
  • Private: A method marked as private can only be accessed within the class it is defined in.
  • Static: A method marked as static belongs to the class rather than an instance of the class. You can call a static method without creating an instance of the class.
  • Final: A method marked as final cannot be overridden by subclasses.

Method Overloading 

With method overloading, multiple methods can have the same name with different parameters.

int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)

Java Control Flow

Condition Statement 

Java has the following conditional statements: 

  • Use if to specify a block of code to be executed, if a specified condition is true 
  • Use else to specify a block of code to be executed, if the same condition is false 
  • Use else if to specify a new condition to test, if the first condition is false 
  • Use switch to specify many alternative blocks of code to be executed

Loops Statement

Loops can execute a block of code as long as a specified condition is reached. In Java, there are three types of loop statements: while loop, do-while loop for loop.  Besides loop statements, The break statement can also be used to jump out of a loop. The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Here are code examples of using control flow, and loop statements. 

public class Main {
    public static void main(String[] args) {
        int num = 10;

        // If..Else
        if(num > 10) {
            System.out.println("Number is greater than 10");
        } else {
            System.out.println("Number is not greater than 10");
        }

        // Else If
        if(num > 10) {
            System.out.println("Number is greater than 10");
        } else if(num < 10) {
            System.out.println("Number is less than 10");
        } else {
            System.out.println("Number is equal to 10");
        }

        // While loop
        int i = 0;
        while(i < num) {
            System.out.println(i);
            i++;
        }

        // For loop
        for(int j = 0; j < num; j++) {
            System.out.println(j);
        }

        // Switch
        switch(num) {
            case 1:
                System.out.println("Number is 1");
                break;
            case 10:
                System.out.println("Number is 10");
                break;
            default:
                System.out.println("Number is not 1 or 10");
                break;
        }

        // Break (used in a loop)
        for(int j = 0; j < num; j++) {
            if(j == 5) {
                break;  // exits the loop early
            }
            System.out.println(j);
        }

        // Continue (used in a loop)
        for(int j = 0; j < num; j++) {
            if(j == 5) {
                continue;  // skips the rest of this loop iteration                // rest = print..
            }
            System.out.println(j);
        }
    }
}

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}