Java is an Object Oriented programming language that is very popular amongst commercial organizations to develop a wide range of applications from small utilities to enterprise wide, mission critical systems.
Objectives
The main objective of this course is to get you up to speed with the first steps required to write a java program. It will guide you on all the steps that you will need to set up java, all the environments and to write your java program.
What is Java?
Java is an Object Oriented programming language, meaning that every day real objects can be modeled by java. The code that is written to model an everyday object is called a class.
Just as real day objects are composed of other objects so can classes be composed of other classes.
For example a “Human” may be composed of the following objects:
- Head
- Legs
- Arms
- Personality
- Attitude
To model the above objects a class is created for each of them. To write a class you can open a simple text editor and write code. Each class is often saved in its own file.
What sets Java apart from other languages?
Java is an interpreted Language.
Java is said to be an interpreted language for it is not compiled into the native platform bytecode. Instead the byte code is interpreted at run time and the native libraries called.
Java Runtime Environment is downloaded together with Java Development Kit (JDK) or separately. Here, you will be able to find code that is used to call the java native libraries.
Each Operating System has its own Java Runtime Environment (JRE). For example the JRE for a Unix machine makes calls to native code provided by the unix platform. On the other hand the JRE for the windows platform makes calls to native code provided by the Windows Platform.
The great thing about Java then is that you can compile your classes into byte code using the javac tool, and then you can deploy that bytecode to different platforms, where each platform will have the correct JRE to make calls to the native code of that platform.
This means you compile your code once and it works everywhere!
Steps necessary to run an application
Getting Started
There are quite a few things that can go wrong when getting ready to write your first application.
You need to compile your classes into platform independent byte code using the javac tool and then execute the bytecode using the java tool.The most unsophisticated way of using the javac and java tools is to open a msdos window (or shell window in the case of unix) and type the javac and java commands.
Both the javac and java commands have switches which specify different aspects of the compilation and execution such as:
* where to create the byte code files when compiling. The byte code files end in the file extension, “.class”.
* The classpath for compiling or executing code. The classpath defines where the classes are that you want to compile or run.
To save you pulling your hair out working out how to use all the switches and possibilities with the javac and java tools you will be shown some bat files that contain all the commands you would ordinarily type on the command line of the shell window. Note that bat files are for the windows platform. If you are using Unix, you will need to write your shell scripts to do the same thing as the bat files.
To compile or run the code you simply have to change directory in a shell window to where the bat files are and type the name of the bat file (without the “.bat” file extension and press the return key of your keyboard.Once you have the example working, you will be able to look more closely at the bat files and understand a bit better what is going on.
Installing java
To develop application using java you need to download and install JDK(java development kit) on your machine. Visit here to download JDK and install it. It is always good to choose the latest version.
In the section, you will be guided on how to comfortably install JDK, together with the JDK source code and its documentation , or the Javadocs.
The first step towards running your first application is to:
* Download the JDK of the web (follow the link above)
* Downloading JDK and documentation of the internet
It will also be a good idea to download the documentation from the same page and install it locally because you are likely to be using it a lot!!!
If you are using unix platform such as Ubuntu you can use “apt-get install “ to install JDK .This tool download JDK and install it for . e.g
jack@jack-pc:~$ apt-cache search jdk
to install jdk X type
jack@jack-pc:~$ sudo apt-get install openjdk-X-jdk
Installing JDK on Windows
Once you have downloaded the JDK, run the executable to install it, by double clicking on it.
You will need to agree to the license terms and select your platform.
Note that the installation will prompt you several times including whether you also want to install the JDK and the JRE. Say yes to everything.
NB after installation you need to set up java path in your environmental variable to run java and javac command on command line
Using Integrated Development Environments (IDEs)
There are lots of different Integrated Development Environments (IDEs) and other tools available to assist you with your development. They can help speed up your development but only if you understand all the terminology they use.
To start with you are going to use a text editor of your choice for writing the code for your applications.
You will then use bat files for compiling and running your application.
If you look into the bat files of the examples you will soon see how the javac.exe and java.exe executables function.
If you remember, javac is an executable used for compiling your code while java is an executable used for running your compiled code.
Later on you can use an IDE (Integrated Development Environment) to help you write your code, compile, and run it.
Both the following IDEs are good:
* Eclipse
* Netbeans
To get a Java Programme running you are required to perform the following
Steps:
* Write in a text editor or IDE (Integrated Development Environment) of your choice the java source code in text format. Good IDEs include: Eclipse and NetBeans.
The java source code may look like:
package com.jackson.helloworld;
public class HelloWorld {
/**
* Constructor
*/
public HelloWorld() {
super();
}
/**
* This method says Hello World to all the java geeks running this
* program.
*
* @return Greeting the hello world message to deliver to all Java Geeks.
*/
public String getGreeting() {
String greeting = "Hello World, Java is sweet!";
return greeting;
}
/**
* This is the point where the program execution starts.
* @param args
* these are parameters that are usually passed into the java program.
*In this case, we do not have any parameters passed into the program.
*/
public static void main( String[] args ) {
// Instantiate the class
HelloWorld helloWorld = new HelloWorld();
// Call the getGreeting method and store the String returned
// in a variable of type String called greeting
String greeting = helloWorld.getGreeting();
// Use a print writer to print the message to the screen
System.out.println( greeting );
}
}
A compiler is used to obtaing a platform independent byte code from a Java Source Code, through a process called compilation. The javac
command is used in the process.:javac -sourcepath SVN_LOCAL\full path -d BUILD_LOCAL\implementation\build\classes SVN_LOCAL\implementation\fullpath\HelloWorld.java
The javac command will generate the following class file:BUILD_LOCAL\implementation\build\classes\fullpath\HelloWorld.class
Note that while the java source files is composed of text, the HelloWorld.class file is a binary file composed of a “byte code”
Running of the “byte code” is achieved using the Java Virtual Machine and a tool called “java”. The Java Run Time Environment is a set of libraries that needs to be installed on the computer that is running the Java Application. The Java Virtual Machine (JVM) is hosted by the Java Run Time. An interpreter in the JVM is used by the java command to interpret the byte code. It also makes calls to different class libraries. These libraries call the java native code. Different operating systems require different Java Run Times. The advantages of the Java Run Time Environment:
- The java byte code (.class files) written by the developer are smaller as they make use of libraries included in the Java Run Time
- Once Java Source Code (.java files) has been compiled into byte code (.class files), the same byte code can be run on many different operating systems as long as the computer hosting the operating system has the Java Runtime Environment.
Included in the Java Runtime Environment (JRE) is a Java Virtual Machine (JVM) which includes an interpreter that interprets the byte code in real time and makes calls to Native Code.
The alternative to using an interpreted language like Java is that you have to compile the source code into native code using a different compiler for each operating system you wish to support.
With the Java programming language however you can compile your Java source code into byte code and then run that same byte code on any operating system, as long as that operating system has the Java Runtime Environment installed.
Having run the java command, this example would print the following text to the msDos (console) window:
Hello World, Java is sweet!
The History of Java
Java evolved from a language called Oak and has actually been around for about twenty years. Knowing its history is unlikely to change your career but you might find it interesting to know a little about how we got to where we are today. This article will give you the details:
Conclusion
This tutorial is expected to give you a head start on how to quickly and easily get started with java, install all the requirements and set up your environment. We will handle java basics in the next tutorial.