|
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.
|
Tutorial 3 Variables and Comments
Welcome
To an experienced coder these two topics probably don't seem to have much in
common, the reason I am going over them in this tutorial is that they are both
important concepts that I didn't want to write 2 seperate tutorials for.
Variables
Ok. Now your computer stores data as different data types for example it sees
the number value 3 in a completely different way to the sentence
"Hello World". So what is a variable. A variable is a place in your computer
where you can store information. Pretty easy so far huh? Well now comes the
important part: Each data type has a variable type associated with it. The most
relevant are listed below.
|
Variable type |
Description and Usage |
| char |
char is short for character. This type of
variable can store a single character of text. For example 'A' , 'Z' but
not "Hello" (note I used ' to mark a char this is important
and if you don't you will get error when you compile) |
| int |
int is short for integer or whole number.
Ths type of variable can be used to store whole number values.
e.g 2 , 3 , 543 , -13 but not 34.3 or "hello" |
| String |
this is a variable type that holds strings of
text. For example "Hello World" or "Goodbye" You could store a number in
this kind of value but you wouldn't be able to do any maths with it as the
computer sees it as text. (note I used " to mark a String this is
important and if you don't you will get errors when you
compile
) |
| float |
this is a variable type for storing floating
point numbers or fractional numbers. For example 16.2 or -11.654 are all
valid float variables. |
| boolean |
this is an interesting one. A boolean value can
only hold true or false (in lowercase) More on this in
the tutorial on loops. |
You should learn the data in the table above so you know
it off by heart. There are some other data types such as double which
aren't really relevant at this stage. If you want to know more about them have a
look at Sun's Java Site.
Now, on to using variables. To store information in a variable you have to do
2 things.
- Give the variable a name
- Tell your computer what data type will be stored in the variable
This process is called declaring variables. A general form of declaring a
variable is
<data type> <name> = <value>;
Pay special attention to the semi colon
at the end of the line. Each seperate statement in Java must have a semicolon
(;) at the end of it. This is VERY IMPORTANT!!!. Below are examples
of declaring variables:
char myChar = 'A'; (Creates a char named
myChar with value A)
int age = 18; (Creates an int named age
with value 18)
String name = "John"; (Creates String
named name with value John)
float g = 9.81; (Creates a float named g with
value 9.81)
boolean isMale = true;
(Creates a boolean named isMale with value true)
Once a variable has been declared you can do what you want with
it. For example print it. System.out.println(name); would print the value stored
in name to the screen. However System.out.println("name") would just print the
text "name". If the variable is of numerical type you can perform mathematical
operations on it. For example the code: age = age + 12; would add 12 to the
value stored in age. (age += 12; is a shorthand way of achieving this)
Comments
Everybody has their own programming style. This is the way in which they
layout their code, define variables etc. However as very often people other than
the programmer will want to go through the source code at a later date (to for
example change a feature or update the program). If you don't tell them how your
mind was thinking when you were programming the software then its going to make
their task 100 times more difficult!!
This is why comments come in useful. Comments are pieces of text which
explain what the code is doing. They are ignored by the computer when you
compile the program so there are no unexpected side effects. Now in Java there
are 2 ways to define a comment
//this is a comment
This is a single line comment and can hold such information such as what a
variable represents.
/*This is called a block comment * and can be
used to write extended * comments. */
This is a block comment. These are useful when writing at the top of your
program what the code does or writing above function headers exactly what the
function does and what parameters it takes.
It is important not to overcomment your code. Take a look at the following
example:
// add five to age age = age + 5;
This comment is redundant as you would expect anybody editing your code to
know how to add to a variable. Only include comments if you think it will
improve the clarity of your source code.
That's all folks!
That's it again for this tutorial. Hope you took all that in, if not email me and we'll sort something
out.
Til next time
This tutorial can be freely distributed as long as full credit is given
to the original author (Me). Copyright 2002
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.
|