Understanding
Your First Program
Hello
World Application Explained
Hello
World Applet Explained
Comparing
the HelloWorld Application and Applet
Hello
World Application Explained
Lets look at
the code for Hello World
/* The HelloWorld class implements an application that
simply displays "Hello World!" to the standard output.
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello
World!");
}
}
|
Now let us
examine each line
/* The HelloWorld class implements an application that
simply displays "Hello World!" to the standard output.
*/
|
By convention we always give a
brief explanation of our class at the beginning.
The /* tells the java compiler that the lines following
are to be ignored and are treated as comments.
The */ tells the Java compiler that we have finished our
comment lines |
|
public
class HelloWorld |
All java programs are written in
sets of one or more classes.
Each class is saved to it's own file
We will ignore the keyword public
for now
The keyword
class marks the beginning of our class definition.
HelloWorld is the name
of this class.
The first letter of your class name should be in capitals.
When saving the source code the file name must be the same as the
class name and remember, Java is case sensitive. So we would save
this file as HelloWorld.java |
|
{ |
The curly brace indicates the
beginning of our class.
In Java, the { curly
brace indicates the start of a block of code.
The } curly brace
indicates the end of a block of code. |
|
public static void
main { |
We will ignore the keyword public,
static and void
for now.
The keyword main is
the name of a method. All
Java applications must have at least one main
method amongst it's
set of classes.
When Java executes this class it executes the main
method first.
If we didn't have a main method
then Java would not know where to start executing our lines of code.
Notice the curly brace again. This indicates the beginning of
our main method
block of code. |
|
(String[]
args) |
This specifies any parameters
being passed to the main method.
We are going to ignore this for now, but we must always type this in. |
|
System.out.println |
This term sends output to the
screen. You must use a capital S for System
or you will get an error. |
|
("Hello
World!") |
The string we which to display
to screen is enclosed within parenthesis ( )
You will note that " marks surrounding the text Hello
World! This is a way of specifying a string. |
|
; |
The semicolon ends the System.out.println("Hello
World!") statement.
Most lines in Java end with a semicolon.
If you forget the semicolon you will get an error |
|
} |
The curly brace indicates the
end of our main method. |
|
} |
The final curly brace indicates
the end of our class.. |
|
Summary
-
A Java program
is put together using classes
-
A class
is a structure that contains methods.
-
Methods
are blocks of code that carry out a specific functions.
-
An application
must have a main method in one of it's classes
-
The class containing
the main method should be saved to a file with the same name as the
class.
-
The main
method is the first code to be executed when your application is run.
-
The
System.out.println method is
used to output strings to the display.
Hello
World Applet Explained
Lets look at
the code for the Hello World applet
/*
The
HelloWorldApplet class implements an applet that simply displays
"Hello World!".
*/
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello
World!",0,50);
}
}
|
Now let us
examine each line
/* The
HelloWorldApplet class implements an applet that simply displays
"Hello World!".
*/
|
By convention we always give a
brief explanation of our class at the beginning.
The /* tells the java compiler that the lines following
are to be ignored and are treated as comments.
The */ tells the Java compiler that we have finished our
comment lines |
|
import
java.applet.Applet;
import java.awt.Graphics; |
Compiled classes can be held
together in packages. Java comes with lots of pre-written
classes held in packages that we can use.
To make our own applets work we must also include a pre-defined class
called Applet. The is held in the java.applet
package and the import statement
tells Java to include this class in our own applet program.
We also have to import
the java.awt.Graphics class
so we can draw the "Hello
World!" string onto
our Applet window. |
|
public
class HelloWorldApplet |
All java programs are written in
sets of one or more classes.
Each class is saved to it's own file
We will ignore the keyword public
for now
The keyword
class marks the beginning of our class definition.
HelloWorldApplet
is the name of this class.
The first letter of your class name should be in capitals.
When saving the source code the file name must be the same as the
class name and remember, Java is case sensitive. So we would save
this file as HelloWorldApplet.java |
|
extends
Applet |
We will ignore this for now.
However you must always include this when writing an applet |
|
{ |
The curly brace indicates the
beginning of our class.
In Java, the { curly
brace indicates the start of a block of code.
The } curly brace
indicates the end of a block of code. |
|
public
void paint(Graphics g) |
We will ignore the keyword public
and void for
now.
The keyword paint
is the name of a method.
The method allows us to draw onto the applet window
We will ignore the Graphics g
terms for now. |
|
{ |
The curly brace indicates the
beginning of our paint method |
|
g.drawString |
Applets have various ways of drawing, or
displaying, their content. The drawString method (part of the Graphics
class) is used to display text to the appet window. |
|
("Hello
World!",0,50) |
The drawString
method takes three arguments:
(1) the string to be displayed. In
this case the string is "Hello
World!"
(2) the x
coordinate, indicating the horizontal starting point for displaying the
string. In this case the x coordinate is
0.
(3) the y
coordinate, indicating the vertical starting point for displaying the
string (which is below the text). In this case the y
coordinate is 50. |
|
; |
The semicolon ends a
statement. Most lines in Java end with a semicolon.
If you forget the semicolon you will get an error |
|
} |
The curly brace indicates the
end of our paint method. |
|
} |
The final curly brace indicates
the end of our class. |
|
Summary
-
A Java program
is put together using classes
-
A class
is a structure that contains methods.
-
Methods
are blocks of code that carry out a specific functions.
-
An
application needs a main method so Java
has a starting point for running your program.
-
An
applet does not have a main method.
This is because applets do not start themselves, they are added to an already
running program, your browser or appletviewer.
-
An
applet uses a Paint method to draw
thing to the applet window
-
The
drawString method is used to draw
strings onto an applet window
Comparing
the HelloWorld Application & Applet
-
An
application must have a method called main. An applet does not have
a main method.
-
An applet uses a
paint method to draw to the applet window
-
The source
code for a class is saved in a .java file. This is true
for both application and applet classes.
-
Compiled source
code is saved in .class files. This is true for both application
and applet classes.
-
An extra HTML
file is needed for running applets.
That is folks!!
Now try the My
Second App exercise
|