What are methods in Java example?
What are methods in Java example?
I'd be happy to help!
In Java, a method is a block of code that can be called multiple times from your program. It's like a function or procedure in other programming languages. Methods allow you to reuse code and make it easier to maintain and modify your program.
Here are some examples of methods in Java:
1. A simple method:
public class MethodExample {
public static void printHello() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
printHello();
}
}
In this example, the printHello()
method simply prints "Hello, World!" to the console. You can call this method multiple times from your program without having to rewrite the code.
2. A method with parameters:
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("5 + 3 = " + add(5, 3));
}
}
In this example, the add()
method takes two integer parameters and returns their sum. You can call this method with different values to perform calculations.
3. A method that returns a value:
public class MathHelper {
public static double sqrt(double num) {
return Math.sqrt(num);
}
public static void main(String[] args) {
System.out.println("The square root of 16 is " + sqrt(16));
}
}
In this example, the sqrt()
method takes a double value and returns its square root. You can use this method to perform calculations that involve square roots.
4. A method that modifies an object:
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(100);
System.out.println("Balance: " + account.getBalance());
account.withdraw(50);
System.out.println("Balance: " + account.getBalance());
}
}
In this example, the BankAccount
class has methods that allow you to deposit and withdraw money from an account. The getBalance()
method returns the current balance.
5. A method that uses recursion:
public class Factorial {
public static int factorial(int num) {
if (num == 0) {
return 1;
} else {
return num * factorial(num - 1);
}
}
public static void main(String[] args) {
System.out.println("Factorial of 5 is " + factorial(5));
}
}
In this example, the factorial()
method calculates the factorial of a given number using recursion. The method calls itself with decreasing values until it reaches 0.
These are just a few examples of methods in Java. Methods can be used to perform a wide range of tasks and can make your program more efficient and easier to maintain.
What are main methods java example w3schools
I'll respond in English for now .
W3Schools provides an excellent Java tutorial section with various examples and exercises to help beginners learn the language. Here, we will focus on the main methods in Java, along with some examples from W3Schools:
What are Main Methods?
In Java, a main method is the entry point of any Java program. It's where the program starts execution when you run it. The main
method is declared as public static void main(String[] args)
in every Java program.
Example 1: Simple Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This example demonstrates a simple Java program that prints "Hello, World!" to the console. The main
method is where the program starts execution.
Example 2: Command-Line Arguments
public class ArgsExample {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + i + ": " + args[i]);
}
}
}
In this example, you can pass command-line arguments to the program. The main
method iterates through the array of strings (args
) and prints each argument.
Example 3: User Input
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
This example demonstrates how to get user input using the Scanner
class. The program prompts the user to enter their name and then prints a greeting message with the entered name.
Example 4: Method Call
public class MethodCall {
public static void main(String[] args) {
greet("John");
greet("Jane");
}
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
In this example, the main
method calls another method (greet
) with different arguments. The greet
method prints a greeting message with the passed name.
These examples demonstrate the basic usage of the main
method in Java. W3Schools provides many more tutorials and exercises to help you learn Java programming.