Java methods list with examples

Bill 177 Published: 10/13/2024

Java methods list with examples

I'm happy to help!

Java provides a wide range of built-in and user-defined methods that can be used to perform various tasks. Here are some common Java method types along with their examples:

1. Void Methods:

These methods do not return any value.

Example:

public static void printHello() {

System.out.println("Hello");

}

To call this method, you would use printHello();

2. Return-Type Methods:

These methods return a value of a specified type.

Example (returns an integer):

public static int getSum(int a, int b) {

return a + b;

}

To call this method and use the returned value, you would do something like:

int result = getSum(2, 3);

System.out.println("The sum is " + result);

3. Constructor Methods:

These methods are used to initialize objects when they are created.

Example (constructs a new Person object):

public class Person {

private String name;

public Person(String name) {

this.name = name;

}

}

Person p = new Person("John");

4. Static Methods:

These methods can be called without creating an instance of the class.

Example (prints "Hello" to the console):

public class Greeting {

public static void printHello() {

System.out.println("Hello");

}

}

Greeting.printHello(); // Output: Hello

5. Instance Methods:

These methods must be called on an instance of the class.

Example (calculates the area of a rectangle):

public class Rectangle {

private int width;

private int height;

public Rectangle(int w, int h) {

width = w;

height = h;

}

public double getArea() {

return width * height;

}

}

Rectangle r = new Rectangle(5, 3);

double area = r.getArea(); // Output: 15.0

6. Abstract Methods:

These methods must be implemented by any subclass.

Example (abstract method in an Animal class):

public abstract class Animal {

public abstract void makeSound();

}

public class Dog extends Animal {

@Override

public void makeSound() {

System.out.println("Woof!");

}

}

Dog d = new Dog();

d.makeSound(); // Output: Woof!

7. Final Methods:

These methods cannot be overridden by subclasses.

Example (final method in a class):

public class MyClass {

public final void finalize() {

System.out.println("Object is being finalized");

}

}

These are just a few examples of the many types of Java methods you can create. Remember to always follow proper naming conventions and usage guidelines when writing your own methods!

What are the list interface methods in Java?

The list of interface methods in Java includes:

add(): This method is used to add elements to a collection. It can take one or more elements as parameters.
public boolean add(E e);

addAll(): This method is used to add multiple elements from another collection to this collection.
public boolean addAll(Collection c);

clear(): This method is used to remove all elements from a collection.
public void clear();

contains(): This method checks if a specific element is present in the collection or not.
public boolean contains(Object o);

containsAll(): This method checks if a specific collection is a subset of this collection or not.
public boolean containsAll(Collection c);

equals(): This method compares two collections for equality.
public boolean equals(Object o);

hashCode(): This method returns the hash code value for the collection.
public int hashCode();

isEmpty(): This method checks if the collection is empty or not.
public boolean isEmpty();

iterator(): This method returns an iterator that allows you to traverse the elements of this collection in proper sequence.
public Iterator iterator();

remove(): This method removes a specific element from a collection.
public boolean remove(Object o);

removeAll(): This method removes multiple elements from another collection to this collection.
public boolean removeAll(Collection c);

retainAll(): This method retains only the elements in this collection that are contained within the given collection.
public boolean retainAll(Collection c);

set(): This method replaces all of the elements in a collection with a specified element.
public void set(int index, E element);

size(): This method returns the number of elements in the collection.
public int size();

toArray(): This method returns an array containing all the elements in this collection.
public  T[] toArray(T[] a);

toString(): This method returns a string representation of this collection, typically the class name followed by the list of elements.
public String toString();

toArray(): This method returns an array containing all the elements in this collection, in proper sequence (partially).
public Object[] toArray();

These are the most common interface methods in Java for working with collections.