In this Java article we want to learn about Java Built-In Methods, Java is popular programming language that is widely used for developing enterprise level applications, web applications and mobile applications. in this article we want to talk about some commonly Java built-in methods and how they can be used in our programs.
There are different Java Methods that you use.
String Methods | This is used for working with strings, which are sequences of characters. |
Math Methods | This is used for working math operations |
Array Methods | This is used for working with arrays |
So now let’s start from Java String Methods.
Java String Methods
Java provides different built-in methods for working with strings, which are sequences of characters. these are some commonly used string methods:
- length()
length() method returns the length of string. this is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { String myString = "Geekscoders.com"; int length = myString.length(); System.out.println(length); } } |
This will be the result
- charAt()
charAt() method returns the character at specific index in a string. if you run this code it will return G.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { String myString = "Geekscoders.com"; char myChar = myString.charAt(0); System.out.println(myChar); } } |
- indexOf()
indexOf() method returns the index of the first occurrence of specified character or substring in a string, it will return 12.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { String myString = "Geekscoders.com"; int index = myString.indexOf("com"); System.out.println(index); } } |
- indexOf()
indexOf() method returns the index of the first occurrence of specified character or substring in string.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { String myString = "Geekscoders.com"; String subString = myString.substring(7, 12); System.out.println(subString); } } |
This will be the result
Java Math Methods
Java also provides different builtin methods for performing mathematical operations. these are some commonly used math methods:
- abs()
abs() method returns the absolute value of a number like this.
1 2 3 4 5 6 7 8 9 10 11 |
public class Example { public static void main(String[] args) { int absValue = Math.abs(-6); System.out.println(absValue); } } |
This ill be the result
- ceil()
ceil() method rounds a number up to the nearest integer like this, it will return 6.0
1 2 3 4 5 6 7 8 9 10 11 |
public class Example { public static void main(String[] args) { double roundedValue = Math.ceil(5.2); System.out.println(roundedValue); } } |
- floor()
floor() method rounds a number down to the nearest integer like this, it will return 4.0
1 2 3 4 5 6 7 8 9 10 11 |
public class Example { public static void main(String[] args) { double roundedValue = Math.floor(4.8); System.out.println(roundedValue); } } |
- pow()
pow() method raises a number to specified power, you can use it like this.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Example { public static void main(String[] args) { double result = Math.pow(4, 2); System.out.println(result); } } |
This will be the result
Java Array Methods
Arrays are fundamental data structure in Java, and Java provides different builtin methods for working with arrays. these are some commonly used array methods:
- length
length property of an array returns the number of elements in the array, it will return 5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Example { public static void main(String[] args) { int[] myArray = {1, 2, 3, 4, 5}; int length = myArray.length; System.out.println(length); } } |
- sort()
sort() method sorts the elements of an array in ascending order.
1 2 |
int[] myArray = {5, 3, 2, 4, 1}; Arrays.sort(myArray); |
- binarySearch()
binarySearch() method searches for a specified element in an array using binary search algorithm, you can use it like this.
1 2 |
int[] myArray = {1, 2, 3, 4, 5}; int index = Arrays.binarySearch(myArray, 4); |