A comment is a piece of text added to a program to make it easier to understand. The computer takes no notice of comments - they are for the benefit of the human reader only. Here is an example of a comment to explain a section of code:
// Draw a face g.setColor(Color.pink); g.fillOval(100,100,200,250); g.setColor(Color.white); g.fillOval(150,145,10,400; g.fillOval(180,145,10,400);
The comment is in the first line. It starts with two forward slashes together, //, which tells Java to ignore everything following that on the line. The comment finishes at the end of the line and then Java wakes up again and expects to find a program statement.
Comments needn't be on a line on their own. They can follow program code statements:
// Draw a face g.setColor(Color.pink); // The "body" of the face g.fillOval(100,100,200,250); g.setColor(Color.white); // The eyes g.fillOval(150,145,10,400; g.fillOval(180,145,10,400);
However, if you put a program statement after a comment on the same line, then Java will assume that it is part of the comment and ignore it:
// The body of the face g.setColor(Color.pink); g.fillOval(100,100,200,250); // Now do the eyes g.setColor(Color.white); g.fillOval(150,145,10,400; g.fillOval(180,145,10,400);
In this case, the two setColor instructions are completely ignored. You can easily have comments that spread over several lines by starting each line with the double forward slash:
// This comment contains a // message that is split // over three lines.
However, there is an easier way to include multiline comments. You can start a comment with a forward slash followed immediately by an asterisk, /* In this case, Java ignores everything that follows - however many lines it stretches across - until it reaches an asterisk followed immediately by a forward slash, */
/* This is a multiline comment. You notice that I don't have to start each line with a special symbol. Java will ignore all of this until it reaches this symbol: */
Using this second comment system means that you can now put program statements after comments on the same line as them:
g.setColor(Color.cyan); /* Draw the track */ g.drawOval(100,100,200,200);
It's not a good idea to cram too many things on one line liket that. Most programmers limit themselves to one statement per line. One warning about comments: A comment is useless if it simply duplicates information that you can get from the instruction itself. Take a look at this example:
g.drawRect(100,100,150,200); // Draw a rectangle
The comment here is pointless. You can see by looking at the program instruction that it draws a rectangle! Try to make your comments meaningful!
What is a variable? Well, it's a small piece of memory inside the computer that is set aside by your program to store an item of information. Your program reserves the memory slot (we say it declares the variable), and then the program can fill that slot with pretty much anything (there are restrictions!) and change it whenever you like. That's why they're called variables - they vary!
Think of a variable as being like a box in memory. It has a name, which is how the program refers to it, a type, which tells the program what sort of things it can hold (for instance, a float type variable can only hold decimal numbers, and a value which is the contents of the box itself.
The first type of variables that I am going to show you are String variables. These can only hold strings, which, I am sure you will recall, are sequences of characters (i.e. letters, punctuation and spaces).
When a Java program runs, it looks for several methods and runs them. We have already met one of these - paint draws the screen - but there are others. The special method init is used to initialize variables (i.e. declare them and give them an initial value). This is called in the same way as paint:
import java.awt.*;
import java.applet.*;
public class greet_me extends Applet
{
public void init ()
{ String AuthorName;
AuthorName = "Richard Bowles";
}
public void paint (Graphics g)
{ g.drawString("Hello",10,50);
g.drawString(AuthorName,50,50);
}
}
Because the program contains the class greet_me, it must be stored within a file called greet_me.java, which then compiles (hopefully) to a file called greet_me.class.
The method init is called using public void but this time it has no parameters. You still have to put in the two brackets, but there is nothing between them, so they appear as (). The statements that make up the method appear in curly brackets following the first line. The line ...
String AuthorName;
... sets up a string variable called AuthorName. This is how variables are declared - first the variable type is given and then one or more variable names (separated by commas if you have more than one) which are all set up to be of that type. Finally, as usual, there is a semicolon at the end of the line.
When the variable is declared, it doesn't yet have a value. The value should be set up, although the program can change it later. The variable is set to a particular string using an equal sign:
AuthorName = "Richard Bowles";
Because AuthorName is a string variable, it can be set to any string enclosed within double quotation marks, or any other string variable. It couldn't be set to a number:
AuthorName = 2;
Java wouldn't like that! You notice that the value in the string variable can be displayed in exactly the same way as any string between quotation marks (referred to as a "string constant"). In this case, don't put in double quotation marks. If you did, it would display the words AuthorName on the screen. Variables can be altered as many times as you like:
AuthorName = "Dopey"; g.drawString(AuthorName,100,120); AuthorName = "Grumpy"; g.drawString(AuthorName,100,140); AuthorName = "Happy"; g.drawString(AuthorName,100,160); AuthorName = "Sleepy"; g.drawString(AuthorName,100,180);
That code segment would display four different values down the screen.
Concatenation means putting strings together end to end to form a longer string. For instance, consider this program (which should be saved in the file show_concat.java):
import java.awt.*;
import java.applet.*;
public class show_concat extends Applet
{
public void init ()
{ String first;
String second;
String third;
first = "I think";
second = "therefore";
third = "I am";
}
public void paint (Graphics g)
{ g.drawString(first + second + third,10,10);
}
}
The + sign between the strings in the drawString instruction produces a string which is all the other strings joined in the order specified. This string is displayed on the screen and then forgotten about (i.e. the long string is not stored in a variable), although the individual string variables themselves do survive, and could be displayed later (or used in more concatenations). The program displays the following:
I thinkthereforeI am
Ah! Whoops! We forgot the spaces. Concatenation doesn't insert special spaces between the strings - there's no reason why it should - so we have to. We could do this in one of several ways:
g.drawString(first + " " + second + " " + third,10,10);
(N.B. There is a single space between each pair of quotation marks in that statement!) This time we are concatenating 5 strings, with three of them represented by string variables, and the other two by string constants (the spaces).
second = " therefore ";
In this case, we wouldn't need the spaces between the variables in the drawString instruction - it would simply go back to the version in the original program.
String space; space = " "; // There is a space between the quotation marks ... g.drawString(first + space + second + space + third, 10, 10);
We don't need to declare a special variable for the first space and another for the second space - the same variable will do in both cases.
These are some of the ways in which the problem could be solved. I'm sure you can think of others. Not only can string variables be concatenated, but constants can be as well:
g.drawString("Another " + "way of" + " concatenating strings!", 97, 35);
It is perfectly possible to declare a string variable and initialise it one statement. This just involves running the two statements together:
String persons_name = "Diane"; String punc = "!£$%^&";
There's nothing to stop you from re-assigning the variable to something else later, of course! This declaration + assignment applies to all sorts of variables (not just string ones), although string variables are the only ones you have met so far.
The double-quotation character (") is used to mark the beginning and end of a string constant, but what do you do if you want to include a double-quotation mark inside the string itself?
To do this, put the \ character in front of the double quotation character. The backslash does not appear in the displayed string, but is a special symbol to Java meaning "a special character is following me". In this case, it tells Java that the quotation character that follows does not mean the end of the string, but is a double quotation character that should appear in the displayed string:
g.drawString("We called him \"Honest\" Ron.", 150, 70);
produces the output:
We called him "Honest" Ron.
at position (150,70) on the screen.