Broad Network


Getting Started with Java

Java Basics - Part 1

Forward: Welcome to the exciting world of Java Programming. In this part of the series I introduce you to the Java programming language.

By: Chrysanthus Date Published: 1 Sep 2012

Introduction

Welcome to the exciting world of Java Programming. Java is a computer language I teach in these tutorials. Java is a very developed language. You can learn Java from these tutorials as your first language. A computer language is what you use to write a program to do the marvelous things you see at the computer and tablet screens. This is the first part of the series, Java Basics. In this part I introduce you to the Java programming language.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Code for other Languages
A programmer types a program in a text editor. A program just typed in a text editor, is coded text, understood mainly by programmers. If the ordinary person looks at the text, he would be able to identify a lot of words in the text, but he would not really understand what the whole text does. The program (code) typed in the text editor, is not machine language. The coded text is the program code. It has certain strings and characters positioned in such a way that the ordinary man cannot understand. Learning programming implies, learning the meaning of such typed text. The complete code typed is the program. A program can also exist in what is known as the binary form (see below).

Source Code and Execution
Code of text as mentioned above, is called the source code. A program is meant to carry out a task like to add numbers together. When a program is carrying out its task, it is being executed; and the program is said to be running. To “run” a program means to have it executed.

Compiler
Many programs are not run in their source code (written) form. In this case the source code has to be converted into what is called the Binary. A binary is a form of the code, which is best handled by the computer hardware. Some software has to do the conversion from the source code into the binary. The software that does this conversion is called the compiler. An example of a compiler is the MinGW Compiler Suite for the language, C++. For the Java language, which is the language of interest in this series, a compiler is found in the package, JDK6. We shall use this package for these tutorials. A package is a group of programs. There is the brand of JDK6 for the Windows Operating System, which is my operating system (Windows SP2). Nothing stops you from using some other compiler with some other operating system. The principles outlined in these tutorials will work with almost all Java compilers for their different operating systems.

Binary Code
For most languages, the result of the compiler is called the binary code. The binary code is the code understood by the machine (computer hardware). You can also call it the machine code. The binary code still has to work (execute) with a particular operating system. The result of the compiler for Java is different. (I explain below).

Prerequisite
In order to study Java and master it, your level of mathematics should be at least that of Middle School. If you did not pass in middle school mathematics, do not worry. There is a way out. You should study the mathematics course easily at the link below. This site offers online interactive middle school math course in a step-by-step fashion, in as short as three months or as long as 10 months. There you have the right to ask questions from the authors of the web site.

However, if you do not have middle school mathematics, you can still learn this course (Java), since the little mathematics involved is taught as you go through the course.

You need to be computer literate to start the course.

Cool Mathematics

Requirements
Here I give you the requirements for my computer. Nothing stops you from using some other requirements, if you know how to handle that.

- The Java SE Development Kit 6 (JDK 6)
- Text Editor
- Your Personal Computer and a Windows Operating System.

Downloading
The compiler, the Java SE Development Kit 6 (JDK 6) is free to download (actually, JDK6 has the compiler). I give you instructions to on how to get the JDK 6 compiler from the Internet. These instructions may change with time.

- Go to the home page, java.com.
- Search for the page titled, "Hello World!" for Microsoft Windows, using the search box of the home page.
- Under, The Java SE Development Kit 6 (JDK 6), click, download the Windows version now.
- Follow the instructions and it will take you to the page titled, Java SE Downloads.
- In this page, accept the agreement and click the link, jdk-6u32-windows-i586.exe, under, Java SE Development Kit 6 Update 32. Download will begin.

The name of the file downloaded is, jdk-6u32-windows-i586.exe. This is a compressed file that has the JDK 6 package that has the Java compiler called, javac.

Installation
Double click the jdk-6u32-windows-i586.exe file and allow default settings (installation may take a while). The installation installs the JDK 6 package.

Java Run Time Environment and Bytecodes
Java Run Time Environment (JRE) or Java Virtual Machine or Java Platform are more or less the same thing. The Java Virtual Machine is a special operating system that Java comes with. It sits along side your operating system and communicates directly with the hardware (microprocessor, memory, etc.). It has to be compatible with your operating system. Mine is compatible with my Windows Operating system.

The Java compiler does not compile the source code into a binary code to work directly with the hardware. It compiles it into what is known as a class file. The class file sends information to the hardware through the Java Virtual Machine, not directly. The instructions of the class file are called bytecodes. The class file is a file and is said to be of a higher language level than a binary file.

When a Java class is executing, it is the Java Virtual machine that is executing it. The Java Virtual Machine is also called an interpreter, and is said to interpret the Java class.

Java Compiler
Know that JDK stands for Java Development Kit. The Java compiler in the JDK is called, javac.

The Console and Working Directory
We shall work with the console; this means that your input and output to any Java program you write will be text. In other to achieve this, you will use the Windows Command Prompt program. (if you have a different operating system, you will have to use an equivalent to the command prompt window, which will display input and output as text on the screen). To access the Command Prompt program, click the start button, point to All Programs, point to Accessories, you will see the Command Prompt program; click it. You will then see a window for white text on black background. That is your window to type input as text and see output as text. For a different operating system, you might have another way of arriving at the console.

Create a directory (folder) in the root c: directory and call it, java. It is in the directory that your source and compiled files will be saved.

Your First Java Program
Start your computer and open the Command Prompt window. You should see some white text in the window, ending with something like, C:\Documents and Settings\User> , where user is your name. This is called the DOS Prompt or Command Prompt. In order to work with the console, you need the DOS prompt. Your commands are typed to the right of the DOS prompt. Your response will be displayed by the system, below the DOS Prompt. After that display, you see the DOS Prompt again.

C:\Documents and Settings\User> is not the DOS prompt we shall use. We shall use the DOS Prompt C:\java> because java will be the working directory.

To arrive at this DOS Prompt, type,  cd.. , and press the Enter key of the keyboard. Type, cd.. , again and press the Enter key. You should now be at the DOS prompt, C:\> . To go to the working directory (DOS Prompt for working directory), type, cd java , and press the Enter key. You should see the DOS Prompt, C:\java>. That is the DOS Prompt (command prompt) for your working directory.

Open your text editor and type the following source code exactly as you see it:

class Hello
    {
        public static void main(String[] args)
            {
                System.out.println("Hello World!");
            }
    }

This is your first source code. Save the document typed with the name Hello.java in the directory, java; the H of the Hello is in uppercase, while the rest of the characters are in lowercase. Note that the file name is Hello and the extension is java (not txt). I will explain the content of the source code later.

Windows Environment Variable
The Windows operating system has a set of variables (items) called environment variables. One of these variables is called the path variable. The value of the path variable consists of directory paths to executable files in the hard disk. If the path of an executable file is in the hard disk, you just need to type the name of the file at the command prompt without the path, in order to execute the file.

In my computer, the name including the extension of the Java compiler file is, javac.exe . This is the file that has to be called to compile a Java source file. It is in the path (and directory), C:\Program Files\Java\jdk1.6.0_32\bin . So this path has to be included as value to the path variable. With that, you will need to just type at the command prompt, javac, without the path in order to compile a source file.

In order to do that, I clicked, Start|Control Panel|System . The System Properties window appeared. I then clicked the Advanced Tab and in the resulting panel I clicked the Environment Variables Button. In the Environment Variables window I double-clicked Path in the System Variables pane. A small dialog box opened. In the Variable value field of the dialog box I added “;C:\Program Files\Java\jdk1.6.0_32\bin” without the quotes at the end. I then clicked all the OK buttons of the opened windows (dialog boxes). After that I restarted the computer. You have to do something similar to enable your java compiler to operate without typing its path in the DOS prompt window.

Compiling the File
The file with the extension, java, has to be compiled to a class file to have the new extension, class.

Bring up the Command Prompt window again (if it is not displayed). While at the C:\java> DOS Prompt, and on the right, type the following and press the Enter Key

    javac Hello.java

After a while, the C:\java prompt should re-appear, below what is typed. This means the file, Hello.java has been saved in the java directory. The file is the result of compilation by the javac file (compiler - program). The word, “javac” in this line, is the name of the compiler.

Note that the source file has the extension, “java” and the compiled file has the extension, “class”. The compiled file is called the class file. Note: a Java compiler compiles a source file into a bytecode (or class) file.

Running the Class File
It is now time to run (execute) the program (file), which is now in the form, Hello.class in the java directory and see the output. Now, type the following at the command prompt, and press the Enter Key:

    java Hello

After a short while you should see the output: Hello World! . The word, “java” here is a command. It commands the Java Virtual Machine to execute (interpret) the program, Hello.class . In this line, you do not type the extension of “class” for Hello.class . Below the output in the command prompt window, you should see the command prompt again.

Case Sensitivity
Java is said to be case sensitive, so the name of a file such as, “Hello” is not the same as “HELLO”, is not the same as, “heLLO”, etc. Always have the extensions in lowercase.

Comment on your First Source Code
I will give you the meaning of most of the things in your first source code as we go along in the series. For now I will only comment on the lines, “class Hello” and “System.out.println("Hello World!");”

In the line, “class Hello” after the word “class”, whatever is typed after, such as “Hello”, becomes the name of the class file, as in, Hello.class . The line “System.out.println("Hello World!");” is an example of what is called a statement. It sends the phrase, "Hello World!" without the quotes, to the output. Note the phrase within the statement.

You have seen a lot, let us take a break here and continue in the next part of the series. I have done my best to be very explicit with all the parts of the series.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message