Special Effects and Game Development
in Java(TM) - Other important elements in Java
by Anibal Wainstein
1.4 Other important elements
I will briefly outline the remaining things necessary before
we can begin programming programs.
The built-in string class String is, according to me, one
of Java language's most important classes because it exists
in nearly all the Java programs. A string can be defined in
the following way:
String myfirstname = new String("Anibal");
String myfirstname = "Anibal";
The form above follows the general rule for creating new objects
(see section 1.3.3). The other way and
also the most used is that you directly set the variable to
the text value. This can only be done with the String class.
This is a trick that the compilator recognizes in order to simplify
the use of the String, because of its extensive use. When you
have defined a string you can do a lot with it. Among other
things, you can merge strings by using the plus operator "+".
String myfirstname = "Anibal";
String myfamilyname = "Wainstein";
String myfullname = myfirstname + myfamilyname;
The string "myfullname" will contain "AnibalWainstein".
It does not look right so we will add a blank space between
the first name and the family name:
myfullname = myfirstname + " " + myfamilyname;
Now you will obtain the right content, "Anibal Wainstein".
As we said before, a String is an object that also includes
methods which could help you handle strings. A String-method
that we will use a lot is indexOf() which you can use to find
out if a part of a phrase contains another phrase or word:
int index = myfullname.indexOf("stein");
Here we declare an integer where the method indexOf() will store
the result of the string search. The result will be 11 because
the phrase "stein" begins at the eleventh position
in the String. If "stein" did not exist in the string
then the result would have been -1 which means that the search
did not find the phrase. Another String method that we will
use a great deals substring() which can be used to fetch a
phrase from a string:
String firstname = myfullname.substring(0,6);
The variable "firstname" will contain the string
"Anibal" which was fetched from "myfullname".
Note that we have specified the start index 0 ("A")
and the end index 6 (the blankspace " ") for the
string which will be fetched. The end index 6 is exclusive,
which means that the letter in the sixth position (the blankspace
" ") will not be included.
1.4.2 Logical operators (Boolean
A boolean variable contains the values "true"
or "false". It can be declared as follow:
boolean isfine = true;
boolean isfine = false;
There are also operators that work with boolean variables.
One of them is the AND operator (&&). It works as
follows. Assume that we have two boolean variables "a"
boolean a = true;
boolean b = false;
boolean result = a && b;
The variable "result" is only true if "a"
is true and "b" is true (In this example the result
is false). Another important operator is the OR operator (||):
boolean result = a || b;
Here "result" is true if and only if one of the variables
"a" or "b" are true.
An operator that generates (returns) a boolean result is
the "equals" operator (= =). Assume that we have
two integers :
int x = 100;
int y = 101;
boolean result = (a == b);
The variable "result" is true if and only if "x"
has the exact same number as "b" or the reverse. The
"equals" operator is actually one of several comparison
operators such as "less than" (<), "greater
than" (>), "greater or equal to" (>=) and
"less or equal to" (<=).
Next Page >>