In the previous sessions, you created some functions for specific tasks. You have also been using some inbuilt functions throughout this course. So, let’s learn how to use some of the internal libraries in Java, that contain functions that could make your life easy.
Note: You can download the codes used in the video from below :
In this lecture, you saw some of the commonly used internal functions. Internal functions can really help you save time. You should use them as often as you can.
Hi. You created your own functions in the previous lectures, but Java also contains inbuilt libraries and functions that you can use in the future to ease your efforts. Let's begin with the function you've probably seen and implemented multiple times already system out print. This is actually a Java function that prints whatever value is passed into it. You also use the function String Length, which return the length of a string. Unlike what it seems, there are several lines of code executed before they give the result. The print statement is an inbuilt function and so is length. You already used multiple such functions without going into the detail of how they are executed. Let's learn how to use some of the internal libraries that contain functions that could make your life easy. You use the basic mathematical operators such as addition, subtraction, multiplication and division. You also used a statement Math Square root to find the square root of a number. There are other functions in the Math library as well. Let's look at the Math internal library.
As you can see, this is the documentation provided by Oracle for a Math library. This is an inbuilt library and you can find the entire documentation of it on the Oracle documentation site. So let's look at the absolute function in our Math library. The absolute function helps us find the absolute value of a number without its positive or negative sign. Next, we will look at the max function which helps us in finding the greater value between the two numbers provided. Over here, I want to find out the absolute value of minus nine. And I can do this by using the Math Library and the apps function provided by it. We then print out the absolute value. Next, let's say I want to find out the maximum value between 89 and 64. I would simply call the max function provided by the Math library that we just saw. Let me run this code.
As you can see, the absolute value of minus nine is nine and this is what we see here. And the maximum value between 89 and 64 is 89, as you can see here. Let's now look at the documentation for the string library. There are multiple functions here. If you look at it, we have an equals method that helps us in comparing two strings if they are equal or not. Similarly, we have equals ignore case, which helps us in ignoring the cases when comparing two strings. Next, we have your length function that helps us find the length of the string. You also have your concat function that helps you concatenate two strings. That is, it helps you add two different strings into one string. Let's use some of these in our code over here, I have declared a string one variable which holds hero in it. Next, I want to find out the length of the string. So I'm simply going to use the string length function provided by my string library. I'm going to declare two compare variables. In the first compare variable, I'm going to check if string one is equal to Hero. Any idea what the result of this would be? It should return as false since we are comparing a hero that has small H with a string hero that has capital H. So let's say you want to ignore the case difference between hero which has capital H and hero that has knowledge. You can simply use dot equals ignore case and this should return you true. Next, you have string two that could be your first name, string three, your second name and let's say I want my string four to be a concatenation of first name and second name so I just call the Concate function and write string three string two dot Concate string three would just combine first name and second name. Let me run this code so you have a better idea.
As you can see, the first system, the first print statement, throws us false because we are comparing capital edge hero with small letter H hero. Next, the compare two returns true value because we have explicitly ignored the cases in here. Third, we simply combine string two and string three that is first name and second name as you can see in the last statement.
Java has inbuilt libraries and functions that can be used to simplify coding efforts
Examples of inbuilt functions include System.out.print and String Length
There are also internal libraries like Math and String with multiple functions
Math library includes functions like absolute and max, which can be used to find the absolute value of a number and the greater value between two numbers, respectively
String library includes functions like equals, equals ignore case, length, and concat, which can be used to compare strings, find the length of a string, and concatenate strings
Examples of how to use these functions are demonstrated in the video
Java documentation site provides detailed information about these inbuilt libraries and functions.
There is an internal library called arrays, which can perform multiple operations on arrays.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
Find the function that can sort an array, and use it to sort the array given in the question.
(Hint: Use the internal library sort() to sort a given array. For e.g., sort(long[] a) sorts the specified array in ascending numerical order.)