About this book

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction Java Programming
About This Book
History

The Java Programming Wikibook is a shared effort in amassing a comprehensive guide of the complete Java platform - from programming advice and tutorials for the desktop computer to programming on mobile phones. The information presented in this book has been conceptualised with the combined efforts of various authors and contributors, and anonymous editors.

The primary purpose of this book is to teach the Java programming language to an audience of beginners, but its progressive layout of tutorials increasing in complexity, it can be just as helpful for intermediate and experienced programmers. Thus, this book is meant to be used as:

  • a collection of tutorials building upon one another in a progressive manner;
  • a guidebook for efficient programming with the Java programming language; and,
  • a comprehensive manual resource for the advanced programmer.

This book is intended to be used in conjunction with various other online resources, such as:

[edit] Who should read this book?

Every thing you would need to know to write computer programs would be explained in this book. By the time you finish reading, you will find yourself proficient enough to tackle just about anything in Java and programs written using it. This book serves as the first few stepping stones of many you would need to cross the unfriendly waters of computer programming. We have put a lot of emphasis in structuring this book in a way that lets you start programming from scratch, with Java as your preferred language of choice. This book is designed for you if any one of the following is true.

  • You are relatively new to programming and have heard how easy it is to learn Java.
  • You had some BASIC or Pascal in school, and have a grasp of basic programming and logic.
  • You already know and have been introduced to programming in earlier versions of Java.
  • You are an experienced developer and know how to program in other languages like C++, Visual Basic, Python or Ruby.
  • You've heard that Java is great for web applications and web services programming.

Although, this book is generally meant to be for programmers who are beginning to learn programming; it can be highly beneficial for intermediate and advanced programmers who may have missed up on some vital information. By the end of this book, you would be able to solve any complicated problem and tackle it using the best of your learnt skills in Java. Once you finish, you are also encouraged to take upon ambitious programming projects of your own as you certainly would be able to do that as well.

This book assumes that the reader has no prior knowledge of programming in Java, or for that matter, any object-oriented programming language. The book makes it easier to understand development methodology as one reads through the book with practical examples and exercises at the end of each topic and module. Although, if you are a complete beginner, we would suggest that you move slowly through this book and practice each exercise at your pace.

[edit] How can you participate

Content is constantly being updated and enhanced in this book as is the nature of wiki-based content. This book is therefore in a constant state of evolution. Any Wikibooks users can participate in helping this book to a better standard as both a reader, or an author/contributor.

[edit] As a reader

If you are interested in reading the content present in this book, we encourage you to:

  • share comments about the technical accuracy, content, or organisation of this book by telling the authors in the Discussion section for each page. You can find the link Discussion on each page in this book leading you to appropriate sections for discussion.
  • leave a signature when providing feedback, writing comments, or giving suggestion on the Discussion pages. This can be achieved by appending -- ~~~~ to your messages. Do not add your signatures to the Book pages, they are only meant for the Discussion pages.
  • review pages – at the bottom of every page of this book, you are likely to find controls that help you review and give feedback for pages. Content on each page should be rated according to the four Wikibooks quality metrics: Reliability, Completeness, Neutrality and Presentation.
  • share news about the Java Programming Wikibook with your family and friends and let them know about this comprehensive Java guide online.
  • become a contributing author, if you think that you have information that could fill in some missing gaps in this book.

[edit] As an author or contributor

If you intent on writing content for this book, you need to do the following:

  • When writing content for this book, you can always pose as an anonymous author, however we recommend you sign-in into the Wikibooks website when doing so. It becomes easier to track and acknowledge changes to certain parts parts of the book. Furthermore, the opinions and views of logged-in users are given precedence over anonymous users.
  • Once you have started contributing content for this book, make sure that you add your name to the authors and contributors list.
  • Be bold and try to follow the conventions for this Wikibook. It is important that the conventions for this book be followed to the letter to make content consistent and reliable throughout.

[edit] Necessary prerequisites

[edit] Installing the Java platform

For in-depth information, see the chapter on the Java platform.

In order to make use of the content in this book, you would need to follow along each and every tutorial rather than simply reading through the book. But to do so, you would need access to a computer with the Java platform installed on it – the Java platform is the basic prerequisite for running and developing Java code, thus it is divided into two essential software:

  • the Java Runtime Environment (JRE), which is needed to run Java applications and applets; and,
  • the Java Development Kit (JDK), which is needed to develop those Java applications and applets.

However as a developer, you would only require the JDK which comes equipped with a JRE as well. Given below are installation instruction for the JDK for various operating systems:

Installation instructions for Windows

[edit] For Windows

[edit] Download instructions

Some Windows based systems come built-in with the JRE, however for the purposes of writing Java code by following the tutorials in this book, you would require the JDK nevertheless. To acquire the latest JDK (version 7), you can manually download the Java software from the Oracle website.

For the convenience of our readers, the following table presents direct links to the latest JDK for the Windows operating system.

Operating system Setup Installer License
Windows x86 Download Oracle Binary Code License Agreement
Windows x64 Download Oracle Binary Code License Agreement

You must follow the instructions for the setup installer wizard step-by-step with the default settings to ensure that Java is properly installed on your system. Once the setup is completed, it is highly recommended to restart your Windows operating system.

If you kept the default settings for the setup installer wizard, you JDK should now be installed at C:\Program Files\Java\jdk1.7.0_01. You would require the location to your bin folder at a later time – this is located at C:\Program Files\Java\jdk1.7.0_01\bin

[edit] Updating environment variables (Optional)

In order for you to start using the JDK compiler utility with the Command Prompt, you would need to set the environment variables that points to the bin folder of your recently installed JDK. Follow the steps below to permanently include your Java platform to your environment variables.

1. For Windows XP, click StartControl PanelSystem.
For Windows 2000, click StartSettingsControl PanelSystem.
For Window Vista or Windows 7, click StartControl PanelSystem and MaintenanceSystem.
Alternatively, you can also press Win+R to open the Run dialog. With the dialog open, type the following command at the prompt:
rundll32 shell32.dll,Control_RunDLL sysdm.cpl
2. Navigate to the Advanced tab on the top, and select Environment Variables...
3. Under System variables, select the variable named Path and click Edit...
4. In the Edit System Variable dialog, go the the Variable value field. This field is a list of directory paths separated by semi-colons (;).
5. To add a new path, append the location of your JDK bin folder separated by a semi-colon (;).
6. Click OK on every opened dialog to save changes and get past to where you started.

[edit] Start writing code

Once you have successfully installed the JDK on your system, you are ready to program code in the Java programming language. However, to write code, you would need a decent text editor. Windows comes with a default text editor by default – Notepad. In order to use notepad to write code in Java, you need to follow the steps below:

1. Click StartAll ProgramsAccessoriesNotepad to invoke the application.
Alternatively, you can also press Win+R to open the Run dialog. With the dialog open, type the following command at the prompt:
notepad
2. Once the Notepad application has fired up, you can use the editor to write code for the Java programming language.

The problem with Notepad however is that it does not support developer-friendly features, such as syntax highlighting and code completion. These features are a vital part of the exercise of writing code. Nevertheless, there are a variety of different open-source editors available as alternatives to Notepad that support these features.

For the purposes of the tutorials in this book, the most recommended editor is the Notepad++, a free and open-source fully integrated text editor that supports syntax highlighting and code completion. You need to download the latest version of Notepad++ in order to start writing code with the editor.

Note:
Amongst others, there are many editors available online that are specifically designed to code Java applications. Such editors have countless other features that facilitate programming with Java, e.g., debugging and application design interfaces, etc. Text editors that have comprehensive features and utilities to facilitate programmers are called Integrated Development Environments or IDEs. Java programmers often recommend the two most widely used IDEs for Java programming needs – these are:

Installation instructions for Linux

[edit] For Linux

[edit] Installation using Terminal

Downloading and installing the Java platform on Linux machines (in particular Ubuntu Linux) is very easy and straight-forward. To use the terminal to download and install the Java platform, follow the instructions below.

1. For Ubuntu, go to ApplicationAccessoriesTerminal.
Alternatively, you can press Alt+F2 to open the Run Application window. At the prompt, type xterm or gnome-terminal to open the Terminal window.
2. At the prompt, write the following:
$ sudo apt-get install openjdk-7-jdk openjdk-7-jre openjdk-7-doc
3. All Java software should be installed and instantly available now.

[edit] Download instructions

Alternatively, you can manually download the Java software from the Oracle website.

For the convenience of our readers, the following table presents direct links to the latest JDK for the Linux operating system.

Operating system RPM Tarball License
Linux x86 Download Download Oracle Binary Code License Agreement
Linux x64 Download Download Oracle Binary Code License Agreement

[edit] Start writing code

The most widely available text editor on Gnome desktops is the Gedit application, while on the KDE desktops, one can find Kate. However unlike Notepad on Windows, both these editors support syntax highlighting and code completion and therefore are sufficient for our purposes.

However, if you require a robust and standalone text-editor like the Notepad++ editor on Windows, you would require the use of the minimalistic editor loaded with features – SciTE. Follow the instructions below if you wish to install SciTE:

1. For Ubuntu, go to ApplicationAccessoriesTerminal.
Alternatively, you can press Alt+F2 to open the Run Application window. At the prompt, type xterm or gnome-terminal to open the Terminal window.
2. At the prompt, write the following:
$ sudo apt-get install scite
3. You should now be able to use the Scite editor for your programming needs.

Note:
Amongst others, there are many editors available online that are specifically designed to code Java applications. Such editors have countless other features that facilitate programming with Java, e.g., debugging and application design interfaces, etc. Text editors that have comprehensive features and utilities to facilitate programmers are called Integrated Development Environments or IDEs. Java programmers often recommend the two most widely used IDEs for Java programming needs – these are:

Installation instructions for Mac OS

[edit] For Mac OS

To do:
Add a section describing the installation of Java onto Mac OS-based machines. Incorporate some of the information provided in the commented section of this page.

Installation instructions for Solaris

[edit] For Solaris

To do:
Add a section describing the installation of Java onto Solaris machines. Incorporate some of the information provided in the commented section of this page.


Introduction Java Programming
About This Book
History
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export