● Text body (PDF format 239 pages: 1.82MB) 2019/12/15 update (→ DL from mirror site) → Sample program in the text --------------------- (2019/11/16) I teach programming at school. I made an introductory Java textbook to teach beginners. For the time being, the content is until it becomes possible to "program with Java for the time being". A little less than 50 pages at the beginning explain basic grammar and basic I / O. The rest is an explanation of frequently used libraries. The GUI explains Swing and SWT. This text is still in the prototype stage, but it will be revised and updated by those who use it in the field of education. We will publish it for free, so we would be grateful if you could give us your opinion. (Please let us know if you find any mistakes) Those who want to use this are welcome. I would like to improve the degree of perfection while receiving opinions. Especially, I hope it will be used by teachers who teach programming at school. We would appreciate it if you could give us any requests or suggestions from the field. (Reflect in the text as much as possible)
--------------------- [Supplement](2019/12 / 13-15) The explanation of bit operation has been enhanced. Added a commentary on the final modifier. Added a basic explanation about Javadoc. Other minor additions. --------------------- [Supplement](2019/11 / 24-12 / 01) The explanation about the ternary operator (...? ...: ...) for condition judgment was missing, so I added it. Added a commentary on how to divide CSV. --------------------- [Supplement](2019/11 / 21-22) Added the contents related to regular expressions.
1 Preface --- 1 1.1 Basic procedure for program development --- 1 1.2 Specific work of program development --- 1 1.2.1 Specification of character code in description of source program and input / output --- 2 1.2.2 How to use the jar format library --- 3 2 Java language --- 4 2.1 Points to keep in mind when learning for the first time --- 4 2.2 Basics regarding variables and values --- 4 2.2.1 Value expression --- 5 2.2.2 Arithmetic operation --- 5 2.2.3 Description of multiple sentences --- 6 2.2.4 Comment description --- 6 2.2.5 Example of variable type and operation --- 6 2.2.6 Methods and arguments --- 7 2.2.7 Handling of characters and character strings --- 7 2.2.7.1 Extract the character string part --- 7 2.2.7.2 Escape sequence --- 8 2.2.8 Value type conversion --- 8 2.2.8.1 Conversion of expressions between numbers and character strings --- 9 2.2.9 Cardinal conversion --- 10 2.2.9.1 Decimal number → n-ary number --- 10 2.2.9.2 N-ary number → decimal number --- 10 2.2.10 Bit operation --- 10 2.2.10.1 Shift operation --- 11 2.2.10.2 Bit-by-bit logical operation --- 11 2.3 Control structure --- 12 2.3.1 Conditional branch (1): if statement --- 12 2.3.1.1 Description of conditional expression and boolean type variable --- 13 2.3.1.2 Comparison of character strings --- 14 2.3.1.3 Ternary operator for condition judgment --- 14 2.3.2 Conditional branch (2): switch statement --- 15 2.3.3 Iteration (1): while statement --- 16 2.3.3.1 Change of variable value by recursive assignment --- 16 2.3.4 Iteration (2): for statement --- 18 2.4 Array --- 19 2.4.1 Allocation of memory to arrays --- 20 2.4.2 Initialization at the time of array declaration --- 20 2.4.3 Alignment of array elements --- 21 2.4.4 Access to elements by extended for statement --- 22 2.5 Object-oriented programming --- 23 2.5.1 Conceptual introduction --- 23 2.5.1.1 Concept of parts in object-oriented --- 23 2.5.1.2 Outline of object-oriented programming --- 23 2.5.2 Explanation according to the sample program --- 23 2.5.2.1 Concept of "class, instance, constructor" --- 26 2.5.2.2 About static members / methods --- 28 2.5.2.3 static initializer --- 28 2.5.3 Class extension (inheritance) --- 29 2.5.4 About abstract classes and interfaces --- 30 2.5.4.1 About anonymous class --- 33 2.5.5 Annotation --- 34 2.5.6 About generics --- 34 2.5.7 Information about the object class --- 35 2.6 Input / output --- 37 2.6.1 Standard input / output --- 37 2.6.1.1 Application of format editing to character string generation --- 38 2.6.1.2 Standard error output --- 39 2.6.2 Command argument --- 40 2.6.3 Output to file --- 41 2.6.3.1 Output to byte stream: FileOutputStream --- 41 2.6.3.2 Exception handling (1): try --- 42 2.6.3.3 Exception handling (2): throws --- 43 2.6.3.4 Output by PrintWriter --- 43 2.6.4 Input from file --- 44 2.6.4.1 Reading text line by line --- 44 2.6.4.2 Reading using the Scanner class --- 46 2.6.5 Binary file input / output --- 47 3 Standard class library --- 50 3.1 Data structure --- 50 3.1.1 List (list): ArrayList / LinkedList --- 50 3.1.1.1 Initialization at list generation --- 52 3.1.1.2 Alignment of elements: sort --- 52 3.1.1.3 Reversing the order of elements: reverse --- 52 3.1.1.4 Access to List elements by extended for statement --- 53 3.1.1.5 Alignment using Comparator --- 53 3.1.2 Set (set, data set): HashSet / TreeSet --- 56 3.1.2.1 Sequential access to elements by Iterator --- 58 3.1.3 Map (associative array, dictionary): HashMap / TreeMap --- 58 3.1.3.1 How to access the registered mappings in sequence --- 60 3.1.4 Data structure conversion --- 62 3.1.4.1 How to convert lists and sets to arrays --- 62 3.2 Editable character string class: StringBuilder, StringBuffer --- 63 3.2.1 Constructor --- 63 3.2.2 Method for editing character strings --- 63 3.2.3 Sample program --- 63 3.2.4 Conversion between byte array, String / StringBuilder / StringBuffer object --- 64 3.3 Math class --- 66 3.3.1 BigInteger,BigDecimal --- 66 3.3.1.1 Object generation and value set --- 67 3.3.1.2 Calculation --- 67 3.3.1.3 About MathContext --- 67 3.3.1.4 About multiple length operations of mathematical functions --- 68 3.4 Handling of date and time --- 70 3.4.1 Date, Calender class --- 70 3.4.2 Time measurement --- 72 3.4.3 LocalDateTime, LocalDate, LocalTime class --- 73 3.4.3.1 Basic functions --- 74 3.4.3.2 Handling of the Japanese calendar --- 77 3.5 File, path, URI --- 79 3.5.1 Path creation, path decomposition --- 79 3.5.2 URI --- 80 3.5.3 File class method --- 81 3.5.3.1 Creating files and directories --- 82 3.5.3.2 Acquisition of directory contents, file existence check --- 83 3.5.3.3 File name change (path change) --- 83 3.5.3.4 Deleting files and directories --- 84 4 GUI and 2D graphics --- 86 4.1 Basic concept of GUI programming --- 86 4.2 Outline of GUI application construction --- 86 4.2.1 GUI toolkit --- 87 4.3 Swing --- 88 4.3.1 GUI construction --- 88 4.3.1.1 GUI construction example --- 88 4.3.1.2 Event processing --- 91 4.3.1.3 Retrieving keys from keyboard events --- 94 4.3.1.4 Building an application as a JFrame class --- 94 4.3.1.5 Safer implementation --- 96 4.3.1.6 Layout manager --- 96 4.3.2 Overview of graphics creation with Swing --- 98 4.3.2.1 Graphics creation flow --- 98 4.3.2.2 Program example --- 99 4.3.2.3 Arguments of paintComponent --- 100 4.3.3 Coordinate system --- 100 4.3.4 Basic method for creating graphics --- 100 4.3.4.1 Designation of drawing color --- 100 4.3.4.2 Drawing a straight line: drawLine method --- 101 4.3.4.3 Drawing of polygonal line: drawPolyline method --- 101 4.3.4.4 Specifying line attributes --- 101 4.3.4.5 Rectangle drawing (1) Frame: drawRect method --- 104 4.3.4.6 Rectangle drawing (2) Fill: fillRect method --- 104 4.3.4.7 Drawing of an ellipse (1) Frame: drawOval method --- 104 4.3.4.8 Draw an ellipse (2) Fill: fillOval method --- 104 4.3.4.9 Draw polygon (1) Frame: drawPolygon method --- 104 4.3.4.10 Draw polygon (2) Fill: fillPolygon method --- 104 4.3.4.11 Drawing of characters --- 104 4.3.4.12 Designation of gradation color --- 106 4.3.5 Use of Buffered Image --- 107 4.3.5.1 Drawing for BufferedImage --- 107 4.3.5.2 How to output a BufferedImage object as image data --- 108 4.3.5.3 Reading and displaying image files --- 110 4.3.5.4 Operation for pixels --- 112 4.3.6 File selection dialog --- 114 4.4 SWT --- 117 4.4.1 GUI application construction flow --- 117 4.4.2 Generation and placement of GUI components --- 118 4.4.3 Event processing registration --- 120 4.4.4 Sample program --- 120 4.4.5 Retrieving keys from keyboard events --- 124 4.4.6 Overview of graphics creation with SWT --- 124 4.4.6.1 GC(Graphic Context) --- 124 4.4.6.2 Graphics creation flow --- 124 4.4.7 Coordinate system --- 125 4.4.8 Color designation --- 125 4.4.9 Basic method for drawing --- 126 4.4.9.1 Draw a straight line: drawLine method --- 126 4.4.9.2 Draw a polygonal line: drawPolyline method --- 126 4.4.9.3 Line attribute settings --- 126 4.4.9.4 Rectangle drawing (1) Frame: drawRectangle method --- 127 4.4.9.5 Rectangle drawing (2) Fill: fillRectangle method --- 127 4.4.9.6 Draw an ellipse (1) Frame: drawOval method --- 127 4.4.9.7 Ellipse drawing (2) Fill: fillOval method --- 127 4.4.9.8 Draw polygon (1) Frame: drawPolygon method --- 127 4.4.9.9 Polygon drawing (2) Fill: fillPolygon method --- 127 4.4.10 Character drawing --- 127 4.4.11 Pixel operation --- 128 4.4.11.1 Pixel coloring process --- 130 4.4.12 Image file input / output --- 130 4.4.12.1 Reading image files --- 130 4.4.12.2 Output to image file --- 130 4.4.13 Sample program --- 131 4.4.14 File selection dialog --- 134 5 Threads and processes --- 137 5.1 Multi-thread programming --- 137 5.1.1 Thread class --- 137 5.1.2 Runnable interface --- 139 5.1.3 Thread synchronization --- 140 5.1.3.1 How to wait for the end of another thread: join method --- 140 5.1.3.2 synchronized modifier --- 141 5.1.3.3 Thread wait / resume execution --- 144 5.1.4 Thread status --- 146 5.2 concurrent library --- 147 5.2.1 Executor and Executor Service --- 147 5.2.2 Callable interface and Future interface --- 149 5.3 Starting an external process --- 152 5.3.1 Relationship between Java program and external process --- 152 5.3.2 How to use --- 152 5.3.3 Sample program --- 153 6 Lambda expression, Stream API, method reference --- 156 6.1 Functional interface --- 156 6.2 Stream interface --- 157 6.2.1 Generation of Stream object --- 158 6.2.2 forEach method --- 158 6.2.3 filter method --- 158 6.2.4 map method --- 159 6.2.5 sorted method --- 159 6.2.5.1 Stream close (end) --- 159 6.2.6 collect method --- 159 6.3 Refer to the method --- 160 7 Regular expression --- 162 7.1 String search: find method --- 162 7.1.1 Pattern detection position --- 163 7.1.2 Continuation of pattern detection --- 163 7.2 Replacement processing: replaceFirst, replaceAll --- 164 7.3 Pattern description method --- 164 7.4 Pattern matching group --- 166 7.5 Splitting the character string: split method --- 167 8 TCP / IP communication --- 168 8.1 Concept of socket --- 168 8.2 Socket class --- 169 8.2.1 Server socket --- 169 8.2.2 Socket --- 169 8.3 Sending and receiving data --- 170 8.4 Sample program --- 171 9 Sound data playback --- 174 9.1 The simplest method --- 174 9.2 Playback of large-sized sound data --- 176 9.3 Practical sound playback --- 178 10 Library and package --- 181 10.1 Creating a library --- 181 10.1.1 Storage location of library file (class file) --- 181 10.1.2 Handling when different packages provide classes with the same name --- 182 10.1.3 Creating a jar file --- 182 11 Connection to database --- 183 11.1 Basic concept about database --- 183 11.1.1 Basic operations on the database --- 183 11.2 Software handled in this manual --- 184 11.2.1 SQLite --- 184 11.2.2 JDBC --- 184 11.3 Transaction --- 184 11.3.1 Connection to database --- 184 11.3.2 Acquisition of Statement object --- 185 11.3.3 Execution of SQL statement --- 185 11.3.3.1 Extraction of search results --- 185 11.3.4 End of transaction --- 185 11.4 SQL --- 185 11.4.1 Creating a database --- 186 11.4.2 Creating a table --- 186 11.4.2.1 Column data type --- 186 11.4.2.2 Sample program (1) --- 187 11.4.3 Joining tables --- 189 11.4.3.1 Sample program (2) --- 190 A Swing simple reference --- 192 A.1 Method common to many components --- 192 A.2 Various components --- 192 A.3 Class hierarchy of main GUI components --- 197 A.4 Typical event --- 198 B SWT simple reference --- 199 B.1 Method common to many controls --- 199 B.2 Various controls --- 199 B.3 Typical event --- 204 C java.nio library --- 205 C.1 Path interface and Paths class --- 205 C.2 Files class --- 205 D Third-party library --- 209 D.1 Apfloat --- 209 E sample program --- 211 E.1 Performance comparison of ArrayList and LinkedList --- 211
Recommended Posts