Share E-Book

Java For Dummies (Barry Burd) (z-library.sk, 1lib.sk, z-lib.sk)

Author

Rating No ratings yet

Log in to rate

Java
Language English

Learn to code with Java and open the gate to a rewarding career Now in its 9th edition, Java For Dummies gives you the essential tools you need to understand the programming language that 17 million software developers rely on. This beginner-friendly guide simplifies every step of the learning process. You'll learn the basics of Java and jump into writing your own programs. Along the way, you'll gain the skills you need to reuse existing code, create new objects, troubleshoot when things go wrong, and build working programs from the ground up. Java For Dummies will help you become a Java developer, even if you're brand new to the world of coding. • Learn the basic syntax and building blocks of Java • Begin to write your own programs in the latest Java version • Test out your code and problem-solve any errors you find • Discover techniques for writing code faster This is the must-have Dummies resource for beginning programmers and students who need a step-by-step guide to getting started with Java. You'll also love this book if you're a seasoned programmer adding another language to your repertoire.

Format PDF
Size 17.5 MB
8
Views
(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.

Page 1
(This page has no text content)
Page 2
(This page has no text content)
Page 3
Java®
Page 4
(This page has no text content)
Page 5
Java® 9th Edition by Barry Burd, PhD
Page 6
Java® For Dummies®, 9th Edition Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. Media and software compilation copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. Published simultaneously in Canada. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/ go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. Java is a registered trademark of Oracle America, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.  NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT.  NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2024951544 ISBN: 978-1-394-28924-0 (pbk); 978-1-394-28925-7 (ebk); 978-1-394-28926-4 (ebk)
Page 7
Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part 1: Getting Started with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: All about Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 2: All about Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 CHAPTER 3: Using the Basic Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Part 2: Writing Your Own Java Programs . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 4: Making the Most of Variables and Their Values . . . . . . . . . . . . . . . . . . . . 59 CHAPTER 5: Controlling the Flow by Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . 97 CHAPTER 6: Controlling Program Flow with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Part 3: Working with the Big Picture: Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 CHAPTER 7: The Inside scOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 CHAPTER 8: Constructing New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 CHAPTER 9: Piles of Files: Dealing with Information Overload . . . . . . . . . . . . . . . . . . 229 CHAPTER 10: Saving Time and Money: Reusing Existing Code . . . . . . . . . . . . . . . . . . . 249 Part 4: Smart Java Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 CHAPTER 11: Putting Variables and Methods Where They Belong . . . . . . . . . . . . . . . 281 CHAPTER 12: Using Collections and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 CHAPTER 13: Using Arrays to Juggle Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 CHAPTER 14: Looking Good When Things Take Unexpected Turns . . . . . . . . . . . . . . 377 CHAPTER 15: Fancy Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 CHAPTER 16: Oooey GUI Was a Worm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Part 5: The Part of Tens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 CHAPTER 17: Ten Packs of Java Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 CHAPTER 18: Ten Bits of Advice for New Software Developers . . . . . . . . . . . . . . . . . . 449 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Page 8
(This page has no text content)
Page 9
Table of Contents vii Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 PART 1: GETTING STARTED WITH JAVA . . . . . . . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: All about Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 What You Can Do with Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Why You Should Use Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Gaining Perspective: Where Java Fits In . . . . . . . . . . . . . . . . . . . . . . . . . .10 Three Ways to Write Computer Programs . . . . . . . . . . . . . . . . . . . . . . . .12 A word you can use to impress people . . . . . . . . . . . . . . . . . . . . . . .13 Imperative programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 CHAPTER 2: All about Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Get Ready for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 The Inside Scoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 What is a compiler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 What is a Java virtual machine? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Developing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Spoiler Alert! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33 CHAPTER 3: Using the Basic Building Blocks . . . . . . . . . . . . . . . . . . . . . . . 35 Speaking the Java Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 The grammar and the common names . . . . . . . . . . . . . . . . . . . . . . .36 The words in a Java program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Checking Out Java Code for the First Time . . . . . . . . . . . . . . . . . . . . . . .39 Understanding a Simple Java Program . . . . . . . . . . . . . . . . . . . . . . . . . .41 The Java method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 The main method in a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 How you finally tell the computer to do something . . . . . . . . . . . . .44 Brace yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 And Now, a Few Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50 Adding comments to your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 What’s Barry’s excuse? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Using comments to experiment with your code . . . . . . . . . . . . . . . .54
Page 10
viii Java For Dummies PART 2: WRITING YOUR OWN JAVA PROGRAMS . . . . . . . . . . . 57 CHAPTER 4: Making the Most of Variables and Their Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Varying a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60 Assignment statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 The types of values that variables may have . . . . . . . . . . . . . . . . . . .63 How to hold the line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Numbers without decimal points . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Combining declarations and initializing variables . . . . . . . . . . . . . .69 Experimenting with JShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 The Atoms: Java’s Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 The char type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75 The boolean type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78 Creating New Values by Applying Operators . . . . . . . . . . . . . . . . . . . . .79 Initialize once, assign often . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 The increment and decrement operators . . . . . . . . . . . . . . . . . . . . .83 Assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 The Molecules and Compounds: Reference Types . . . . . . . . . . . . . . . .90 Java’s String type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Java’s Random type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Coding with classes, objects, and their methods . . . . . . . . . . . . . . .92 What did he just say? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 CHAPTER 5: Controlling the Flow by Making Decisions . . . . . . . . . . 97 Making Decisions (Java if Statements) . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Guess the number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 She controlled keystrokes from the keyboard . . . . . . . . . . . . . . . . .99 Reading double values: a cautionary tale . . . . . . . . . . . . . . . . . . . .100 The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Equal, equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Brace yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Your intent to indent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Elseless in Helsinki . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104 Forming Conditions with Comparisons and Logical Operators . . . . .107 Comparing numbers; comparing characters . . . . . . . . . . . . . . . . .107 Comparing objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 When one line isn’t enough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 Java’s logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112 Vive les nuls! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 (Conditions in parentheses) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 An import declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Warning! Explicit content! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
Page 11
Table of Contents ix The Nesting Habits of if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Choosing among Many Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Java’s glorious switch statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Express yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 Your grandparents’ switch statement . . . . . . . . . . . . . . . . . . . . . . .129 Free fall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 CHAPTER 6: Controlling Program Flow with Loops . . . . . . . . . . . . . . 135 Repeating Instructions Over and Over Again (Java while Statements) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 Count On Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 The anatomy of a for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 The world premiere of “Al’s All Wet” . . . . . . . . . . . . . . . . . . . . . . . . .142 You Can Always Get What You Want . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Who moved my file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Reading a single character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 On the var side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149 File handling in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Block on the while side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 PART 3: WORKING WITH THE BIG PICTURE: OBJECT-ORIENTED PROGRAMMING . . . . . . . . . . . . . . . . . . . . . . . . . 155 CHAPTER 7: The Inside scOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Defining a Class (What It Means to Be an Account) . . . . . . . . . . . . . . .158 Declaring variables and creating objects . . . . . . . . . . . . . . . . . . . . .160 Using an object’s fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 One program; several classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 The road not taken . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Defining a Method within a Class (Displaying an Account) . . . . . . . . .164 An account that displays itself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166 The display method’s header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Sending Values to and from Methods (Calculating Interest) . . . . . . . .171 Passing a value to a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Returning a value from the getInterest method . . . . . . . . . . . . . . .175 Giving Your Numbers a Makeover . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 Hide-and-Seek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 Good programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 Public lives and private dreams: Making a field inaccessible . . . .183 Enforcing rules with accessor methods . . . . . . . . . . . . . . . . . . . . . .185 Streamlining Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 Experiments with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 Putting a variable in its place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 Telling a variable where to go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
Page 12
x Java For Dummies Passing Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 Pass by value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 Returning a result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 Pass by reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199 Returning an object from a method . . . . . . . . . . . . . . . . . . . . . . . . .201 A final word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 CHAPTER 8: Constructing New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Defining Constructors (What It Means to Be a Temperature) . . . . . .206 What is a temperature? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 What is a temperature scale? (Java’s enum type) . . . . . . . . . . . . . .207 Okay, so then what is a temperature? . . . . . . . . . . . . . . . . . . . . . . .208 What you can do with a temperature . . . . . . . . . . . . . . . . . . . . . . . .211 What does a temperature look like? . . . . . . . . . . . . . . . . . . . . . . . . .214 Constructing a temperature; a slow-motion replay . . . . . . . . . . . .215 The default constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 The Easy Way to Create a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Constructors don’t have to be dull . . . . . . . . . . . . . . . . . . . . . . . . . .223 Equality for all! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 CHAPTER 9: Piles of Files: Dealing with Information Overload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 What It Means to Be an Employee . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Putting your class to good use . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 She reads keystrokes from the keyboard . . . . . . . . . . . . . . . . . . . .234 Working with Disk Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Cutting a check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241 Repeat after me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Reading from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 Where’s your file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 You can decide where to put your file . . . . . . . . . . . . . . . . . . . . . . .245 Reading a line at a time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245 Clean up after yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 CHAPTER 10: Saving Time and Money: Reusing Existing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250 Creating a subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 Creating subclasses is habit-forming . . . . . . . . . . . . . . . . . . . . . . . .257 Using Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258 Making types match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 The second half of the story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
Page 13
Table of Contents xi Changing the Payments for Only Some of the Employees . . . . . . . . .262 A Java annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Using methods from classes and subclasses . . . . . . . . . . . . . . . . .264 Yet Another switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 Doing Something about the Weather . . . . . . . . . . . . . . . . . . . . . . . . . . .270 Constructors for subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 Using all this stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273 PART 4: SMART JAVA TECHNIQUES . . . . . . . . . . . . . . . . . . . . . . . . . . 279 CHAPTER 11: Putting Variables and Methods Where They Belong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Making a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282 Package deal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282 Using the Player class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284 Running the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 One class; nine objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288 Some notes on formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289 It’s All About Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291 Access for Java modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 Access modifiers for classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 A word or two about method-local variables . . . . . . . . . . . . . . . . .294 Access for members (fields and methods) . . . . . . . . . . . . . . . . . . .295 Making Static (Finding the Team Average) . . . . . . . . . . . . . . . . . . . . . . .301 So much static! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Meet the compact constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Displaying the overall team average . . . . . . . . . . . . . . . . . . . . . . . .304 All about static import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306 All about static references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307 Why static members don’t always play well with others . . . . . . . .308 CHAPTER 12: Using Collections and Streams . . . . . . . . . . . . . . . . . . . . . . . 313 Using an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 A new way to do looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Import a module in one fell swoop . . . . . . . . . . . . . . . . . . . . . . . . . .319 Using generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Wrap it up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321 Are we done yet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323 Once and again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 Sorting and Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 Another Kind of Collection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326 So many collection classes! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328 Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Problem-solving the old-fashioned way . . . . . . . . . . . . . . . . . . . . . .330 A familiar problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
Page 14
xii Java For Dummies Lambda expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .332 The interpretation of streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334 Why bother? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340 Method references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 Some black sheep among the lambdas . . . . . . . . . . . . . . . . . . . . . .344 CHAPTER 13: Using Arrays to Juggle Values . . . . . . . . . . . . . . . . . . . . . . . . 349 Getting Your Ducks All in a Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349 How to book hotel guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351 Tab stops and other special things . . . . . . . . . . . . . . . . . . . . . . . . . .352 Make life easy for yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Array versus ArrayList: The Ultimate Showdown . . . . . . . . . . . . . . . . .356 Do You Have a Room? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357 Writing to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359 What else can go wrong? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360 Array Madness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 Return to Sanity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365 Using the Room class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 The conditional operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370 How to Argue with Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371 Settling the argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372 Checking for the right number of program arguments . . . . . . . . .374 CHAPTER 14: Looking Good When Things Take Unexpected Turns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Garbage In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378 Making things right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382 The parameter in a catch clause . . . . . . . . . . . . . . . . . . . . . . . . . . . .382 Do it yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385 Who will catch the exception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387 Catching two or more exceptions at a time . . . . . . . . . . . . . . . . . . .394 The Buck Stops Here, Except When It Doesn’t . . . . . . . . . . . . . . . . . . .395 Catch it soon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396 Catch it later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .398 Checked or unchecked? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .400 Try, Try Again! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .402 CHAPTER 15: Fancy Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 Java’s Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .407 The Java Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408 Declaring two interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409 Implementing interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .411 Putting the pieces together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413
Page 15
Table of Contents xiii Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417 Caring for your pet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420 Using all your classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422 Relax! You’re Not Seeing Double! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424 CHAPTER 16: Oooey GUI Was a Worm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Getting Started with GUI Applications . . . . . . . . . . . . . . . . . . . . . . . . . .428 Setting up your computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428 Creating a bare-bones JavaFX project . . . . . . . . . . . . . . . . . . . . . . . .431 Running your bare-bones JavaFX project . . . . . . . . . . . . . . . . . . . . .434 Adding Stuff to Your JavaFX Project . . . . . . . . . . . . . . . . . . . . . . . . . . . .436 Where’s my Scene Builder application? . . . . . . . . . . . . . . . . . . . . . .436 Building a scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437 Tweaking some code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .440 Running your new project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441 PART 5: THE PART OF TENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 CHAPTER 17: Ten Packs of Java Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 This Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445 For Business Issues Related to This Book . . . . . . . . . . . . . . . . . . . . . . .445 Download the Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . .446 Your Grandparents’ Java Download Site . . . . . . . . . . . . . . . . . . . . . . . .446 The Horse’s Mouth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446 Join Java User Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446 Find the Latest News about Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446 Find News, Reviews, and Sample Code . . . . . . . . . . . . . . . . . . . . . . . . .447 Got a Technical Question about Anything? . . . . . . . . . . . . . . . . . . . . . .447 Become Involved in the Future of Java . . . . . . . . . . . . . . . . . . . . . . . . . .447 CHAPTER 18: Ten Bits of Advice for New Software Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 How Long Does It Take to Learn Java? . . . . . . . . . . . . . . . . . . . . . . . . . .450 Which of Your Books Should I Read? . . . . . . . . . . . . . . . . . . . . . . . . . . .450 Are Books Other than Yours Good for Learning Java? . . . . . . . . . . . . .450 Which Computer Programming Language(s) Should I Learn? . . . . . .451 Which Skills Other than Computer Coding Should I Learn? . . . . . . . .451 How Should I Continue My Learning as a Software Developer? . . . . .452 How Else Should I Continue My Learning as a Developer? . . . . . . . . .452 How Can I Land a Job Developing Software? . . . . . . . . . . . . . . . . . . . . .453 I Still Don’t Know What to Do with My Life . . . . . . . . . . . . . . . . . . . . . . .453 If I Have Other Questions, How Can I Contact You? . . . . . . . . . . . . . . .454 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Page 16
(This page has no text content)
Page 17
Introduction 1 Introduction What’s all the fuss about Java? To help answer that question, I offer a few facts: » Nine million of the world’s developers work on applications in Java, and Java runs on approximately 7 billion devices.* » Ninety percent of all Fortune 500 companies use Java.** » According to the TIOBE Programming Community Index, Java is the world’s third most popular programming language.*** » In 2021, Glassdoor, Inc., ranked jobs based on earnings potential, job satisfac- tion, and number of available job openings. Among the company’s “50 Best Jobs in America for 2021,” a career as a Java developer ranked number one.**** Sounds good. Right? Please, read on. About This Book This book isn’t the usual dry techie guide. It’s written for normal human beings — people with little or no programming experience. In this book, I divide Java into manageable chunks. Each chunk is (more or less) a chapter on its own. I explain concepts in plain language using complete code examples that you can download and run. I keep each code example focused on a few key concepts. I resist the urge to use fancy tricks that impress professional programmers. I expand on concepts that may be difficult for newcomers. I add diagrams to help you visualize important ideas. I provide exercises with each chapter along with solutions to the exercises on the book’s website. * www.geeksforgeeks.org/reasons-to-learn-java ** https://softjourn.com/insights/is-java-still-used *** www.tiobe.com/tiobe-index **** www.glassdoor.com/blog/best-jobs-in-america-for-2021
Page 18
2 Java For Dummies Finally, and most importantly — and without question the most significant of all this book’s features — I throw in some jokes. I’ve written some good jokes and lots of bad jokes. (I should say “lots and lots” of bad jokes.) I’ve hidden Easter eggs in the text. I’ve added anecdotes about all kinds of topics. Some of the anecdotes are true, and many of them are . . . well, you figure it out. Foolish Assumptions In this book, I make a few assumptions about you, the reader. If one of these assumptions is incorrect, you’re probably okay. If all these assumptions are incorrect, please buy the book anyway: » I assume that you have access to a computer. Here’s the good news: You can run most of the code in this book on almost any computer. The only computers you can’t use to run this code are ancient boxes that are more than ten years old (give or take a few years). » I assume that you can navigate your computer’s common menus and dialog boxes. You don’t have to be a Windows, Linux, or Macintosh power user, but you should be able to start a program, find a file, put a file into a certain directory — that sort of thing. Most of the time, when you follow instructions in this book, you’re typing code on the keyboard, not pointing- and-clicking the mouse. » I assume that you can think logically. That’s all there is to programming in Java — thinking logically. If you can think logically, you have it made. If you don’t believe that you can think logically, read on. You may be pleasantly surprised. » I make few assumptions about your computer programming experience (or your lack of such experience). In writing this book, I’ve tried to do the impossible: Make the book interesting for experienced programmers yet accessible to people with little or no programming experience. So, I assume no particular programming background on your part. If you’ve never created a loop or indexed an array, that’s okay. On the other hand, if you’ve done these things (maybe in Visual Basic, Python, or C++), you’ll discover some interesting plot twists in Java. The developers of Java took the best ideas in object-oriented programming, streamlined them, reworked them, and reorganized them into a sleek, powerful way of thinking about problems. You’ll find many new, thought-provoking features in Java. As you find out about these features, many of them will seem quite natural to you. One way or another, you’ll feel good about using Java.
Page 19
Introduction 3 Icons Used in This Book If you could watch me write this book, you’d see me sitting at my computer, talk- ing to myself. I say each sentence in my head. Most of the sentences, I mutter several times. When I have an extra thought or a side comment that doesn’t belong in the regular stream, I twist my head a little bit. That way, whoever’s listening to me (usually, nobody) knows that I’m off on a momentary tangent. Of course, in print, you can’t see me twisting my head. I need some other way to set a side thought in a corner by itself. I do it with icons. When you see a Tip icon or a Remember icon, you know that I’m taking a quick detour. Here’s a list of icons that I use in this book: A tip is an extra piece of information — a helpful tidbit that the other books may forget to tell you. Everyone makes mistakes. Heaven knows that I’ve made a few in my time. Any- way, when I think people are especially prone to make a mistake, I mark it with a Warning icon. Sometimes I want to hire a skywriting airplane crew. “Barry,” says the white smoky cloud, “if you want to compare two numbers, use the double equal sign. Please don’t forget to do this.” Because I can’t afford skywriting, I have to settle for a more modest option: I create a paragraph marked with the Remember icon. “If you don’t remember what such-and-such means, see blah-blah-blah,” or “For more information, read blahbity-blah-blah.” Writing computer code is an activity, and the best way to learn an activity is to practice it. That’s why I’ve created things for you to try in order to reinforce your knowledge. Many of these are confidence-builders, and some are more challeng- ing. When you first start putting concepts into practice, you’ll discover all kinds of issues, quandaries, and roadblocks that didn’t occur to you when you started reading about the material. But that’s a good thing. Keep at it! Don’t become frus- trated. Or, if you do become frustrated, visit this book’s website (https:// javafordummies.allmycode.com) for hints and solutions. This icon calls attention to useful material that you can find online. Check it out!
Page 20
4 Java For Dummies Occasionally, I run across a technical tidbit. The tidbit may help you understand what the people behind the scenes (the people who developed Java) were thinking. You don’t have to read it, but you may find it useful. You may also find the tidbit helpful if you plan to read other (geekier) books about Java. Beyond the Book In addition to what you’re reading right now, this book comes with a free, access- anywhere Cheat Sheet containing code that you can copy and paste into your own Java program. To get this Cheat Sheet, simply go to www.dummies.com and type Java For Dummies Cheat Sheet in the Search box. For your extra reading pleasure, I’ve also arranged for my publishers to make available for download two appendices and a bonus chapter: The details are as follows: » Appendix A: Setting Up Your Computer, which gives you the skinny on how to prep your computer for your upcoming Java adventure » Appendix B: Who’s Afraid of Java Documentation?, a non-nonsense guide to Java’s API documentation » Bonus Chapter 1: Pat Your Head While You Rub Your Belly, a guide for those interested in multitasking with your Java code All three can be found at www.dummies.com/go/javafd9e. Where to Go from Here If you’ve gotten this far, you’re ready to start reading about Java application development. Think of me (the author) as your guide, your host, your personal assistant. I do everything I can to keep things interesting and, most importantly, to help you understand. If you like what you read, send me a note. My email address, which I created just for comments and questions about this book, is JavaForDummies@allmycode.com. (In case you’re wondering, I answer my own emails. I don’t use bots or paid assistants.) And don’t forget — for the latest updates, visit this book’s website. The site’s address is https://javafordummies.allmycode.com.
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List