Open and find: • Tips on choosing an IDE for Java • How to change primitive types into objects • Advice on formatting variable content • Different types of operators • What to do with conditional statements • Directions for setting up loops • How to create and use classes • Secrets of manipulating strings John Paul Mueller is a veteran technical editor and an expert on computer industry certifications. He has written books on C#, Java, Windows programming, and VBA, and is coauthor of the bestselling C ++ All-in-One For Dummies. $39.99 USA / $47.99 CAN / £27.99 UK Programming Languages / Java Learn this platform-independent language and create programs that will run anywhere Java is the primary language used for Android development and many web applications, so once you master it, you can create apps for all sorts of uses. With this eLearning Kit, you learn Java at your own pace using written, visual, and interactive lessons. Discover how to obtain and install Java and go from beginner to programmer extraordinaire! • Install the newest version — find and install the most up-to-date Java version for Windows®, Mac®, or Linux® • Explore variables — learn to use primitive variables to store information and object variables to perform tasks within your application • Get in the loop — repeat tasks with loops, create and use classes, access data sets, and work with strings • Banish bugs — detect and solve problems to prevent application crashes • XML makes it better — add value to your applications by working with XML data Includes Online Course See inside for details! Includes: • Full color book plus online training course • FREE 1 year access to online self-paced training via access code • 10 interactive lessons cover the essentials of Java John Paul Mueller eLEARNING KIT LEARNING KIT Java ® Java ® Cover Images: Front Cover Image ©iStockphoto.com/ olaser; Back and Inside Cover Images Courtesy of John Paul Mueller www.it-ebooks.info www.it-ebooks.info Java ® LEARNING KIT www.it-ebooks.info www.it-ebooks.info by John Paul Mueller Java ® LEARNING KIT www.it-ebooks.info Java ® eLearning Kit For Dummies ® Published by John Wiley & Sons, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2014 by John Wiley & Sons, Inc., Hoboken, New Jersey Published by John Wiley & Sons, Inc., Hoboken, New Jersey 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 either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. 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, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its afliates in the United States and other countries, and may not be used without written permission. Java is a regis- tered 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 www.wiley.com/techsupport. 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: ISBN 978-1-118-09878-3 (pbk); ISBN 978-1-118-22370-3 (ebk); 978-1-118-23706-9 (ebk); ISBN 978-1-118-26201-6 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 www.it-ebooks.info Contents at a Glance Introduction 1 Chapter 1: Starting With Java 7 Chapter 2: Using Primitive Variables 47 Chapter 3: Using Object Variables 75 Chapter 4: Formatting Variable Content 111 Chapter 5: Working with Operators 129 Chapter 6: Working with Conditional Statements 165 Chapter 7: Repeating Tasks Using Loops 185 Chapter 8: Handling Errors 209 Chapter 9: Creating and Using Classes 247 Chapter 10: Accessing Data Sets Using Arrays and Collections 285 Chapter 11: Performing Advanced String Manipulation 337 Chapter 12: Interacting with Files 359 Chapter 13: Manipulating XML Data 387 Index 403 www.it-ebooks.info www.it-ebooks.info Table of Contents Introduction 1 About This Kit 1 How This Book Is Organized 2 Conventions Used inThis Book 4 Foolish Assumptions 5 Icons Used inThis Kit 5 Accessing theJava eCourse 6 Class Is In 6 Chapter 1: Starting With Java 7 Considering Why You’d Use Java 10 Using a popular language 10 Working witha standardized language 11 Creating applications formultiple platforms 12 Dening other reasons touse Java 12 Obtaining and Installing theJava 8 JDK 13 Downloading Java 8 13 Performing theWindows installation 14 Performing theLinux installation 15 Performing theMac installation 16 Accessing theJava executables 16 Choosing anIDE forJava 21 Working witha text editor 21 Finding anIDE 22 Determining when touse a particular development environment 23 Creating a Simple Application 24 Writing theapplication code 24 Identifying theparts ofthe application 26 Compiling theSimple Application 27 Opening thecommand prompt 28 Creating thecompiled .class le 29 Executing theSimple Application 30 Using theJava command 30 Understanding theJavaW commanddifference 31 Adding Comments toYour Java Code 32 Grasping theimportance ofcomments 32 Creating single-line comments 33 Creating multiline comments 34 www.it-ebooks.info viii Java eLearning Kit For Dummies Importing Other Classes 34 Introducing theJava API 35 Importing single classes 36 Importing anentire package 37 Obtaining Help forJava 39 Getting local help 40 Getting local utility help 41 Locating Java information online 42 Summing Up 42 Try-it-yourself lab 44 Know this tech talk 44 Chapter 2: Using Primitive Variables 47 Working withthe Primitive Types 50 byte 51 short 55 int 57 long 59 oat 61 double 63 boolean 65 char 67 Creating and Using Constants 69 Summing Up 71 Try-it-yourself lab 72 Know this tech talk 72 Chapter 3: Using Object Variables 75 Working withCommon Objects 78 String 78 Calendar 80 BigDecimal 81 BigInteger 84 Changing Primitive Types intoObjects 86 Automatic boxing and unboxing 87 Byte versusbyte 87 Short versusshort 90 Integer versusint 92 Long versuslong 94 Float versusoat 97 Double versusdouble 100 Boolean versusboolean 101 Character versuschar 101 www.it-ebooks.info [...]... two, Java eLearning Kit For Dummies teaches you how to: ✓ Install a copy of Java on your particular platform (Windows, Linux, and Macintosh instructions are all provided.) ✓ Obtain an Integrated Development Environment (IDE) for your copy of Java, should you wish to use one ✓ Write a basic Java program ✓ Obtain help using Java to write applications for your machine ✓ Use variables to store information... the latest information The Go Online icon (see the “Icons Used in This Kit section) will also direct you to online resources that you’ll want to access Icons Used in This Kit The familiar and helpful For Dummies icons point you in the direction of really great information that’s sure to help you as you work your way through assignments Look for these icons throughout Java eLearning Kit For Dummies, in... With Java ✓ Java s increasing popularity makes it the most common language used to develop cross-platform applications ✓ Developers rely on the fact that Java is a standardsbased language ✓ Each platform that Java supports has its own version of the Java Software Development Kit (SDK) and Java Runtime Engine (JRE), yet uses precisely the same language ✓ All Java applications share common components ✓ Java. .. Your purchase of this For Dummies eLearning Kit includes access to the course online at the For Dummies eLearning Center If you have purchased an electronic version of this book, please visit http://www .dummies. com/go/ getelearningcode to gain your access code to the online course If you purchased the paperback book, you find your access code inside the front cover of this book Dummies eCourses require... provides essential information for getting Java installed, obtaining and installing an IDE, creating a simple first application, and obtaining help about Java features If you’re absolutely certain that you already know all of this information, then you can probably start with Chapter 2 If you feel like downloading the book’s sample Java code, head on over to http://www .dummies. com/go/javaelearning, where... lost in the electronic fray, worry no longer Java eLearning Kit For Dummies is here to help you, providing you with an integrated learning experience that includes not only the book you hold in your hands but also an online version of the course at http://learn .dummies. com Consider this introduction your primer About This Kit Each piece of this eLearning Kit works in conjunction with the others, although... figuring out whether Java is the right programming language for you, consider looking online at sites such as Java Pros and Cons (http://c2.com/cgi/wiki?JavaProsAndCons) Working with a standardized language For Java to work in all the environments that it does, it has to have a strong standardized language Standardization means that code designed to perform a specific task on one platform performs that same... Installing the Java 8 JDK The examples in this book rely on the Java Standard Edition (SE) Development Kit 8, which is the latest version of Java At least some of the examples will work with older versions of Java, but you’ll find that you get better results using Java 8 Oracle does create Java 8 in a number of other editions, but don’t worry about them for this book Yes, if you have Java Enterprise... ommand) and export a path to Java 8 by typing the following command: $export JAVA_ HOME=/System/Library /Java/ java-8 After you export the path, you need to verify that you have the right version of Java selected Test your installation by typing the following command in Terminal: GO ONLINE $java -version You see the version number of Java that you have selected Choosing an IDE for Java An IDE is simply a special... the host operating system Java crosses all these boundaries You can find Java running on just about every operating system, and Java applications can run both within the operating system environment and as part of a browser presentation It isn’t hard to find a copy of Java Some operating systems come with Java installed, or another application will install Java for you Even if Java isn’t installed on . Mueller www.it-ebooks.info www.it-ebooks.info Java ® LEARNING KIT www.it-ebooks.info www.it-ebooks.info by John Paul Mueller Java ® LEARNING KIT www.it-ebooks.info Java ® eLearning Kit For Dummies ® Published by John. useful information related to the discussion at hand. Accessing the Java eCourse Your purchase of this For Dummies eLearning Kit includes access to the course online at the For Dummies eLearning. 34 www.it-ebooks.info viii Java eLearning Kit For Dummies Importing Other Classes 34 Introducing the Java API 35 Importing single classes 36 Importing anentire package 37 Obtaining Help for Java 39 Getting