From the course: Oracle Java Foundations

Java APIs

(upbeat chiming) - [Joe] Java APIs. Java comes with a number of different application program interfaces to allow you to do useful things in the programming language without having to write the code yourself. First up, we see here an array. An array is a simple group of elements that are indexed and is one of the easiest data structures to work with. Each element in the array has a different index, index of 0, 1, 2, 3, and we can easily access and manipulate the array. But for more complex, flexible capabilities, there is a Collection API that is provided that allows us to work with different types of collections: array lists, linked lists, sets, stacks, queues, and we can do things like add, update, remove, search, and rearrange. So the Collection API is very useful for allowing us to manipulate collections of objects. The Java Streams APIs allow us to efficiently filter, map and reduce large streams of data. They perform these actions upon data using Lambda expressions. And Lambda expressions are a form of functional programming in Java. Here we see, for example, that we create a new ArrayList of employees and we're not showing all the code, but there would be code here that would actually populate the list. So we have a list of employees and then we can say to that list, return a stream of objects and do this in parallel, multi-threaded, automatically, filtering the list. So for each employee whose salary is greater than 1000, and allow that employee to be calculated. If you have an employee whose salary is not greater than 1,000, then that employee would be filtered out. Then for each of the employees whose salary is greater than 1000, we're going to calculate a bonus for that employee. So we calculate the bonus for each employee, and this can be done in parallel. So Java Streams APIs are a very powerful way of manipulating, working with large datasets. Java also comes with input/output APIs. This allows you to transfer data through a series of interconnected streams. These are different streams than we saw in the previous slide. We can read information from various sources. Typically, a flat file, but doesn't have to be, and that would be the input direction. And we can write information to various destinations. That's the output direction. Here we see an example where we have path information for a file in the file system and we can retrieve that information and manipulate it as an object. So if we want to work with that file, copy it, delete it, do other thing, open it, things like that, we can do so. In the second line of code, we see a file here called readme, which is being read using UTF-8 character set. And for each line of the file that's being read, they're just simply printing out the line. So they're reading the line from the file and printing it out to standard I/O. But with this, you can see that their capabilities exist in Java to work with file systems and work with files and other data sources. There's also a Java Concurrency API. They'll take advantage of multicore CPU architectures. It'll execute code concurrently to achieve better performance and user experience. It'll create multiple different tasks that all can run basically at the same time. Now, of course, that's going to be affected by how many CPUs you actually have and other workloads in the computer. But basically, from day one, when Java was first created, the ability to create multi-threaded code has been there. So here we see an example where we're going to create a callable object of type BigDecimal to do a calculation with, called taxCalculation. And we have some code in there that we do some tax calculations. And then to use this, we would go to our ExecutorService, which will execute the actual threads. It'll create a newCachedThreadPool with all the different threads. It'll then go ahead and we submit this, submit the tax calculation. It'll actually cause the thread pool to be executed and each of the different results are being executed and then returned at the end. Our last API that we're going to look at here is the Java Database Connectivity protocol, JDBC that allows us to connect to vendor databases and execute SQL statements. The Java Persistence API, JPA, has code that allows Java objects to be written to relational databases and relational SQL data to be read from those databases, queried from those databases, and then create Java objects as a result. This is called object relational mapping as we move Java data into a database and database data into Java objects. So this all starts with the application logic, which would make a call to or invoke the Java Persistence API. For example, we need to read information about a dog, let's say. So we would have a dog object here, and the dog object would know how to read and write itself to the database. The application logic would go to JPA and say, Hey, we need to connect to the database and we need to read and write information about a dog. So JPA would do this by using a provider-neutral API. So regardless of what type of database we're working with, Oracle, Db2, SQL Server, all of them work the same way from the Java perspective. The provider-neutral API allows us to connect to the database, create a statement object to execute SQL statements, execute the SQL statement, and get back the returned result set of all the rows that come back when the query is done. Now, in order to make this work, there's also a provider-specific driver that is used. So the provider-neutral API is used by Java persistence architecture, and then the provider JDBC driver's used at runtime to actually connect to the database. So for example, Oracle creates a JDBC driver, as does IBM for Db2, as does Microsoft for SQL Server. And most relational databases, if not all relational databases out there, have drivers for JDBC and Java. So we see here that the application logic asks JPA to read or write to the database. It uses the provider-neutral API to connect, execute a statement, and get a result set or write to the database. The provider JDBC driver connects to the database using a native database protocol, and then the database responds to the SQL statements that are implemented. There are other APIs as well, and we cover those in the Java learning subscription. (logo swooshes)

Contents