Introduction
to Unit 5
The unit introduces
two important Java programming concepts:
-
arrays
-
string objects ?
instances of the String class
As
with the previous unit, the concepts in this unit are not all explicitly
about object-oriented programming. However, such concepts as arrays of
objects, and objects of the String class illustrate how the concepts introduced
in this unit are important for working with different kinds of non-primitive
variable types, and also for the implementation of methods that process
each member of an array.
The
sequence of the topics in this unit is by design ?you will understand
strings much easier if you have grasped the concepts of Java character
arrays first.
Both
of these concepts are related, since the words (and the spaces between
them) below could be represented either as an array or an instance of the
String class:
this is a
sequence of characters
As
you will discover working through this unit, we could declare an array
of characters and initialise this array variable as follows:
char sentence
= {'t', 'h', 'i', 's', ' ', 'i', 's', ... }
Or
we could declare and initialise an instance of the String class to contain
the sentence as follows:
String sentence
= "this is a sequence of characters";
As
can be seen above, arrays are designed around the individual values stored
in them (i.e. each character is treated separately), while a String object
stores and processes complete sequence of characters. However, String
objects can only be used with characters, while arrays can
be used to store organised collections of any type of value (character,
objects, primitive types).
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|