java functions list

Stanley 104 Published: 09/02/2024

java functions list

Here are some common Java functions that you can use:

String Functions:

toLowerCase(): converts a string to lowercase. toUpperCase(): converts a string to uppercase. concat(String s): concatenates a string with another string. substring(int beginIndex, int endIndex): returns a subset of characters from the original string. indexOf(String str): finds the index of the first occurrence of a specified value within an array. lastIndexOf(String str): finds the index of the last occurrence of a specified value within an array. split(String regex): splits this string around matches of the given regular expression. trim(): removes whitespace from both ends of a string. replace(String oldPattern, String newPattern): replaces all occurrences of the first argument with the second.

Math Functions:

abs(int a): returns the absolute value of an integer. ceil(double a): returns the smallest integer that is greater than or equal to the input value. floor(double a): returns the largest integer that is less than or equal to the input value. max(double a, double b): returns the maximum value of two numbers. min(double a, double b): returns the minimum value of two numbers. pow(double a, double b): calculates the power of one number raised to another. random(): returns a pseudorandom number in the range 0.0 through 1.0. sqrt(double a): calculates the square root of a number.

Array Functions:

length(): returns the size of an array. get(int index): returns the value at the specified position in an array. set(int index, int value): assigns a new value to the element at the specified position. equals(Object obj): checks whether two arrays are equal.

Date and Time Functions:

format(String format): formats a date according to a given pattern. parse(String text): parses a string into a Date object. getTime(): returns the current system time in milliseconds. sleep(long milliseconds): causes the calling thread to sleep for a specified period of time.

File and Directory Functions:

listFiles(): returns an array of files that can be found within this directory. mkdir(String dir): creates a new directory with the specified name. delete(): deletes the file or directory denoted by this abstract path name. exists(): checks whether a file or directory exists.

Miscellaneous Functions:

className(): returns the name of the class of which an invocation is part. getClass(): returns the runtime class of this Java object. notify(): resets the lock on this object and test whether it should be notified. wait(long timeout): causes the current thread to wait until either another thread invades the Object's critical section or a specified amount of real time has elapsed.

This is not an exhaustive list, but it gives you an idea of the types of functions available in Java.

java function class

Here is a Java function class that demonstrates several common methods and concepts:

public class JavaFunctionClass {

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

return a + b;

}

public static String concatenate(String a, String b) {

return a + b;

}

public static boolean isEqual(int a, int b) {

return a == b;

}

public static double calculateAverage(double[] numbers) {

double sum = 0.0;

for (double number : numbers) {

sum += number;

}

return sum / numbers.length;

}

public static void main(String[] args) {

System.out.println("Addition: " + add(5, 3));

System.out.println("Concatenation: " + concatenate("Hello", "World"));

System.out.println("Equality: " + isEqual(10, 10));

double[] numbers = {1.0, 2.0, 3.0, 4.0};

System.out.println("Average: " + calculateAverage(numbers));

}

}

In this class:

add(int a, int b) method takes two integer parameters and returns their sum. concatenate(String a, String b) method concatenates two string parameters into one string and returns it. isEqual(int a, int b) method checks if two integers are equal and returns true or false. calculateAverage(double[] numbers) method calculates the average of an array of double numbers. In the main() method: We test the add() method with arguments 5 and 3. We test the concatenate() method with string arguments "Hello" and "World". We test the isEqual() method with integer arguments 10 and 10 (which are equal). We calculate the average of an array of four double numbers: {1.0, 2.0, 3.0, 4.0}.