ebook img

Java in easy steps: Covers Java 9 PDF

253 Pages·2017·7.8 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java in easy steps: Covers Java 9

In easy steps is an imprint of In Easy Steps Limited 16 Hamilton Terrace · Holly Walk · Leamington Spa Warwickshire · CV32 4LY www.ineasysteps.com Sixth Edition Copyright © 2017 by In Easy Steps Limited. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission from the publisher. Notice of Liability Every effort has been made to ensure that this book contains accurate and current information. However, In Easy Steps Limited and the author shall not be liable for any loss or damage suffered by readers as a result of any information contained herein. Trademarks All trademarks are acknowledged as belonging to their respective companies. Contents 1 Getting started Introduction Installing the JDK Writing a first Java program Compiling & running programs Creating a variable Recognizing data types Creating constants Adding comments Troubleshooting problems Summary 2 Performing operations Doing arithmetic Assigning values Comparing values Assessing logic Examining conditions Setting precedence Escaping literals Working with bits Summary 3 Making statements Branching with if Branching alternatives Switching branches Looping for Looping while true Doing do-while loops Breaking out of loops Returning control Summary 4 Directing values Casting type values Creating variable arrays Passing an argument Passing multiple arguments Looping through elements Changing element values Adding array dimensions Catching exceptions Summary 5 Manipulating data Exploring Java classes Doing mathematics Rounding numbers Generating random numbers Managing strings Comparing strings Searching strings Manipulating characters Summary 6 Creating classes Forming multiple methods Understanding program scope Forming multiple classes Extending an existing class Creating an object class Producing an object instance Encapsulating properties Constructing object values Summary 7 Importing functions Handling files Reading console input Reading files Writing files Sorting array elements Making array lists Managing dates Formatting numbers Summary 8 Building interfaces Creating a window Adding push buttons Adding labels Adding text fields Adding item selectors Adding radio buttons Arranging components Changing appearance Summary 9 Recognizing events Listening for events Generating events Handling button events Handling item events Reacting to keyboard events Responding to mouse events Announcing messages Requesting input Summary 10 Deploying programs Producing an application Distributing programs Building Java archives Deploying applications Creating Android projects Exploring project files Adding resources & controls Inserting Java code Testing the application Deploying Android apps Summary Preface The creation of this book has provided me, Mike McGrath, a welcome opportunity to update my previous books on Java programming with the latest techniques. All examples I have given in this book demonstrate Java features supported by current compilers on both Windows and Linux operating systems, and the book’s screenshots illustrate the actual results produced by compiling and executing the listed code, or by implementing code snippets in the Java shell. Conventions in this book In order to clarify the code listed in the steps given in each example, I have adopted certain colorization conventions. Components of the Java language itself are colored blue; programmer-specified names are red; numeric and string values are black; and comments are green, like this: // Store then output a text string value. String message = “Welcome to Java programming!” ; System.out.println( message ) ; Additionally, in order to identify each source code file described in the steps, a colored icon and file name appears in the margin alongside the steps, like these: App.java App.class App.jar App.xml Grabbing the source code For convenience, I have placed source code files from the examples featured in this book into a single ZIP archive. You can obtain the complete archive by following these easy steps: Browse to www.ineasysteps.com then navigate to Free Resources and choose the Downloads section Find Java in easy steps, 6th Edition in the list, then click on the hyperlink entitled All Code Examples to download the archive Now, extract the archive contents to any convenient location on your computer I sincerely hope you enjoy discovering the programming possibilities of Java and have as much fun with it as I did in writing this book. Mike McGrath 1 Getting started Welcome to the exciting world of Java programming. This chapter shows how to create and execute simple Java programs, and demonstrates how to store data within programs. Introduction Installing the JDK Writing a first Java program Compiling & running programs Creating a variable Recognizing data types Creating constants Adding comments Troubleshooting problems Summary Introduction The Java™ programming language was first developed in 1990 by an engineer at Sun Microsystems named James Gosling. He was unhappy using the C++ programming language so he created a new language that he named “Oak”, after the oak tree that he could see from his office window. As the popularity of the World Wide Web grew, Sun recognized that Gosling’s language could be developed for the internet. Consequently, Sun renamed the language “Java” (simply because that name sounded cool) and made it freely available in 1995. Developers around the world quickly adopted this exciting new language and, because of its modular design, were able to create new features that could be added to the core language. The most endearing additional features were retained in subsequent releases of Java as it developed into the comprehensive version of today. The essence of Java is a library of files called “classes”, which each contain small pieces of ready-made proven code. Any of these classes can be incorporated into a new program, like bricks in a wall, so that only a relatively small amount of new code ever needs to be written to complete the program. This saves the programmer a vast amount of time, and largely explains the huge popularity of Java programming. Additionally, this modular arrangement makes it easier to identify any errors than in a single large program. Java technology is both a programming language and a platform. In Java programming, the source code is first written as human-readable plain text files ending with the .java extension. These are compiled into machine-readable .class files by the javac compiler. The java interpreter can then execute the program with an instance of the Java Virtual Machine (Java VM): The New icon pictured above indicates a new or enhanced feature introduced with the latest version of Java. As the Java VM is available on many different operating systems, the same .class files are capable of running on Windows, Linux and Mac operating systems – so Java programmers theoretically enjoy the cross-platform ability to “write once, run anywhere”. In order to create Java programs, the Java class libraries and the javac compiler need to be installed on your computer. In order to run Java programs, the Java™ Runtime Environment (JRE) needs to be installed to supply the java interpreter. All of these components are contained in a freely available package called the Java™ Platform, Standard Edition Development Kit (JDK). The Java programs in this book use version JDK 9, which incorporates both the Development Kit itself and the Runtime Environment, and can be downloaded from the Oracle® website at www.oracle.com/technetwork/java/javase/downloads The Oracle download page also features other packages, but only the JDK 9 package is required to get started with Java programming. The JDK 9 package is available in versions for 32-bit and 64-bit variants of the Linux, Mac, Solaris and Windows platforms – accept the Oracle License Agreement, then select the appropriate version for your computer to download the Java Development Kit. There is no truth in the rumor that JAVA stands for “Just Another Vague Acronym”. Installing the JDK Select the appropriate Java Development Kit (JDK) package for your system from the Oracle® downloads page, and then follow these steps to install Java on your computer: Uninstall any previous versions of the JDK and/or Java Runtime Environment (JRE) from your system Start the installation and accept the License Agreement When the “Custom Setup” dialog appears, either accept the suggested installation location or click the Change button to choose your preferred location – such as C:\Java for Windows systems or usrJava for Linux systems Ensure that the Public JRE and Development Tools features are selected from the list. Optionally, you may deselect the other features as they are not required to start programming with this book Click the Next button to install all the necessary Java class libraries and tools at the chosen location

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.