- Java 9 Programming By Example
- Peter Verhas
- 355字
- 2021-07-02 23:37:36
Optimizing the Sort - Making Code Professional
In this chapter, we will develop the sorting code and make it more general. We want to sort not only an array of Strings. Essentially, we will write a program that can sort anything that is sortable. That way, we will bring the coding to its full extent toward one of the major strengths of Java: abstraction.
Abstraction, however, does not come without a price tag. When you have a class that sorts strings and you accidentally mix an integer or something else, which is not a string, into the sortable data, then the compiler will complain about it: Java does not allow you to put an int into a String array. When the code is more abstract, such programming errors may slip in. We will look at how to handle such exceptional cases catching and throwing Exceptions.
To identify the bugs, we will use unit testing, applying the industry standard JUnit version 4. As JUnit heavily uses annotation, and because annotations are important, you will learn about it a bit.
After that, we will modify the code to use the generics feature of Java that was introduced into the language in version 5. Using that possibility, we will catch the coding error during compilation time, which is better than during run time. The earlier a bug is identified, the cheaper it is to fix.
For the build, we will still use Maven, but this time, we will split the code into small modules. Thus, we will have a multi-module project. We will have separate modules for the definition of a sorting module and for the different implementations. That way, we will look at how classes can extend each other and implement interfaces, and, generally, we will really start to program in the object-oriented way.
We will also discuss Test Driven Development (TDD), and at the end of the section, we will start using the brand new feature of Java 9: module support.
In this chapter, we will cover the following topics:
- Object-oriented programming principles
- Unit testing practices
- Algorithmic complexity and quick sort
- Exception handling
- Recursive methods
- Module support
- The Android Game Developer's Handbook
- MATLAB圖像處理超級學習手冊
- PHP基礎案例教程
- 名師講壇:Java微服務架構實戰(zhàn)(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Python面向對象編程:構建游戲和GUI
- 飛槳PaddlePaddle深度學習實戰(zhàn)
- Node Cookbook(Second Edition)
- SQL Server 2016 從入門到實戰(zhàn)(視頻教學版)
- 愛上C語言:C KISS
- PHP Microservices
- 軟件再工程:優(yōu)化現(xiàn)有軟件系統(tǒng)的方法與最佳實踐
- OpenStack Sahara Essentials
- 編程真好玩:從零開始學網(wǎng)頁設計及3D編程
- Fast Data Processing with Spark 2(Third Edition)
- 零基礎學Python(升級版)