Statistics
353
Views
60
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-07-16
Support
Share

No description

Tags
No tags
Language: 中文
File Format: PDF
File Size: 8.2 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Quick Java “We’ll be doing this next project in Java.” Unfortunately, you’re a C++ programmer, or maybe a Python programmer. How are you going to get up to speed in a hurry? There are lots of Java books for beginners, telling you all about what a computer is and how it represents everything in bits. You don’t need that. At the other extreme, there are thousand-page tomes that you aren’t going to get through in a few days, if ever. You need something in-between. This book is intended to fill that gap. It’s written for the programmer who doesn’t need to be taught how to program, just how to do it in Java—and who needs to get started in a hurry. Java is covered from the inside out. First, all the things that go inside a class, most of which are practically identical to C++. After that, all the various and complicated kinds of classes and interfaces and how they relate to each other in large-scale programs. Testing is essential, so (unlike most Java books) JUnit is covered in detail. Then, in case you need to go in that direction, some functional programming, a little about parallel programming, and more than enough to get you started in building GUIs (graphical user interfaces) and doing animation. There’s a lot in this little book and, despite my best efforts, you won’t learn Java in a weekend. But it should be a good start.
FEATURES • Circular approach allows very fast entry into Java • Full description of JUnit testing • Summary of functional programming in Java • Introduction to synchronization and parallel processing • Extensive description of building GUIs
Quick Java David Matuszek
Designed cover image: David Matuszek First edition published 2024 by CRC Press 2385 NW Executive Center Drive, Suite 320, Boca Raton FL 33431 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2024 David Matuszek Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright. com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.co.uk Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging-in-Publication Data Names: Matuszek, David L., author. Title: Quick Java / David Matuszek. Description: First edition. | Boca Raton : CRC Press, 2024. | Series: Quick programming | Includes bibliographical references and index. Identifiers: LCCN 2023011142 (print) | LCCN 2023011143 (ebook) | ISBN 9781032502779 (paperback) | ISBN 9781032515830 (hardback) | ISBN 9781003402947 (ebook) Subjects: LCSH: Java (Computer program language) Classification: LCC QA76.73.J38 M35257 2024 (print) | LCC QA76.73.J38 (ebook) | DDC 005.13/3‐‐dc23/eng/20230609 LC record available at https://lccn.loc.gov/2023011142 LC ebook record available at https://lccn.loc.gov/2023011143 ISBN: 978-1-032-51583-0 (hbk) ISBN: 978-1-032-50277-9 (pbk) ISBN: 978-1-003-40294-7 (ebk) DOI: 10.1201/9781003402947 Typeset in Minion by MPS Limited, Dehradun
To all my students past, present, and future
(This page has no text content)
Contents Author, xv Preface, xvii Versions, xix Chapter 1 ■ A Lightning Tour of Java 1 1.1 Projects 1 1.2 First Glimpse: The Circle Class 2 1.3 Data 4 1.4 Operators 5 1.5 Program Structure 6 1.6 Statements 8 1.7 Program Execution 9 1.8 Hello World 10 Chapter 2 ■ Preliminaries 13 2.1 IDEs 13 2.2 Comments and Tags 14 Chapter 3 ■ The “Inner Language” of Java 17 3.1 Variables and Naming Conventions 17 3.2 Basic Data Types 19 3.2.1 Primitive Types 19 3.2.2 Arrays 19 vii
3.2.3 Strings 21 3.2.4 Exceptions 22 3.2.5 Operators and Precedence 23 3.2.6 Declarations and Casting 25 3.2.7 Constants 26 3.2.8 Methods 27 3.2.9 Methods Calling Methods 29 3.2.10 Overloading 30 3.2.11 Scope 30 3.2.11.1 Variables Declared in Classes 30 3.2.11.2 Variables Declared in Methods 31 3.2.11.3 Variables Declared in Blocks 32 3.3 Statement Types 32 3.3.1 Statements Also in C++ 34 3.3.1.1 Blocks 34 3.3.1.2 Assignment Statements 35 3.3.1.3 Method Calls and Varargs 36 3.3.1.4 If Statements 36 3.3.1.5 While Loops 38 3.3.1.6 Do-while Loops 39 3.3.1.7 Traditional For Loops 40 3.3.1.8 For-each Loop 41 3.3.1.9 Classic switch Statements 42 3.3.1.10 Labeled Statements 44 3.3.1.11 Break Statements 44 3.3.1.12 Continue Statements 45 3.3.1.13 Return Statements 46 3.3.1.14 Empty Statements 46 3.3.2 Statements Not in C++ 47 3.3.2.1 Assert Statements 47 3.3.2.2 Print “Statements” 48 viii ▪ Contents
3.3.2.3 Switch Statements and Expressions 49 3.3.2.4 Pattern Matching in switch Statements 50 3.3.2.5 Try-catch-finally 52 3.3.2.6 Throw Statements 53 3.3.3 Reading from a File 54 3.3.4 Try With Resources 55 3.3.5 Writing to a File 56 3.4 Classes and Objects 57 3.4.1 Some Useful Objects 58 3.4.1.1 String Objects 58 3.4.1.2 StringBuilder Objects 59 3.4.1.3 Using Scanner 60 3.4.1.4 Console 61 3.4.1.5 Objects, Generics, and Stacks 62 3.4.1.6 Maps 63 3.4.1.7 The Java API 64 3.5 Objects and Classes 66 Chapter 4 ■ The “Outer Language” of Java 69 4.1 Class Structure 69 4.1.1 A Simple Class 70 4.1.2 The Class Header 71 4.1.3 Interfaces I 72 4.1.4 Fields 73 4.1.5 Constructors I 74 4.1.6 Defining Methods 75 4.1.7 Example: Bank Account 77 4.1.8 References 78 4.1.9 Constructors II 80 4.1.10 Static 82 4.1.11 Escaping Static 84 Contents ▪ ix
4.1.12 The Main Method 84 4.1.13 A More Complete Example 86 4.2 Inheritance 88 4.3 Casting Objects 89 4.4 Overriding 90 4.4.1 Overriding toString 92 4.4.2 Overriding equals 93 4.4.3 Overriding hashCode 94 Chapter 5 ■ Advanced Java 97 5.1 Information Hiding 97 5.1.1 Reasons for Privacy 98 5.1.2 Getters and Setters 98 5.1.3 Private Constructors 99 5.2 The Inner Language 100 5.2.1 General 100 5.2.1.1 Ordering 100 5.2.1.2 Javadoc 101 5.2.1.3 Var Declarations 104 5.2.1.4 Namespaces 104 5.2.2 Data 105 5.2.2.1 Wrapper Classes 105 5.2.2.2 Integers 105 5.2.2.3 Doubles 107 5.2.2.4 Characters and Unicode 107 5.2.2.5 Booleans 109 5.2.2.6 Other Primitives 109 5.2.2.7 Arrays 110 5.2.2.8 Strings 111 5.2.2.9 Multiline Strings 112 5.2.2.10 Formatter 113 5.2.2.11 Regular Expressions 116 x ▪ Contents
5.2.3 Collections 117 5.2.3.1 Iterators 119 5.2.4 Additional Operators 120 5.2.4.1 instanceof 120 5.2.4.2 The Ternary Operator 120 5.2.4.3 Bit and Shift Operators 121 5.2.4.4 Increment and Decrement Operators 121 5.3 The Outer Language 122 5.3.1 Generic Classes 122 5.3.2 Interfaces II 124 5.3.3 Abstract Classes 126 5.3.4 Final and Sealed Classes 128 5.3.5 Inner Classes 129 5.3.5.1 Member Classes 129 5.3.5.2 Static Member Classes 130 5.3.5.3 Local Inner Classes 131 5.3.5.4 Anonymous Inner Classes 132 5.3.6 Enums 133 5.3.7 Records 135 5.3.8 Serialization 136 5.3.9 Modules 137 5.3.10 Build Tools 138 Chapter 6 ■ Functional Programming 141 6.1 Function Literals 141 6.2 Functional Interfaces 142 6.3 Implicit Functional Interfaces 144 6.4 Persistent Data Structures 144 Chapter 7 ■ Unit Testing 147 7.1 Philosophy 147 7.2 What to Test 148 Contents ▪ xi
7.3 JUnit 149 7.4 JUnit 5 Assertions 150 7.5 Testing Exceptions 152 7.6 Assumptions 153 7.7 Simple Test Example 153 Chapter 8 ■ GUIs and Dialogs 157 8.1 A Brief History 157 8.2 Dialogs 157 8.2.1 Message Dialog 158 8.2.2 Confirm Dialog 158 8.2.3 Input Dialog 158 8.2.4 Option Dialog 159 8.2.5 Color Chooser Dialog 160 8.2.6 Load File Dialog 160 8.2.7 Save File Dialog 161 8.2.8 Custom Dialog 162 Chapter 9 ■ How to Build a GUI Program 163 9.1 Event-Driven Programs 163 9.2 The Event Dispatch Thread 164 9.3 Import the Necessary Packages 164 9.4 Make a Container 165 9.5 Add a layout manager 166 9.6 Create components 168 9.7 Add listeners 169 9.8 Sample code 170 9.8.1 JFrame and JPanel 170 9.8.2 JEditorPane 171 9.8.3 JScrollPane 172 9.8.4 JTabbedPane 173 9.8.5 JButton 174 xii ▪ Contents
9.8.6 JTextField 175 9.8.7 JTextArea 175 9.8.8 JCheckBox 177 9.8.9 JRadioButton 177 9.8.10 JLabel 179 9.8.11 JComboBox 180 9.8.12 JSlider 180 9.8.13 JSpinner 181 9.8.14 JProgressBar 182 9.8.15 Menus 183 9.8.16 Keyboard Input 183 9.8.17 Mouse Input 185 9.9 DiceRoller 186 Chapter 10 ■ Threads and Animation 189 10.1 Threads 189 10.2 Synchronization 191 10.3 Timers 192 10.4 Property Changes 192 10.5 Swingworker 193 10.6 The Bouncing Ball 197 10.6.1 MVC 198 10.6.2 Controller 198 10.6.3 Model 199 10.6.4 View 200 Appendix A ■ Code for BouncingBall 201 Bouncing Ball: Controller 201 Bouncing Ball: Model 204 Bouncing Ball: View 206 Index, 209 Contents ▪ xiii
(This page has no text content)
Author I ’M DAVID MATUSZEK, known to most of my students as “Dr. Dave.” I wrote my first program on punched cards in 1963 and immediately got hooked. I taught my first computer classes in 1970 as a graduate student in computer science at the University of Texas in Austin. I eventually got my PhD from there, and I’ve been teaching ever since. Admittedly, I spent over a dozen years in industry, but even then I taught as an adjunct at Villanova University. I finally escaped from industry and joined the Villanova faculty full-time for a few years, then I moved to the University of Pennsylvania, where I directed a Master’s in Computer and Information Technology (MCIT) program for students entering computer science from another discipline. Throughout my career, my main interests have been in artificial intelligence (AI) and programming languages. I’ve used a lot of pro- gramming languages. I retired in 2017, but I can’t stop teaching, so I’m writing a series of “quick start” books on programming and programming languages. I’ve also written three science fiction novels—Ice Jockey, All True Value, and A Prophet in Paradise—and I expect to write more. Check them out! And, hey, if you’re a former student of mine, drop me a note. I’d love to hear from you at david.matuszek@gmail.com. xv
(This page has no text content)
Preface I F YOU ARE AN EXPERIENCED PROGRAMMER, this book is your guide to getting up to speed in Java in a hurry. Not just the language, but also the basics of unit testing, graphical user interfaces, threads, animation, and functional programming. If you are coming from one of the C languages, you will find most of the statement types familiar. These are clearly designated, so you can just skim over that material. If you are coming from C++, you will find the object-oriented concepts are very similar, but the terminology is different. If you are coming from another language, such as Python, there is very little you can skip. Sorry. Let’s get started! xvii
(This page has no text content)
Versions T HIS BOOK IS ABOUT Java 8 and Java 17. Why those two? Java 8 is the last version that is free for commercial use. If you are programming for Android, Java 8 (also known as version 1.8) is the last version you can use. For these reasons, additions to Java after version 8 will be noted as such. As this book is being written, the current version is 17. A new version is released every six months, so when you buy this book, the latest version number is probably higher. This should not be a concern, because Java “never throws anything away.” Newer versions do not invalidate older versions, and the language changes gradually. In fact, Java 17 is an LTS (Long Term Support) version, along with versions 8 and 11. If you are uncertain which version of Java you have, you can execute this from the command line: java -version or execute the following println statement from within a running Java program: System.out.println(System.getProperty("java.version")); Since Java 9, you can also run jshell from the command line. This program lets you execute Java statements (such as the above print statement) one at a time. This is no way to write a program, but it’s handy for trying things out. xix
The above is a preview of the first 20 pages. Register to read the complete e-book.