Basic Introduction To Java

This page has been created to provide a basic introduction to the Java Programming Language, including a HelloWorld code example. Please enhance this page with any information you deem necessary.

What is Java?…

Lets not get too technical here. In short, Java is a programming language which can run on any mainstream operating system (Windows/MAC/Linux). When a developer writes statements in Java and saves them into a *.java file, the file .java file must first be compiled before the statements can be executed. The compilation process involves translating the Java statements into machine readable byte code which is then saved into a *.class file. The bytecode (class file) can then be run on any Java Virtual Machine (JVM) regardless of computer architecture, this gives the Java language its platform independence.

Before we begin writing code, we need to set up our environment (if not done so already!)

First, we need to ensure that the Java Runtime Environment (JRE) and the Java Development Kit (JDK) are installed on our machines. These are both freely available at the Java website. The JRE is software which allows our computers to execute Java code, it is likely that this is already installed on your machines. The JDK, however, is not likely to be installed on your computer unless you have coded Java with it before. For Java beginners, our Interface Development Environment (IDE) of choice is jGrasp which is freely available here. JGrasp is a very simple application which allows you to create, compile and run Java files.

Begin Learning to write Java - Our HelloWorld Example

  • Open up jGrasp
  • Click "File" at the top of the screen
  • Hover over "New" and select Java - this creates an empty Java file for us to write code into
  • At this point, we should save the new Java file. Save the file in the desired location with the name "HelloWorld.java"
  • Now type the following Java code into our new Java file in JGrasp:
class HelloWorld {  //This is the name of our class - this should always be identical to the name we have given file 

    public static void main (String[] args) { //This is the "main method and is the starting point of every java application

        System.out.println("Hello World!!");
    }
}
  • Save the file, then click the green cross at the top of jGrasp. This will check our code for errors and then compile the Java file and generate a HelloWorld.class in the same folder as our HelloWorld.java file.
  • Finally, click the run icon which is the little red running mad next to the compile icon in jGrasp. This will execute our HelloWorld.class file and complete whatever commands we have written in the main method.

You should see the following output on screen in the lower panel on jGrasp:

Hello World!!

Explanation

What just happened here?… The lower panel on jGrasp where "Hello World!!" was printed is called the console. The console is a representation of what our program would look like if it was run in a command prompt like cmd.exe, MS-DOS, or the UNIX/LINUX terminals. Back when Java was created in the early 90's, most applications would have been created for and run entirely in the console.

Class declaration

An application is created from classes. A class stores related data in fields, where the fields can be different types. So you could, for example, store a text string in one field, an integer in another field, and a floating point in a third field. For example, a very simple class might store a string of text and define one method to set the string and another method to get the string and print it to the console. Methods that work on the data are called accessor methods.

Main method

Every application needs one class with a main method. This class is the entry point for the program, and is the class name passed to the java interpreter command to run the application. The code in the main method executes first when the program starts, and is the control point from which the controller class accessor methods are called to work on the data.

Formal definitions aside…
Apart from the class and the main method declarations, we wrote one line of code in this application:

"System.out.println("Hello World!!")";
  • The semi-colon at the end of the statement is important. I tells the compiler that this is the end of the line of code.
  • The word "System" refers to the name of a package which exists inside the JDK which we downloaded earlier.
  • A package is a group of Java classes written by somebody.
  • In this case, we wrote "System.out", which means that we accessed a class called "out" which resides in the package "System".
  • "printlin()" is a method which has been written inside the "out" class. It takes any String of characters and prints them to the console, which is why putting "Hello World!!" inside the brackets printed Hello World!! to the console.

Video Tutorial

This is my first video tutorial. They are surprisingly difficult to make, sometimes my words gets tangled and at one point I break into a fit of coughs so bear with me on that one…

And there you have it! You have just created your first Java program!