0% completed
In Java, pre-defined methods (also known as built-in methods) are methods that are already provided by Java's standard libraries. These methods are part of Java's core classes and packages, allowing you to perform common tasks without writing the underlying code yourself. Utilizing pre-defined methods enhances code reusability, efficiency, and readability.
Java offers a variety of pre-defined methods across different classes. Here are some of the most commonly used methods categorized by their respective classes:
The Math
class provides methods for performing basic numeric operations such as exponentiation, logarithms, square roots, and trigonometric functions.
Method | Description | Example Usage |
---|---|---|
abs(int a) | Returns the absolute (positive) value of an integer. | Math.abs(-10) returns 10 |
pow(double a, double b) | Returns the value of a raised to the power of b . | Math.pow(2, 3) returns 8.0 |
sqrt(double a) | Returns the square root of a number. | Math.sqrt(16) returns 4.0 |
random() | Returns a double value greater than or equal to 0.0 and less than 1.0 . | Math.random() might return 0.534 |
The Arrays
class contains various methods for manipulating arrays (such as sorting and searching).
Method | Description | Example Usage |
---|---|---|
sort(int[] a) | Sorts the specified array of integers into ascending order. | Arrays.sort(new int[]{3, 1, 2}) results in {1, 2, 3} |
toString(int[] a) | Returns a string representation of the array. | Arrays.toString(new int[]{1, 2, 3}) returns "[1, 2, 3]" |
binarySearch(int[] a, int key) | Searches the specified array for the specified value using the binary search algorithm. The array must be sorted prior to making this call. | Arrays.binarySearch(new int[]{1, 2, 3}, 2) returns 1 |
The String
class provides methods for manipulating and analyzing strings.
Method | Description | Example Usage |
---|---|---|
length() | Returns the number of characters in the string. | "Hello".length() returns 5 |
toUpperCase() | Converts all characters in the string to uppercase. | "hello".toUpperCase() returns "HELLO" |
equals(Object anObject) | Compares two strings for equality. | "Java".equals("java") returns false |
Below is an example that demonstrates the use of several pre-defined methods from the Math
class to perform mathematical calculations.
Explanation:
abs()
Method:
Math.abs(-25)
returns 25
, the absolute value of -25
.Absolute value of -25 is: 25
pow()
Method:
Math.pow(2.0, 5.0)
calculates 2.0
raised to the power of 5.0
, resulting in 32.0
.2.0 raised to the power of 5.0 is: 32.0
sqrt()
Method:
Math.sqrt(144)
returns 12.0
, the square root of 144
.Square root of 144 is: 12.0
random()
Method:
Math.random()
generates a random double between 0.0
(inclusive) and 1.0
(exclusive).Random number between 0.0 and 1.0: 0.534...
(varies each run)Below is an example that demonstrates the use of pre-defined methods from the Arrays
class to manipulate and display array data.
Explanation:
Arrays.toString()
Method:
{5, 2, 8, 1, 9}
to the string "[5, 2, 8, 1, 9]"
.Original array: [5, 2, 8, 1, 9]
Arrays.sort()
Method:
{1, 2, 5, 8, 9}
.Sorted array: [1, 2, 5, 8, 9]
Arrays.binarySearch()
Method:
8
in the sorted array and returns its index 3
.Index of number 8: 3
Below is an example that demonstrates the use of pre-defined methods from the String
class to manipulate and analyze string data.
Explanation:
length()
Method:
message.length()
returns 12
, which is the number of characters in "Hello, Java!"
.Length of the message: 12
substring()
Method:
message.substring(0, 5)
extracts characters from index 0
to 4
, resulting in "Hello"
.Greeting: Hello
toUpperCase()
Method:
message.toUpperCase()
converts all characters to uppercase, resulting in "HELLO, JAVA!"
.Uppercase Message: HELLO, JAVA!
equals()
Method:
message.equals("Hello, Java!")
compares the two strings and returns true
since they are identical.Is message equal to 'Hello, Java!': true
Efficiency: Pre-defined methods are optimized for performance and reliability, saving you time from implementing common functionalities.
Reusability: They allow you to reuse code without rewriting standard operations, making your programs cleaner and more maintainable.
Readability: Using well-named pre-defined methods makes your code more understandable to others and to your future self.
Functionality: Java's standard library provides a wide range of methods that cover most programming needs, from mathematical calculations to data manipulation.
Pre-defined methods in Java empower you to perform a multitude of tasks efficiently and effectively without reinventing the wheel. By familiarizing yourself with these methods and understanding how to utilize them, you can write more robust, readable, and maintainable code.
Explore Java's extensive standard library to discover even more methods that can aid in your programming endeavors.
.....
.....
.....