Java From Beginner To Advanced

0% completed

Previous
Next
Pre-defined Methods in Java

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.

Common Pre-defined Methods in Java

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:

1. Math Class Methods

The Math class provides methods for performing basic numeric operations such as exponentiation, logarithms, square roots, and trigonometric functions.

MethodDescriptionExample 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

2. Array Class Methods

The Arrays class contains various methods for manipulating arrays (such as sorting and searching).

MethodDescriptionExample 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

3. String Class Methods

The String class provides methods for manipulating and analyzing strings.

MethodDescriptionExample 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

Example 1: Using Math Pre-defined Methods

Below is an example that demonstrates the use of several pre-defined methods from the Math class to perform mathematical calculations.

Java
Java

. . . .

Explanation:

  • abs() Method:
    • Math.abs(-25) returns 25, the absolute value of -25.
    • Output: 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.
    • Output: 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.
    • Output: Square root of 144 is: 12.0
  • random() Method:
    • Math.random() generates a random double between 0.0 (inclusive) and 1.0 (exclusive).
    • Output: Random number between 0.0 and 1.0: 0.534... (varies each run)

Example 2: Using Array Pre-defined Methods

Below is an example that demonstrates the use of pre-defined methods from the Arrays class to manipulate and display array data.

Java
Java

. . . .

Explanation:

  • Arrays.toString() Method:
    • Converts the array {5, 2, 8, 1, 9} to the string "[5, 2, 8, 1, 9]".
    • Output: Original array: [5, 2, 8, 1, 9]
  • Arrays.sort() Method:
    • Sorts the array in ascending order, resulting in {1, 2, 5, 8, 9}.
    • Output: Sorted array: [1, 2, 5, 8, 9]
  • Arrays.binarySearch() Method:
    • Searches for the element 8 in the sorted array and returns its index 3.
    • Output: Index of number 8: 3

Example 3: Using String Pre-defined Methods

Below is an example that demonstrates the use of pre-defined methods from the String class to manipulate and analyze string data.

Java
Java

. . . .

Explanation:

  • length() Method:
    • message.length() returns 12, which is the number of characters in "Hello, Java!".
    • Output: Length of the message: 12
  • substring() Method:
    • message.substring(0, 5) extracts characters from index 0 to 4, resulting in "Hello".
    • Output: Greeting: Hello
  • toUpperCase() Method:
    • message.toUpperCase() converts all characters to uppercase, resulting in "HELLO, JAVA!".
    • Output: Uppercase Message: HELLO, JAVA!
  • equals() Method:
    • message.equals("Hello, Java!") compares the two strings and returns true since they are identical.
    • Output: Is message equal to 'Hello, Java!': true

Why Use Pre-defined Methods?

  • 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.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next