Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Java For Dummies Quick Reference
Java For Dummies Quick Reference
Java For Dummies Quick Reference
Ebook405 pages3 hours

Java For Dummies Quick Reference

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

A reference that answers your questions as you move through your coding

The demand for Android programming and web apps continues to grow at an unprecedented pace and Java is the preferred language for both. Java For Dummies Quick Reference keeps you moving through your coding while you solve a problem, look up a command or syntax, or search for a programming tip. Whether you're a Java newbie or a seasoned user, this fast reference offers you quick access to solutions without requiring that you wade through pages of tutorial material.

  • Leverages the true reference format that is organized with quick answers and solutions so you can read less and do more
  • Offers new elements such as a syntax guide, command guide, special generics and annotation section, and programming tips
  • Boasts a new, compact trim size that easily goes where you go for convenient referencing

Java For Dummies Quick Reference helps you move quickly and efficiently through Java without missing a beat!

LanguageEnglish
PublisherWiley
Release dateMay 1, 2012
ISBN9781118239742
Java For Dummies Quick Reference

Read more from Doug Lowe

Related to Java For Dummies Quick Reference

Related ebooks

Programming For You

View More

Related articles

Reviews for Java For Dummies Quick Reference

Rating: 4.1 out of 5 stars
4/5

10 ratings1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 4 out of 5 stars
    4/5
    god

Book preview

Java For Dummies Quick Reference - Doug Lowe

Introduction

Welcome to Java For Dummies Quick Reference — the one Java book that’s designed to sit on your desk for handy reference when you need to look up one of those nasty little details that everyone all-too-often tends to forget, such as the name of that one method of the it’sonthetipofmytongue class or the exact syntax of the whatchamacallit command.

This book contains handy, fast reference information for the most commonly used Java statements and language features, including the syntax for basic statements and all the arcane details of object-oriented programming (abstract classes, inheritance, inner classes, generics, and so on).

You also find fast reference information for the most commonly used application programming interface (API) classes, including collection classes, stream and file I/O classes, regular expressions, and Swing classes.

About Java For Dummies Quick Reference

Java For Dummies Quick Reference is intended to be a handy reference for all the details you most often need to recall when you’re writing Java programs. Of course, you can consult the online documentation. But that will have you scanning through 37 bazillion methods of the class you’re interested in just to find the one that you need, in spite of the fact that nearly all those 37 bazillion methods are rarely, if ever, used.

In this book, you get information on the tools you use most often, all packaged in one handy place. And all the information is current for the newest release of Java, known as JDK 7.

Foolish Assumptions

The only real assumption this book makes is that you know how to program in the Java language. This book is not a Java tutorial: For that, I recommend my book, Java All-in-One For Dummies, 3rd Edition.

You don’t need to be an expert Java programmer to benefit from this book. But if you’ve never written a Java class — if you don’t know the difference between an int data type and a for statement, you should get an introductory book first. The purpose of this book is to help you remember those elusive details that so easily slip out of the frontal lobes of even the most experienced Java programmer.

Conventions Used in This Book

Conventions is a fancy way of saying that information presented to you in this book is done so consistently. When you see a term italicized, for example, look for its definition, which is included so that you know what things mean. Sometimes, step-by-step instructions included in this book direct you to enter specific text onscreen. In this case, the text you need to type appears in bold. Most importantly, Java commands and syntax are in monofont so that they stand out from regular text.

How This Book Is Organized

Java For Dummies Quick Reference is divided into five parts. You don’t have to read these parts sequentially, and you don’t even have to read all the sections in any particular part. You can use the Table of Contents and the index to find the information you need and quickly get your answer. In this section, I briefly describe what you find in each part.

Part 1: Java Language Tools

This part shows you how to use Java command-line tools to compile and run Java programs.

Part 2: Java Language Basics

This part provides a complete reference for the elements of the Java programming language, including its data types, keywords, statements, operators, and so on. If you have trouble remembering the syntax of the switch statement or the difference between float and double, turn to this part.

Part 3: Basic Java Classes

In this part, you find reference information for many of the most commonly used Java classes. You find information about collection classes, such as the ArrayList and LinkedList, the Math class, and the Exception class. And you find reference information about working with regular expressions.

Part 4: File and Network I/O

This part presents reference information for file and network I/O classes. You find a wealth of information about classes that work with streams, classes that work with files and directories, and classes that work with IP addresses and network sockets.

Part 5: Swing

In this part, you find information useful when using Swing to create applications that use a graphical user interface. Documented here are the most common classes for creating frames, panels, and controls, adorning them with gizmos such as borders and scroll bars, and handling events generated as users interact your application.

Icons Used in This Book

Like any For Dummies book, this book is chock-full of helpful icons that draw your attention to items of particular importance. You find the following icons throughout this book:

tip.eps Pay special attention to this icon; it lets you know that some particularly useful tidbit is at hand.

TechnicalStuff.eps Hold it — overly technical stuff is just around the corner. Obviously, because this is a programming book, almost every paragraph of the next 250 or so pages could get this icon. So I reserve it for those paragraphs that go into greater depth, such as explaining how something works under the covers — probably deeper than you really need to know to use a feature, but often enlightening.

You also sometimes find this icon when I want to illustrate a point with an example that uses some Java feature that hasn’t been covered so far in the book, but that is covered later. In those cases, the icon is just a reminder that you shouldn’t get bogged down in the details of the illustration, and instead focus on the larger point.

Warning.eps Danger, Will Robinson! This icon highlights information that may help you avert disaster.

Remember.eps Did I tell you about the memory course I took?

CrossRef.eps This handy icon refers you to some other place in the book where you find additional information on the presented subject.

Where to Go from Here

This book isn’t designed to be read in any particular order. So you don’t have a real place to start, or to finish. When you can’t remember how to do something in Java, pick up this book, flip to the appropriate part, and look up the topic. Or, consult the index.

Also, be sure to check out the companion website for this book at www.dummies.com/go/javafdqr. There, you’ll find all the code samples shown throughout the book. You can easily cut and paste these fragments into your own programs as you see fit.

Enjoy!

Please note that some special symbols used in this eBook may not display properly on all eReader devices. If you have trouble determining any symbol, please call Wiley Product Technical Support at 800-762-2974. Outside of the United States, please call 317-572-3993. You can also contact Wiley Product Technical Support at www.wiley.com/techsupport.

Part 1

Java Language Tools

This beginning, ground-level part presents reference information for setting up the Java development environment and for compiling and running Java programs. This includes downloading and installing the Java Development Kit (JDK), understanding the Java folder structure, setting path variables for Java, and using Java commands.

In this part . . .

check.png Downloading and installing the JDK

check.png The JDK folder structure

check.png Setting path variables

check.png Using Java command line tools

Downloading and Installing the Java Development Kit

The Java Development Kit (JDK) can be downloaded from the following web address:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

The Java download tab includes links to download the JDK or the Java Runtime Environment (JRE). Follow the JDK link because clicking the JRE link gets you only the JRE, not the complete JDK.

The JDK download comes in two versions: an online version that requires an active Internet connection to install the JDK, and an offline version that lets you download the JDK installation file to your computer and install it later.

tip.eps I recommend using the offline version. That way, you can reinstall the JDK if you need to without having to download it again.

The exact size of the offline version depends on the platform, but most versions are between 50MB and 60MB, so that means that your download will take a few hours if you don’t have a high-speed Internet connection. With a broadband cable, DSL, or T1 connection, though, the download takes less than five minutes.

After you download the JDK file, install it by running its executable file. The procedure varies slightly depending on your operating system, but basically, you just run the JDK installation program file after you download it, as follows:

check.png On a Windows system, open the folder in which you saved the installation program and double-click the installation program’s icon.

check.png On a Linux or Solaris system, use console commands to change to the directory to which you downloaded the file and then run the program.

After you start the installation program, it prompts you for any information that it needs to install the JDK properly, such as which features you want to install and what folder you want to install the JDK in. You can safely choose the default answer for each option.

Note: If you’re an Apple Mac user, you don’t have to download and install the Java JDK. Apple pre-installs the JDK on Mac computers.

JDK Folder Structure

The JDK setup program creates several folders on your hard drive. The locations of these folders vary depending on your system, but in all versions of Windows, the JDK root folder is in the path Program Files\Java on your boot drive. The name of the JDK root folder also varies, depending on the Java version you’ve installed. For version 1.7, the root folder is jdk1.7.0.

Here are the subfolders created in the JDK root folder. As you work with Java, you’ll refer to these folders frequently.

In addition to these folders, the JDK installs several files in the JDK root folder.

TechnicalStuff.eps RTF (rich text format) is a document format that most word-processing programs can understand.

Setting the Path for Java

After you install the JDK, you need to configure your OS so that it can find the JDK command line tools. Start by setting the Path environment variable — a list of folders that the OS uses to locate executable programs. To do this on a Windows XP, Windows Vista, or Windows 7 system, follow these steps:

1. Click the Windows (Start) button and then right-click Computer (Windows 7 or Vista) or My Computer (XP).

This brings up the System Properties page.

2. Click the Advanced tab (XP) or the Advanced System Settings link (Vista and 7); then click the Environment Variables button.

The Environment Variables dialog box appears.

3. In the System Variables list, scroll to the Path variable, select it, and then click the Edit button.

A dialog box pops up to let you edit the value of the Path variable.

4. Add the JDK bin folder to the beginning of the Path value.

tip.eps Use a semicolon to separate the bin folder from the rest of the information that may already be in the path.

Note: The name of the bin folder may vary on your system, as in this example:

c:\Program Files\Java\jdk1.7.0\bin;other directories...

5. Click OK three times to exit.

The first OK gets you back to the Environment Variables dialog box; the second OK gets you back to the System Properties dialog box; and the third OK closes the System Properties dialog box.

java Command

The java command runs a Java program from a command prompt. The basic syntax is

java filename [options]

When you run the java command, the JRE is loaded along with the class you specify. Then, the main method of that class is executed.

Here’s an example that runs a program named HelloApp:

C:\java\samples>java HelloApp

tip.eps The bold indicates the part you type.

Remember.eps The class must be contained in a file with the same name as the class, and its filename must have the extension .class. You typically don’t have to worry about the name of the class file because it’s created automatically when you compile the program with the javac command.

The Java runtime command lets you specify options that can influence its behavior.

javac Command

The javac command compiles a program from a command prompt. It reads a Java source program from a text file and creates a compiled Java class file. The basic form of the javac command is

javac filename [options]

For example, to compile a program named HelloWorld.java, use this command:

javac HelloWorld.java

Normally, the javac command compiles only the file that you specify on the command line, but you can coax javac into compiling more than one file at a time by using any of the following techniques:

check.png If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java file in the same folder, the Java compiler automatically compiles that class, too.

check.png You can list more than one filename in the javac command. The following command compiles three files:

javac TestProgram1.java TestProgram2.java TestProgram3.java

check.png You can use a wildcard to compile all the files in a folder, like this:

javac *.java

check.png If you need to compile a lot of files at the same time but don’t want to use a wildcard (perhaps you want to compile a large number of files but not all the files in a folder), you can create an argument file, which lists the files to compile. In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. Here’s an argument file named TestPrograms that lists three files to compile:

TestProgram1.java

TestProgram2.java

TestProgram3.java

You can compile all the programs in this file by using an @ character, followed by the name of the argument file on the javac command line, like this:

javac @TestPrograms

The javac command has a gaggle of options that you can use to influence how it compiles your programs.

tip.eps A class file is a compiled Java program that can be executed by the java command. The Java compiler reads source files and creates class files.

tip.eps A deprecated API is a feature that is considered obsolete.

To use one or more of these options, type the option before or after the source filename. Either of the following commands, for example, compiles the HelloApp.java file with the -verbose and -deprecation options enabled:

javac HelloWorld.java –verbose –deprecation

javac –verbose –deprecation HelloWorld.java

javap Command

The javap command is called the Java disassembler because it takes apart class files and tells you what’s inside them. You won’t use this command often, but using it to find out how a particular Java statement works is fun, sometimes. You can also use it

Enjoying the preview?
Page 1 of 1