Home

Introduction to Java

 

Overview of Java

 

Introduction

A computer language is a series of words used to give instructions to the computer, telling it what to do, how to do it, and when to do it. Just like there are various human languages used to communicate, there are also various computer languages. One of these computer languages is called Java. The purpose of this site is to study this language.

In our lessons, we will learn Java as a computer language not as a development environment. This means that we will primarily build applications that show how this language functions, not how to create graphical applications. The types of programs we will create on this site are called console applications. By default, a console application displays its result(s) in a black window. To make it a little easier, we will use an environment that displays a window but with a different background color.

Getting Into Java

As you may be aware, the computer doesn't speak or understand human languages. Still, to communicate your instructions to the computer, you can write them in a human language, using characters, letters, symbols, and words you are familiar with. When doing this, you prepare text that would be communicated to the computer. For the computer to understand these instructions, they must be "translated" into the computer's own language, also called the machine language. This intermediary "translator" is also called a compiler. There are various compilers on the market. One of them, or a few of them, are provided by Sun and they are free. Although we will create only console applications, we will use NetBeans IDE.

To start, connect to the Sun Microsystems web site, click the Downloads link or do a search on the word "Java". Download the latest Java 2 Platform, Standard Edition or J2SE development kit. At the time of this writing, it is J2SE 5.0. You may be prompted to download the NetBeans IDE + JDK 5.0 Update 2. This is fine.

Language Fundamentals

 

Introduction to Classes

Imagine you want to create a list of books of your personal library. To characterize each book, you can create a list of words that describe it. This list would be made of general characteristics of each book. Here is an example:

Book
Book Title
Author
Publisher
Year Published

In the computer world, the work Book in the above example is referred to as a class: A class is a list of criteria used to describe something. Each word or group of words, in bold in the above example, is called a property of the class. In our example, Book Title is a property of the Book class. In the same way, Author is another property of the of the Book class. In Java, there is never space between two words that represent the same entity. This means that the above list is better represented as follows:

Book
BookTitle
Author
Publisher
YearPublished

One class can also be a property of another class (unfortunately, we need to mention these issues in our introduction but don't make too much effort to understand them: everything will be clearer in future lessons). For example, imagine you create another class that can be used to describe each author of a book. The list of properties can be created as follows:

Author
Name
Nationality

You can then use this class to define a criterion in the Book class, exactly as done above: only the Author word is necessary in the Book class, its Name and Nationality criteria are directly represented.

Every property that is created in the class is referred to as its member. For example, in the above lists, Nationality is a member of the Author class while Publisher is a member of the Book class.

Introduction to Objects

To describe a particular book of your collection, you can provide information for each characteristic of the class used to describe it. Here is an example:

Author: Mojang
Name: Ferdinand Oyono
Nationality: Cameroon

In the same way, you can describe each author of the collection by filling out its characteristics using the above criteria. When describing an object as done in this example, you must first give a name to the object (this is not always necessary as we will see in future lessons). In the above example, Modjang is the name of the object. Author is (still) the name of the class. This name is necessary because you would need it in a class where Author must be used as a member. Based on this, you can use the name of the above class when describing a Book object. Here is an example:

Book: First Item
Book Title: Houseboy
Author: Modjang
Publisher: Heinemann
Year Published: 1991

It is important to understand the difference between a class and an object. An object is the result of describing something using the preset criteria of a (known) class. In the strict sense, you must first create a class by specifying each criterion you want to use to eventually describe some objects. On the other hand, you can create an object only that is based on an existing class.

Everything statement in Java must end with a semi-colon. This results in the following:

Book: First Item;
Book Title: Houseboy;
Author: Modjang;
Publisher: Heinemann;
Year Published: 1991;
 

Methods

In many cases, only characteristics are necessary to have a class on which complete objects can be described. Still, an object may be expected to perform one or more assignments or actions. An assignment that an object can perform is called a method.

Imagine you have books that that can open or close themselves when asked to do so (Sci-Fi anyone?). The action used to open may be called Open. To differentiate an assignment, or method, from a property, the name of a method must be followed with parentheses. Here are two examples:

Book
BookTitle
Author
Publisher
YearPublished
Open()
Close()

Although this list shows the methods last, this is not a rule, the methods can be the first, the last, or included between, as long as they belong to the class. In the same way, a class that is made a property (member) of a class can also have its own methods.

Every item that is created in a class is referred to as its member.

Introduction to Built-In Classes

A computer language is developed from scratch. Then other languages can be developed using its existing rules. This is also a rule that people who developed Java followed. One of the assignments they performed was to create many classes that you can directly use in your program to lay a solid foundation and work on top of that. The classes that already exist with Java are referred to as built-in classes. To make it easy to "catalog" the various built-in classes, they were created in libraries called packages and there are various of them.

One of the classes that ship with Java is called System. One of the jobs of the System class is to display something on the screen. To perform this job, the System class is equipped with a property called out. Actually, out itself is a class as we mentioned earlier that one class can be used as a property of another class. To actually display something on the screen, the out class is equipped with a method called println (read Print Line). To display something, you can write it between the parentheses of this method. If the item to display is a word, you must include it between double-quotes.

We will see that another job it can handle is to get a value from the user.

Applications Fundamentals

 

Creating a Project

A Java application is primarily created from a file located in a folder. As we will see in the next section, this file contains a class (we will learn more about classes starting in Lesson 4). You must use a folder in which you will save the file:

  • You can create the folder when saving the file. To do this in Notepad, when saving the file, locate the drive or the parent folder and display it in the Look In combo box. Then click the Create New Folder button Create New Folder. Type the name of the folder and press Enter
  • You can create a folder using Windows Explorer. To do this, in the left frame, select the drive or the parent folder. In the right frame, right-click an empty area -> New -> Folder, type the name of the folder, and press Enter
  • You can create a folder using the Command Prompt. The command to create a folder at the prompt is MD followed by the intended name of the file. Here is an example of creating a folder named Exercise1 in the C: drive:
     
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\Documents and Settings\Administrator>CD\
    
    C:\>MD Exercise1
    
    C:\>

Command Prompt

Instead of working manually, you can use NetBeans.

Creating a File

Java code consists of instructions written in plain English. The simplest document of a Java code contains a class and something called main. The formula we will use and accept at this time is:

public class Exercise {
    public static void main(String[] args) {
    }
}

Every single word or symbol on the above code will be explained in later lessons. For now, accept it "as is".

After typing the code, save it in the desired folder. Here is an example:

Save As

Introductions

 
 

 

 

 

 

Practical LearningPractical Learning: Starting NetBeans

  1. To start NetBeans, on the taskbar, click Project -> (All) Programs -> NetBeans -> NetBeans
  2. On the main meni, click File -> New Project...
    In the first page of the wizard, In the Categories list, click Java (it should be selected already). In the Project list, click Java Application (it should be selected already)
     
    New Project
  3. Click Next
  4. In the Projects list, make sure that Java Application is selected and click Next
  5. In the Project Name text box, type Exercise1
    In the Project Location, accept the suggested path
  6. In the Create Main Class text box, type Exercise

     
    New Java Application
  7. Click Finish
 

Compiling a Project

After writing code and saving the file, you can prepare it to see the result. You must first compile it. Of course you must have downloaded the Java compiler or NetBeans. If you are working from the Command Prompt, it may be a good idea to modify the System Variable. Before doing this, locate the installation of Java on your computer and show the contents of the bin folder:

Windows Explorer

Select and copy the path in the Address bar.

To prepare the path, open the Control Panel and access the Administrative Tools followed by System. Click Advanced

System Properties

In the Startup and Recovery section, click Environment Variables. In the  section, System Variables section, double-click Path. In the Variable Value, press End, type ; and type the path to the bin folder of Java. Here is an example:

Environment Variables

Click OK.

To compile the project at the Command Prompt, switch to the folder where you saved the file:

Command Prompt

To actually compile, you use the program named javac followed by the name of the file and the extension .java. An example is:

javac Exercise.java

Command Prompt

Executing a Project

To see the result of an application, you must execute it. To do this at the Command Prompt, the program to use is called java. Therefore, to execute an application at the Command Prompt, type java followed by the name of the file with the extension and press Enter.

Command Prompt

Practical LearningPractical Learning: Executing a Project

  • To execute the application, on the main menu, click Run -> Run Main Project

Java Support for Code Writing

 

Empty Spaces

Java code consists of instructions written in plain English. Many of these instructions can be written on one line of code. Here is an example:

public class Exercise{public static void main(String[] args){}}

This code works fine because Java doesn't care where the empty spaces among the words and expressions of code are positioned. For example, the above code could have been written as follows:

public
class
Exercise
{
public
static
void
main
(
String
[
]
args
)
{
}
}

It would still work fine. If you write code like any of the above two sections, it can be difficult to read. The alternative is to write each statement on its own line. A statement is a line that code that ends with a semi-colon.

The exception to this rule is when you use the for loop as we will learn in future lesson. Besides the semi-colon statements, you can use indentation to make your code easy to read. Indentation consists of aligning sections of code that belong to the same block. For example, if you start a section of code with an opening curly bracket "{", you must end that section with a closing curly bracket "}". Here is an example:

public class Exercise
{
    public static void main(String[] args)
    {
    }
}

Comments

A comment is text that is not part of your code but serves as guide when trying to figure out what the lines of code of your program are. This means that a comment is not taken into consideration when your code is built. For this reason, when writing a comment, you use plain language as you wish.

There are two usual ways of including comments in your program. To comment the contents of one line, you start it with double forward slashes like this //

Here is an example:

// Here is a simple sentence

You can include many lines of comments in your program. To do that, comment each line with the double slashes. An alternative is to start the beginning of the commented line or paragraph with /* and end the commented section with */

Here is another commented version of our program:

// A simple exercise

/* Here is a simple sentence that I want to display
when the program starts. It doesn't do much.
I am planning to do more stuff in the future. */

// The end of my program

Practical LearningPractical Learning: Using Comments

  • Find the // TODO code application logic here line. Click on the right side of that line. Press Enter and type:
     
    /**
     *
     * @author Gertrude Monay
     */
    public class Exercise {
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("Welcome to the Wonderful World of Java!!!");
        }
    }

 

 
 
 

Home Copyright © 2008-2016, FunctionX, Inc. Next