Java is a versatile programming language that provides a broad set of built-in methods to simplify tasks and provide ready-to-use functionality. These built-in methods are part of the Java's standard library, or API, and cover different areas such as mathematical operations, string manipulation, working with arrays, and more. In this section, we will explore some of the key and commonly used built-in methods in Java.
1. `java.lang.Math`:
Method |
Description |
Example |
abs() |
Returns the absolute value of a number. |
int absoluteValue = Math.abs(-10); // Result: 10 |
ceil() |
Rounds a decimal value up to the nearest integer. |
double roundedUp = Math.ceil(4.3); // Result: 5.0 |
floor() |
Rounds a decimal value down to the nearest integer. |
double roundedDown = Math.floor(4.8); // Result: 4.0 |
max() |
Returns the larger of two numbers. |
int largest = Math.max(10, 20); // Result: 20 |
min() |
Returns the smaller of two numbers. |
int smallest = Math.min(10, 20); // Result: 10 |
pow() |
Calculates the power of a number. |
double result = Math.pow(2, 3); // Result: 8.0 |
sqrt() |
Calculates the square root of a number. |
double result = Math.sqrt(25); // Result: 5.0 |
round() |
Rounds a decimal value to the nearest whole number. |
long roundedValue = Math.round(4.6); // Result: 5 |
exp() |
Returns Euler's number e raised to the power of a double value. |
double result = Math.exp(1); // Result: 2.718281828459045 |
log() |
Returns the natural logarithm (base e) of a double value. |
double result = Math.log(Math.E); // Result: 1.0 |
2. `java.lang.String`:
Method |
Description |
Example |
length() |
Returns the length of the string. |
String str = "Hello"; int length = str.length(); // Result: 5 |
charAt() |
Returns the char value at the specified index. |
String str = "Hello"; char ch = str.charAt(1); // Result: 'e' |
substring() |
Returns a substring of the original string. |
String str = "Hello World"; String subStr = str.substring(6); // Result: "World" |
contains() |
Checks if the string contains the specified sequence of char values. |
String str = "Hello"; boolean contains = str.contains("ell"); // Result: true |
indexOf() |
Returns the index of the first occurrence of the specified substring. |
String str = "Hello World"; int index = str.indexOf("World"); // Result: 6 |
toLowerCase() |
Converts all of the characters in this string to lower case. |
String str = "Hello"; String lowerStr = str.toLowerCase(); // Result: "hello" |
toUpperCase() |
Converts all of the characters in this string to upper case. |
String str = "Hello"; String upperStr = str.toUpperCase(); // Result: "HELLO" |
trim() |
Returns a string whose value is this string, with any leading and trailing whitespace removed. |
String str = " Hello "; String trimmedStr = str.trim(); // Result: "Hello" |
replace() |
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar. |
String str = "Hello World"; String replacedStr = str.replace(' ', '_'); // Result: "Hello_World" |
3. `java.util.Arrays`:
Method |
Description |
Example |
sort() |
Sorts the specified array into ascending numerical order. |
int[] numbers = {5, 2, 1, 3, 4}; Arrays.sort(numbers); // numbers after sort: {1, 2, 3, 4, 5} |
binarySearch() |
Searches the specified array of ints for the specified value using the binary search algorithm. |
int[] numbers = {1, 2, 3, 4, 5}; int index = Arrays.binarySearch(numbers, 3); // index: 2 |
equals() |
Returns true if the two specified arrays of ints are equal to one another. |
int[] array1 = {1, 2, 3}; int[] array2 = {1, 2, 3}; boolean isEqual = Arrays.equals(array1, array2); // Result: true |
fill() |
Assigns the specified int value to each element of the specified array of ints. |
int[] array = new int[5]; Arrays.fill(array, 10); // array after fill: {10, 10, 10, 10, 10} |
copyOf() |
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. |
int[] original = {1, 2, 3}; int[] copy = Arrays.copyOf(original, original.length); // copy: {1, 2, 3} |
4. `java.time.LocalDate`:
Method |
Description |
Example |
now() |
Obtains the current date from the system clock in the default time-zone. |
LocalDate today = LocalDate.now(); |
plusDays() |
Returns a copy of this LocalDate with the specified number of days added. |
LocalDate tomorrow = LocalDate.now().plusDays(1); |
minusDays() |
Returns a copy of this LocalDate with the specified number of days subtracted. |
LocalDate yesterday = LocalDate.now().minusDays(1); |
getDayOfWeek() |
Gets the day-of-week field, which is an enum DayOfWeek. |
DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek(); |
isLeapYear() |
Checks if the year is a leap year, according to the ISO proleptic calendar system rules. |
boolean isLeapYear = LocalDate.now().isLeapYear(); |