Java methods list
Java methods list
I'm happy to help you with that! Here's a comprehensive list of Java methods:
Basic Methods
void main(String[] args)
- The main entry point for any Java program. int compareTo(Object o)
- Compares the object with the specified object for order. boolean equals(Object obj)
- Returns true if this object is equal to the specified object. int hashCode()
- Returns a hash code value for the object. String toString()
- Returns a string representation of the object.
Array Methods
void sort(int[] arr, int low, int high)
- Sorts an array of integers. int binarySearch(int[] arr, int target)
- Searches for an element in an array using a binary search algorithm. void copyOfRange(int[] srcArr, int srcPos, int srcEnd, int dstArr, int dstPos, int length)
- Copies a range of elements from one array to another.
Collection Methods
boolean add(E e)
- Adds an element to the collection. boolean remove(Object o)
- Removes the specified element from the collection. int size()
- Returns the number of elements in the collection. Iterator<E> iterator()
- Returns an iterator over the collection's elements. List<E> subList(int fromIndex, int toIndex)
- Returns a new list that is a view of the portion of this list between the specified fromIndex and toIndex.
String Methods
int compareTo(String anotherString)
- Compares two strings lexicographically. boolean endsWith(String suffix)
- Tests whether the string ends with the given suffix. boolean startsWith(String prefix)
- Tests whether the string starts with the given prefix. int indexOf(String str)
- Returns the index of the first occurrence of the specified substring. String substring(int beginIndex, int endIndex)
- Returns a new string that is a subset of this string.
Math Methods
double sin(double a)
- Returns the sine of an angle in radians. double cos(double a)
- Returns the cosine of an angle in radians. double tan(double a)
- Returns the tangent of an angle in radians. double sqrt(double a)
- Returns the square root of the specified number. int abs(int a)
- Returns the absolute value of an integer.
Miscellaneous Methods
void notify()
- Wakes up a single thread that is waiting on this object's monitor. boolean isInterrupted()
- Tests whether this thread has been interrupted. long getAvailableProcessors()
- Returns the number of available processors in the current JVM. double Math.random()
- Returns a pseudorandom double value between 0 (inclusive) and 1 (exclusive).
These are just a few examples of Java methods. There are many more, depending on the class or interface you're working with.
How many methods are there in Java?
I'd be happy to help you with that!
In Java, there are several methods that can be used for various purposes. The main types of methods are constructors, instance methods, and class methods.
Constructors:
A constructor is a special method in Java that is called when an object is created. It initializes the object's state by assigning values to its instance variables. There is no return type for a constructor, not even void. A constructor's main purpose is to initialize objects of its class.
Java has two types of constructors:
Default Constructor: Also known as the no-arg constructor, it does not take any parameters. Parameterized Constructor: It takes one or more parameters.Instance Methods:
Instance methods are the most common type of method in Java. They can access and modify the state (instance variables) of an object. Instance methods belong to an instance of a class. They have access to all the member variables of the class, including private ones. There are three types of instance methods:
Non-Static Method: Also known as an instance method. It belongs to an instance of a class. Static Method: Also known as a class method. It belongs to the class itself, not an instance of the class. Static methods can be used without creating an object of the class. Abstract Method: A method that is declared but has no implementation.Class Methods:
Java provides two types of class methods:
Static Methods: Also known as class methods or shared methods. They are called on a class itself, not on an instance of the class. Final Methods: These are static and cannot be overridden by subclasses.In addition to these main categories, there are other types of methods in Java, such as:
Override Method: A method that is implemented in a subclass to provide specialized behavior. Abstract Override Method: An abstract method that is declared but has no implementation. Private Methods: Methods that can only be accessed within the same class.In total, there are over 20 different types of methods in Java! Each with its own unique characteristics and purposes.
Hope this helps you understand Java methods better!