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);
}
}
}