Read an Excerpt
Java eLearning Kit For Dummies
By John Paul Mueller
John Wiley & SonsCopyright © 2014 John Wiley & Sons, Ltd
All rights reserved.
Starting With Java
[check] Java's increasing popularity makes it the most common language used to develop cross-platform applications.
[check] Developers rely on the fact that Java is a standards-based language.
[check] 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.
[check] All Java applications share common components.
[check] Java applications rely on a special application called a compiler to change Java source code that the developer understands to an executable form that the JRE understands.
[check] Use comments to make it easier to remember what tasks your application performs.
[check] Java applications rely on the Java Application Programming Interface (API) to perform many tasks.
[check] Develop a better understanding of Java by viewing the help files.
1. What are the advantages of using a language that works across multiple platforms?
Cross-platform compatibility is a major advantage of using Java, as described on page 10
2. Where can I obtain a copy of the Java SDK?
You can find the installation and configuration instructions for the Java SDK starting on page 13
3. Do I absolutely have to use an Integrated Development Environment (IDE)?
Java makes it quite easy to use a simple text editor to create programs, but you'll do much better if you rely on an IDE as described on page 21
4. What are the main parts of a Java application?
You can find a description of the major parts on page 24
5. What purpose does the .class file serve?
A .class file contains the byte code executed by the JRE as described on page 29
6. Why is the JavaW command important?
Discover the JavaW difference on page 31
7. When should I comment my code?
There are distinct times when you should consider commenting your code, as explained on page 32
8. Where do I find an explanation of the Java API content?
The Java API contains all the standard classes used to create applications, and you can find out about it on page 35
9. Must I go online to get help?
Java provides both local and online help, as described on page 42
Java is an amazing language. Most programming languages are designed around a single operating system or environment. For example, when you work with the Microsoft .NET Framework languages, such as C# or Visual Basic, you're working with Windows unless you get a special third-party product to make these languages work elsewhere. Likewise, Macintosh developers commonly use Objective C to create their applications. Using PHP means that you're creating applications that appear in a browser, rather than applications that run within 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 your system, a version of the language is available for most platforms. One of the first tasks that you perform in this chapter is to install your own copy of Java.
Java receives strong support in the developer community. You can find all sorts of tools, add-on products, and free source code. In fact, the number of choices can become overwhelming. This chapter starts out slowly and makes you aware of choices you need to make based on how you plan to use Java to create applications.
Programs contain standard basic parts that this chapter shows you how to construct. You can add to program functionality by using standardized features found in the Java Application Programming Interface (or Java API), a collection of code samples that lets you write Java applications with greater ease by reducing the amount of code you need to write. Code makes your application do something useful, but in order to understand the code, you need to add comments as a means of documentation. Adding comments to your code makes it easier to read.
An essential fact to remember about Java is that you can get lots of help using this language — you're never left to your own devices with Java, because help is always available. You don't need to know any sort of special handshake or rely on certain people to discover how Java works. This chapter provides you with basic information about getting help with Java. You'll also find more information on getting help as the book progresses.
Considering Why You'd Use Java
Java, like all programming languages, has both positive and negative features. It's important to know why you're learning Java before you do anything else because having a good reason to do something is a motivator. The following sections describe some of the benefits of using Java as a programming language.
Using a popular language
Literally hundreds (perhaps thousands) of computer languages are available today. Some of these languages are definitely more popular than others. Precisely which language is most popular depends on who you ask. For example, many people trust the judgment of TIOBE — an organization that tracks programming language popularity (amongst other things) — when it comes to language popularity (http://www.tiobe.com/content/ paperinfo/tpci/index.html). The TIOBE information is a little stark. If you prefer some commentary with your statistics, try a site such as Udemy/ blog (https://www.udemy.com/blog/best-programming-language). Just about everyone agrees that Java is a popular language (and many organizations view Java as the most popular language).
If you're learning a first programming language and you want to ensure you have the best chance of success of getting a job based on your skills using the language, Java is a great choice. Java's popularity also makes it a good choice because you have better access to a wide range of third-party tools, libraries, and add-ons. Using a popular language also means that your chances of finding someone to help you with a problem are significantly better. In fact, popularity makes Java an excellent choice as a language even when learning a third or fourth language, just because it's used in so many places.
However, popularity doesn't necessarily make Java the right language for you to use to satisfy a particular application requirement — functionality makes Java the right language to use. The reason you encounter so many languages is that people often find something lacking in an existing language and try to create a better language that addresses the failure. Java is great for satisfying business and user-oriented needs, but I wouldn't use it to create a device driver for an operating system because Java lacks low-level support. This book can't answer the question of whether Java will precisely match your needs, but it will give you the knowledge required to answer that question on your own. Always consider language features when choosing a language for a particular application need. If you still need help 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 task on every other platform. Of course, each platform must run the same version of the JRE to ensure compatibility. You knew there was going to be a little gotcha, didn't you? When you think about it, this is a small gotcha and a reasonable requirement.
Creating applications for multiple platforms
At one time, writing an application for Windows running on a PC answered the needs of most businesses. Organizations simply didn't require many platforms to complete required tasks. Most organizations today, however, have to support a number of platforms. People want to access the same data using their PC, tablet, and smartphone, for example. An application has to be versatile enough to work well on all three pieces of hardware (as a minimum). Because tablet and smartphone operating systems vary considerably, an application must also work well across a number of operating systems and present the user with essentially the same interface to reduce support costs. Java makes it possible to support all these requirements.
Defining other reasons to use Java
Java does have a lot to offer, and you should at least consider the following reasons to adopt it:
[check] Java provides a large, standardized library of functions that make it possible to write robust applications without relying on any third-party products.
[check] The people who created Java are constantly trying to simplify its syntax (language structure) to make it easier to learn.
[check] Relying on Object-Oriented Programming (OOP) techniques makes Java more robust and less susceptible to problems than other languages because OOP technologies have a great track record in that area. (See the discussion at http://www.freetopessays.com/content/ paper-outlining-various-concepts-found-object-orientedprogramming-oop for details.)
[check] All memory-related tasks are handled for you, so all you have to worry about is writing your code.
[check] The huge help facility means that you won't be left out in the cold trying to figure something out.
[check] Strong community support means that you can ask someone what the documentation means when you can't figure it out.
[check] Great performance on a wide variety of platforms.
Don't worry about understanding all these reasons now. Some of these reasons are emphasized later in the book, and some of them won't really matter unless you work with other languages. File these reasons away for now and think about them later as you work through this book and begin building applications of your own.
Obtaining and 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 Edition (EE) Development Kit 8, the examples will work just fine, but you really don't require such a fancy version of the product to use this book. With that in mind, the following sections describe how to download and install the Java SE Development Kit 8.
Downloading Java 8
To create a Java application, you must have the Java Development Kit (JDK). If you want others to run your application, but these others don't need to write application code, then they must have the Java Runtime Environment (JRE). The version of the JRE must match the version of your JDK to ensure that the application will run as expected. You can download a copy of the JDK for your operating system from https://jdk8.java.net. The JDK currently supports the following:
[check] Windows 32-bit and 64-bit
[check] Macintosh OS X 64-bit
[check] Linux 32-bit and 64-bit
[check] Linux for the Acorn RISC Machine (ARM) 32-bit processor (ARM processors are typically used in tablets and smartphones.)
[check] Solaris 32-bit and 64-bit (You have to install the 32-bit version first and then the 64-bit version.)
[check] Solaris SPARC 32-bit and 64-bit (You have to install the 32-bit version first and then the 64-bit version.)
Simply follow the link for the version of Java 8 that you require. When you want to obtain a copy of the JRE for your users, you can get it from https://jdk8.java.net by clicking the JRE link rather than the JDK link. (You must use the JDK when working with a device equipped with an ARM processor.) The JDK also comes with a copy of the JRE as a subdirectory of the installation.
Performing the Windows installation
Files needed: The Java installation program you downloaded
The Windows version of Java comes as an executable file that installs much like any other executable file on your system. After you download the version of the JDK for your system, use the following steps to install it.
1. Double-click the downloaded file.
You may see a User Account Control dialog box appear. This dialog box asks whether you want to allow the application to make changes to your system. Click Yes to allow system changes. If you click No, the application exits, and Java doesn't install.
You see the Welcome page of the setup wizard.
2. Click Next.
You see the Custom Setup page of the wizard, as shown in Figure 1-1. The three items you must install — Development Tools, Source Code, and Public JRE — are already selected in the figure.
3. (Optional) Click Change to select a location to store the Java files.
Normally, it's best to use the default installation location unless you have a special reason for storing the files somewhere else. Using the default location makes it easier for tools, such as an IDE, to find the files later.
4. Click Next.
The setup wizard performs the installation.
After the JDK installation is complete, you see a secondary setup dialog box appear for the JRE installation.
5. (Optional) Click Change to select a location for the JRE installation.
In most cases, using the default installation location works fine.
6. Click Next.
The JRE installation starts. After the JRE installation is complete, the secondary setup dialog box disappears, and you return to the initial setup dialog box.
7. Click Close.
The setup is complete, and the setup wizard disappears.
Performing the Linux installation
Files needed: The Java installation program you downloaded
The precise steps you use to install Java on Linux depend on your Linux distribution. Installing Java by using the downloadable files means acting as the root user. Some versions of Linux (such as Fedora, Red Hat, and openSuSE) enable the root user, but others (such as Ubuntu and Debian) don't. No matter which version of Linux you own, you generally have access to the super-user do (sudo) command. With this in mind, the easiest way to get Java on Linux is to execute the following command:
sudo apt-get install oracle-java8-installer
Performing the Mac installation
Files needed: The Java installation program you downloaded
After you download the version of the JDK for your system, extract the file on your hard drive, preferably to the /System/Library/Java/java-8 folder. To use Java from an IDE, all you need to do is tell the IDE where to find the files. However, if you want to use Java for running applications normally, you must create a path to it by using the instructions found in the "Creating a path on the Mac" section, later in this chapter.
Accessing the Java executables
The location of the Java executable files on your system is important. The folder containing these files provides access to all the tools you require to use Java. When you install Java, the setup program tells you where these files will be stored. The following sections tell you the default location for storing the Java 8 files and provide some tips on accessing them.
Creating a path in Windows
If you're not using an IDE in Windows, you'll always have to create a path to the Java executable files. That's because Windows doesn't normally offer any Java support out of the box. The files you need will normally appear in one of two locations, unless you specifically store them somewhere else.
Excerpted from Java eLearning Kit For Dummies by John Paul Mueller. Copyright © 2014 John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.