Java Beginning Programming

Since its creation about 16 years ago, Java has become ubiquitous. Developers appreciate its "write once, run anywhere" philosophy. If you want to start making use of Java and its many benefits, keep reading.

A Brief History

One bleak day in June of 1991, James Gosling sat staring out his window, dreaming of a new programming language that would allow users to create platform independent applications. A soft wind whispered, “Write Once, Run Anywhere.” At first he thought the large tree outside his window was speaking to him, so he named the language “Oak.” Later, lightning hit the tree, startling Mr. Gosling and making him spill his coffee. And thus, Java was born.

Okay, so that isn’t exactly how it happened. But that isn’t important. What is important to know about Java’s history is this:

  1. It was indeed invented by James Gosling in June of 1991.

  2. It was originally named after the tree outside his office and later renamed to Java from a group of random words.

  3. It was invented to be portable, meaning that it could run on any platform (so long as the Java Virtual Machine was installed, that is).

  4. Java is a derivative of the C/C++ languages.

In this tutorial, I will attempt to teach you the basics of computer programming with Java. In doing so, I will make several assumptions. 

  • You are familiar with the basic workings of a computer.
  • You have no experience programming (outside of your VCR remote, and let’s face it, your son was in the trenches with you on that one).
  • You are using a computer with one of the following operating systems installed: Windows 95, 98, 2000, NT or XP. 
  • You have downloaded and installed the Java Development environment. If not, you can do so here. Be sure to follow the installation instructions included with the program.

Let’s get started.

{mospagebreak title=Terminology}

Before we begin, there are some words and phrases you should become familiar with. Don’t worry, I won’t make you spell them in front of the class; my chalkboard is already full from the last class.

Computer Program: Simply put, a computer program is a set of instructions put together that tells your computer what to do. Programs can range in complexity from simple mathematical equations to launching a space shuttle into orbit.

Programming Language: Just as you speak a language to communicate with the people around you, your computer requires you to speak a certain language to it. (Note: Do not actually speak to your computer; it won’t speak back). In this case, the language is Java.

Compiler: This takes your programming language, translates it into a lingo the computer can understand and creates an executable file that allows your program to run.

We’ll review some other terms later. For now, let’s start you off on your first program. Traditionally, your first program, no matter what language you choose, prints the words, “Hello World!” to your screen. But you are going to one day write programs that launch space shuttles, so for you, the program will be a gritty, in your face, “Goodbye Cruel World.”

{mospagebreak title=Creating the Goodbye Cruel World application}

To start, you will need to create a source file. Left-click on your Start menu and choose All Programs>Accessories>Notepad. When your document opens, type the following code:

Note: All code is case-sensitive, so be sure to type it in exactly as you see it.

* This application will print the words “Goodbye Cruel World” across
* your monitor. Also note that these are just comments in your code
* for other programmers, and is invisible to the compiler.

class GoodbyeCruelWorldApp {
  public static void main(String[] args) {
    System.out.println("Goodbye Cruel World!"); // Displays the string.

Save the notepad file to your C: drive (it typically does not matter where you store the file. But in this case we will use your C: drive) under the name "", being sure to include the quotation marks. Then close your saved file.

Next we must change your source file into a .class File. To do this, left-click on your Start menu and choose the Run command. Type cmd into the pop-up box and press Enter. This will load the Command window, shown below:


In order to compile your Source File, you must change the directory to where your file is located. In the above example the prompt is set to your C: Drive. To change the directory, type cd C: and press Enter.

Next, to display the files within your C: Drive, type dir

You are now able to compile. In the prompt line type javac and press Enter.

If you type in dir again, you will see the new file, GoodbyeCruelWorldApp.class.

You are now ready to run your first program!

Running Your First Program

Without changing your directory, type java GoodbyeCruelWorldApp at the prompt.

This should display the text: “GoodbyeCruelWorld!” in the prompt window.

{mospagebreak title=Understanding the Code}

Now that you’ve created a program to bid good riddance to those who laughed when you told them about your impending job with NASA, it’s time to examine the code you’ve just written to understand how it works.

The first section we will look at is the Source Code Comments, shown below.

* This application will print the words “Goodbye Cruel World” across
* your monitor. Also note that these comments are in your code for
* other programmers, and is invisible to the compiler.

As you can see, the /* informs the computer that this is where you will be making comments and that it is not allowed to read these, no matter how curious it might get. These comments are for other programmers, should they ever need to fix your code or add something to it. After all, with all your brains and chutzpah, you won’t be in the same position forever.

To close the Comment section and let the computer know that it is okay to look again, simply type */ to end the comment. You can open another comment anywhere within the application you are creating and in fact, it is recommended that you do so, and often.

// text

In the above example, the computer ignores everything from // to the end of the line.

Next we will look at the Class Definition section of the Code. Don’t fret too much over this new phrase at the moment; I will discuss it in more depth in the second part of this tutorial.

class GoodbyeCruelWorldApp {
public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.

In the above sample, the bold lines indicate the opening and closing parts of the Class Definition.

Finally, we come to the Main Method:

public static void main(String[] args)

Every program you write within Java must contain this Main Method. Basically, it passes information to your application that it will need to perform the function you created it to do.

And lastly, the line


tells the program to print the phrase “GoodbyeCruelWorld!” (or any other phrase you place within it) to your screen.

Let’s say we want to work with numbers. In this example, you will see something called variables. Don’t worry about those just yet, as we will be touching upon them in great detail in future articles. In this sample, the program is going to multiply one number by another number and print out the result.

A program to calculate numbers.
We will name this program “”.
class TheMathWizard {
  public static void main(string args[]) {
    int num; // this is how you declare a variable and name it num
    num=4000 //this places the value 4000 in our variable named num
    System.out.println(“This is your puny number!: “ + num);
    num = num*2; //this tells the program to multiply our variable num
    // by two
    System.out.print (“The total of your puny number * 2 is “);

After running this mathematically complex program, you should see the following on your screen:

This is your puny number! : 4000
The total of your puny number *2 is 8000

If we wanted to complicate matters, we could have the program ask you for a number to multiply by or have the user enter a number to be multiplied (or both). But we’ll save that for another day.

You are now free to take a break. Go update your resume and get ready to send it to NASA. But don’t mail it off just yet. The second part of the "How to Program in Java" tutorial will be coming soon.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye