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 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 books 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 i n 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 Getting startedWelcome 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 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 Goslings 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.
Next page