On
completion of this section you will know
In the
chapter on static we mentioned that
commonly used methods are declared as static
and then put into methods where they can be directly invoked by other
applications. Java has a rich array of
such classes. As well as those classes that contains a number of static methods
it also has another set of classes that allows us to process primitive data and
text. For example to process the
primitive data type double Java has
a class called Double, which has
methods for converting text data to numeric and vice versa. It also has methods
for converting the numeric double
data type to other numeric data types such as int, float or long. Another class called Integer does the same for the primitive
int data type while the classes Long and Float does the same for the primitive data types long and float. A class called Boolean
allows us to do the same for primitive boolean
data types.
A class
called String allows us to do
extensive processing on text data.
Listing
9.1 below shows a selection of some of the methods of the String class.
Listing 9.1
|
1 |
public class Untitled4 |
|
2 |
{ |
|
3 |
public
static void main(String[] args) |
|
4 |
{ |
|
5 |
String
string; |
|
6 |
int start,
end, length; |
|
7 |
string =
"To be or not to be"; |
|
8 |
string =
string.concat("\n"); |
|
9 |
string =
string.concat("That is the question"); |
|
10 |
System.out.println(string); |
|
11 |
System.out.println(string.substring(3,12)); |
|
12 |
System.out.println(string.substring(19)); |
|
13 |
length =
string.length(); |
|
14 |
System.out.println("The length of the string is " + length); |
|
15 |
start = 0; |
|
16 |
while
(start<length) |
|
17 |
{ |
|
18 |
end =
string.indexOf(" ",start); |
|
19 |
if(end
== -1) |
|
20 |
end =
length; |
|
21 |
System.out.println(string.substring(start,end)); |
|
22 |
start =
end+1; |
|
23 |
} |
|
24 |
} |
|
25 |
} |
In line 5
we declare our String pointer,
called string. Note that the
similarity of the names does not cause any confusion since the name of the
class starts with a capital letter and the name of the variable starts with a
lower case letter. At line 7 an object
of String is created and the
sentence “To be or not to be” is assigned to it. This seems a strange way to create an object
since we stated earlier that the keyword new
is required for creating objects.
That is actually true, but Java makes a special allowance for the class String and allows us to create an
object as shown in line 7. The code in
this line could be replaced with:
string = new String( "To be or not to
be");
without
making any difference to the programme.
In line 8
we meet our first method of the class String
– the method concat. The name of
this method is an abbreviation of the word “concatenate”, which means adding
one piece of text on to another piece.
Thus in line 8 the character “\n” is added to our text. This is one of
Java’s special characters and is called the “new line” character. It is roughly
equivalent to pressing the Enter key when entering data from the keyboard, or
in other words if any other piece of text is added later on, it will appear one
line below what is already there. Line
9, of course, adds, or concantenates, “That is the question” to the text and so
when string is printed at line 10,
the output is:
To be or
not to be
That is the
question
In lines 11
and 12 we meet the method substring. Substring means a part or subset of a string.
Here we are telling the system to print a part of our substring, beginning at
character character position 3 and finishing at character position 12. thus
this line prints out “be or not”. At line 12 another version of substring prints out all of the string
from character 19 onwards – in other words it prints “That is the question”.
At lines 15
– 23 we have a while loop which
prints out the entire piece of text word by word. It does this by searching for
spaces and printing substrings of the text that lies between one space and the
next one. It uses the method indexOf
to look for the space. This takes two arguments. The first is the piece of text
we wish to find – the space character in our case – and the second argument is
the position in the main text where we wish to start searching.
At the
first iteration of the loop, at line 18, the variable end is evaluated to 2, since that is the position where the first
occurrence of the space character occurs. (Remember that Java starts counting
at 0.) Thus at line 21 “To” is printed out. At line 22 start is evaluated to 3
and when the loop iterates, at line 18 indexOf
is asked to look for the space character beginning at position 3. It finds the
next occurrence at position 5 and thus at line 21 will print out “be”. It
proceeds in the same manner to print all of the other words until start reaches
a value of 31, which is the “q” of “question”. When we ask it to search for a
space character starting at this point it is not going to find it and thus indexOf will return a value of -1. This tells us the we are at the end of the
piece of text and thus we have the if
construct in lines 19-20 which changes the value of end from -1 to that of length
so that we can print the final word of the sentence.
Another
method of the class String that we shall be making good use of subsequently is trim.
This method removes leading or trailing spaces from a piece of text. In
listing 9.2 below we demonstrate its use.
At line 6 an object string is
created containing the word Hello,
which is preceded and succeeded by three spaces. Thus at line 7 the value printed out should
be 11. At line 8, however, we use the trim method which returns another
object which contains the original text but have the leading and trailing
spaces removed. Thus the output of line
8 would be 5.
Listing 9.2
|
1 |
public class Untitled5 |
|
2 |
{ |
|
3 |
public
static void main(String[] args) |
|
4 |
{ |
|
5 |
String
string; |
|
6 |
string =
" Hello "; |
|
7 |
System.out.println(string.length()); |
|
8 |
System.out.println(string.trim().length()); |
|
9 |
} |
|
10 |
} |
The class
Double complements the primitive data
type double. It contains a number
of methods for converting between text data and the primitive double type
and vice versa. It also contains methods for converting between the primitive
double type and other primitive numeric
data types. In our case here we shall
only look at its methods for converting between the text data and primitive
double type.
We first
look at converting from text to numeric in Listing 9.3 below.
Listing 9.3
|
1 |
public class Untitled6 |
|
2 |
{ |
|
3 |
public
static void main(String[] args) |
|
4 |
{ |
|
5 |
String
sFirst, sSecond; |
|
6 |
double
first, second; |
|
7 |
sFirst =
"12"; |
|
8 |
sSecond =
"6"; |
|
9 |
first =
Double.parseDouble(sFirst); |
|
10 |
second =
Double.parseDouble(sSecond); |
|
11 |
System.out.println(sFirst + sSecond); |
|
12 |
System.out.println(first + second); |
|
13 |
} |
|
14 |
} |
Here we have
two String variables declared at line 5 and two double variables declared at line 6. At lines 7 and 8 we create String objects which contains the
values “12” and “6”. At line 9 we use
the static method parseDouble of the class Double to convert the value of the
first String into the primitive
double type and store the result in first.
A similar action takes place at line 10.
At lines 11
and 12 we compare the different outputs that text data and numeric data
produces. At line 11 we are printing
text data and thus the “+” sign simply meant to concatentate the two pieces of
text stored in sFirst and sSecond and will thus give “126” as an
output. This is not one hundred and twenty six, it is simply the two pieces of text
“12” and “6” joined together. At line 12
we are printing out numeric data and thus the “+” sign means addition. We
therefore get an output of 18 from this line.
We look at going in the opposite direction
with Listing 9.4 below.
Listing 9.4
|
1 |
public class Untitled7 |
|
2 |
{ |
|
3 |
public static
void main(String[] args) |
|
4 |
{ |
|
5 |
String
sFirst, sSecond; |
|
6 |
double
first, second; |
|
7 |
first =
12; |
|
8 |
second =
6; |
|
9 |
sFirst =
Double.toString(first); |
|
10 |
sSecond =
Double.toString(second); |
|
11 |
System.out.println(sFirst + sSecond); |
|
12 |
System.out.println(first + second); |
|
13 |
} |
|
14 |
} |
Here in
lines 7 and 8 the numeric variables are given their values. In line 9 the static method toString of the class Double
is used to convert the value of the numeric data first into text and stored in sFirst. A similar action
occurs at line 10.
Listings
9.3 and 9.4 may appear vary silly to us and as they are look fairly
useless. However we will shortly
encounter a number of situations where all data may come to us in text format
and the numeric components of that have to be converted into their appropriate
data types. Conversely, when we wish to output data to the screen in any more
sophisticated format than we have been doing it up to now, it has to be
outputted in text format only – consequently the need for being able to convert
numeric data to text.
Finally
we shall examine the Boolean class in Listing 9.5
Listing 9.5
|
1 |
public class Untitled8 |
|
2 |
{ |
|
3 |
public
static void main(String[] args) |
|
4 |
{ |
|
5 |
boolean b; |
|
6 |
Boolean
sB; |
|
7 |
sB = new
Boolean("true"); |
|
8 |
b =
sB.booleanValue(); |
|
9 |
System.out.println(b); |
|
10 |
} |
|
11 |
} |
This class
uses a slightly different way of converting text data to boolean. At line 7 we create an object of the class Boolean and pass the text “true” to its
constructor. At line 8 the method booleanValue() is used to return the primitive boolean data type from the object and
store it in the variable b.