types of functions in java

Sandra 100 Published: 09/11/2024

types of functions in java

I'm happy to provide a detailed answer in English!

Java is an object-oriented programming language that supports multiple programming paradigms, including procedural, functional, and imperative programming. In Java, a function is a block of code that can be invoked repeatedly with different inputs. Here are some types of functions in Java:

Method: A method is a function that belongs to a class and is used to perform a specific task. Methods can take arguments, return values, and access the instance variables of the class.

Example: public void sayHello(String name) { System.out.println("Hello " + name); }

Constructor: A constructor is a special type of method that is called when an object is created. It initializes the object's state by setting its properties or calling other methods.

Example: public MyClass(int x, int y) { this.x = x; this.y = y; }

Static Method: A static method is a function that belongs to a class rather than an instance of the class. Static methods can only access static variables and cannot modify instance state.

Example: public static void printHello() { System.out.println("Hello"); }

Lambda Expression: Lambda expressions are small, anonymous functions that can be used as arguments to higher-order functions or as event handlers.

Example: Runnable r = () -> System.out.println("Hello");

Functional Interface: A functional interface is an interface with a single abstract method (SAM). Functional interfaces are used to define lambda expressions.

Example: interface Print { void print(String s); }

Inner Class Method: An inner class method is a method that is defined inside another class. Inner class methods can access the instance variables of the outer class and are often used for encapsulation.

Example: public class Outer { public class Inner { public void sayHello() { System.out.println("Hello"); } } }

Local Method: A local method is a function that is defined inside another method. Local methods can access the instance variables of the surrounding class and are often used for encapsulation.

Example: public void main(String[] args) { int x = 5; method(x); } public void method(int y) { System.out.println(y); }

** Anonymous Class Method**: An anonymous class method is a method that is defined inside another method, but it's not associated with any instance of the class.

Example: public void main(String[] args) { new Runnable() { public void run() { System.out.println("Hello"); } }.run(); }

These are some common types of functions in Java. Functions play a crucial role in Java programming, and understanding their types is essential for writing efficient, readable, and maintainable code.

Remember: "Code should be written to be understood by humans first, and machines second." - Alan Perlis

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.