How many methods are there in java with examples

Loren 155 Published: 08/01/2024

How many methods are there in java with examples

I'm glad you asked! In Java, there are several built-in methods that can be used to perform various operations. Here are some of the most commonly used methods in Java:

1. Arithmetic Methods

Java provides a set of arithmetic methods for performing basic mathematical operations.

+ (addition): int a = 5; int b = 3; int sum = a + b; - (subtraction): int a = 5; int b = 3; int difference = a - b;

* (multiplication): int a = 5; int b = 3; int product = a * b; / (division): int a = 10; int b = 2; int quotient = a / b;

% (modulus): int a = 17; int b = 5; int remainder = a % b;

2. Comparison Methods

Java provides comparison methods to compare values.

== (equality): int a = 5; int b = 5; boolean result = a == b; != (inequality): int a = 5; int b = 3; boolean result = a != b; < (less than): int a = 5; int b = 7; boolean result = a < b; > (greater than): int a = 7; int b = 5; boolean result = a > b; <= (less than or equal to): int a = 5; int b = 5; boolean result = a <= b;

>= (greater than or equal to): int a = 7; int b = 7; boolean result = a >= b;

3. Logical Methods

Java provides logical methods for performing logical operations.

&& (logical and): boolean a = true; boolean b = false; boolean result = a && b; || (logical or): boolean a = true; boolean b = false; boolean result = a || b; ! (logical not): boolean a = true; boolean result = !a;

4. String Methods

Java provides various string methods for manipulating strings.

toString() (converts an object to a string): Integer i = 5; String str = i.toString(); concat() (concatenates two strings): String str1 = "Hello "; String str2 = "World"; String result = str1.concat(str2); trim() (removes whitespace from the beginning and end of a string): String str = " Hello World "; String result = str.trim();

5. Array Methods

Java provides array methods for manipulating arrays.

length (gets the length of an array): int[] arr = new int[5]; int length = arr.length; clone() (creates a copy of an array): int[] original = {1, 2, 3}; int[] clone = original.clone();

6. Math Methods

Java provides math methods for performing mathematical operations.

abs() (calculates the absolute value of a number): double x = -5.5; double result = Math.abs(x); ceil() (rounds up to the nearest integer): double x = 3.7; int result = (int) Math.ceil(x);

7. Date Methods

Java provides date methods for working with dates and times.

parse() (parses a string into a date object): String str = "2022-01-01"; Date date = Date.parse(str); format() (formats a date object as a string): Date date = new Date(); String str = DateFormat.format(date, "yyyy-MM-dd");

These are just a few examples of the many methods available in Java. Each method has its own unique purpose and can be used to perform a specific operation or task.

How many main methods are there in Java?

I'm glad you asked! In Java, there are several ways to execute a program, and we call them "main methods." Yes, you read that right - multiple ways!

Let's start with the most common one:

1. The traditional main method (public static void main(String[] args))

This is the original and widely used way to write a Java program. It's defined in the public class declaration, and it takes an array of strings as an argument. This method is responsible for executing the program when you run it from the command line or use an Integrated Development Environment (IDE).

Here's why this is called "traditional": back in the early days of Java, this was the only way to write a Java program that could be executed outside of an IDE.

2. The static block (static { ... })

In Java 14 and later versions, you can use a static block to execute code when your program starts. This is similar to the traditional main method but doesn't take any arguments.

Here's why this is useful: sometimes you need to perform some initialization or setup tasks before your main code runs. You can put these tasks in a static block, and Java will execute it automatically when your program starts.

3. The lambda expression (using the Runtime.getRuntime().run() method)

In Java 8 and later versions, you can use lambda expressions to create small, anonymous functions. One way to do this is by using the Runtime.getRuntime().run() method to execute a lambda expression as if it were a command-line program.

Here's why this is useful: sometimes you need to perform some one-time tasks or calculations without starting up a full-fledged Java program. You can write a lambda expression and pass it to the run method, which will execute it as a separate process.

4. The JavaFX main method (public static void start(Stage stage))

If you're using JavaFX (Java for building GUI applications), you need to define a start method in your main class instead of a traditional main method. This method takes a Stage object as an argument, which is the top-level container for your GUI application.

Here's why this is useful: JavaFX allows you to build desktop applications with a rich set of UI components and APIs. When you run a JavaFX program, it will start up and display your GUI application on the screen.

5. The GraalVM native image main method (public static void main(String[] args))

GraalVM is an open-source project that allows you to compile your Java code into native executables (like .so or .dll files) that can run without a JVM. When you use GraalVM, you need to define a main method in your class file, just like with traditional Java.

Here's why this is useful: if you have a performance-critical Java application, you might want to consider compiling it into a native executable using GraalVM. This can provide significant speedups and reduced memory usage compared to running your code through the JVM.

In summary, there are several ways to execute a Java program:

The traditional main method The static block Lambda expressions with Runtime.getRuntime().run() JavaFX's start method (for GUI applications) GraalVM native image main method

Each of these methods has its own unique use cases and advantages. By understanding how each of them works, you can choose the best approach for your specific needs and write more efficient, effective Java code!