|
Graeme Kerry
is a first year computer systems engineering student
from the UK, studying at Lancaster University. He has been
programming in Java for about 3 years. Outside of studies
he enjoys sports and socialising with friends.
Kezz's Java Tutorials are written in Graeme's
spare time.
|
Part 1 HelloWorld.java
Introduction
Welcome to the first of my introduction to Java tutorials. These tutorials
are aimed at complete beginners to programming. Java is a good language to start
with because its syntax is very similar to the more powerful c++ and it has been
designed in a very object oriented way. Object oriented programming (or OOP for
short) is a way of programming that breaks the problem down into parts. These
parts then slot together to form the solution.
What You Will Need
To follow these tutorials all you need to get your hands on is the Java SDK
(sometimes called the JDK). SDK stands for Software Development Kit. The Java
SDK is available free!! from here . This kit contains all you
need to program in java. To write your source files I recommend using a program
such as notepad or Xemacs (under *NIX environments). These are just suggestions
and any plain text editor can be used as long as it can save files in raw text
format.
Main Parts of the Java SDK
The SDK contains many programs and is quite a hefty download. The main parts
you will be using are detailed below.
- javac.exe - This is the java compiler. It turns your
source code into files that can be read by the java interpreter.
- java.exe - This is the java interpreter. It reads
through you compiled code line by line and executes the statements.
- appletviewer.exe - This will be used later on in the
tutorials when we get into applet programming. This tool displays an applet
as it would appear on an html page. (An applet is a java program that can
run in a web browser).
HelloWorld.java
Ok enough explanation, lets start coding! This is the most basic of programs
and all programming tutorials and courses I've seen seem to start with it.
Fire up your text editor and enter the code listed below and save it as
HelloWorld.java.
public class HelloWorld {
public static void main (String [] args)
{
System.out.println("Hello
World!!!");
}
}
After saving this file open an ms-dos command box
(Start->Run->command.com should do the trick, on newer versions of Windows
try cmd.exe) or a *NIX terminal and change to the directory where you saved the
source file. To compile a java source file we use javac, the java compiler. So
to compile out HelloWorld.java program execute the following statement:
javac HelloWorld.java. If you copied the code exactly right as shown above the
compiler won't say a thing and you'll just end up with a new command prompt.
This is good!!! Now to run your program use the java
interpreter (java.exe) by executing java HelloWorld. The computer now reads your
file and should print "Hello World!!!" before leaving you at a fresh command
prompt. Thats all there is to it. I realise this isn't very impressive but its
essentially one line of code!.
Lets take a closer look at the source code.
public class HelloWorld {
This line of code defines a new java class called HelloWorld. A class is a
java program. Note the capitalisation of the HelloWorld. To make this file
compile it must be stored in a file called
HelloWorld.java with exactly the same name (capitalisation etc). The { at the
end signifies the start of a block of code in java and as you can see on the
last line this block is ended with a }
public static void main (String [] args)
{
This is the main part of the program. It is called the main method. This
method is always the first thing that is executed in a java application so
everything in here is very important. Once again a { must be used to start
the main method block. This means that everything that follows
between the { and } belongs to the main method.
System.out.println("Hello World!!!");
This line is a call to the println method of the System.out
class. Don't worry about the technicalities, just remember that to print
something to the screen you use System.out.println("<what you want to
print>"); The speechmarks indicate that you want to print a string of text.
This means that java will print exactly what is in the speechmarks.
Rounding Off
Ok thats all for this time. I hope you've been able to follow it. The next
tutorial will be on getting user input and probably types of variable but lets
worry about that next time!! If in the meantime you have any problems you can
contact me @ graeme.kerry@btinternet.com and I
will try to answer your questions.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|