OOP Notes: Page 1 of 3
Object Types and Strings
Lecture 2
Object Types and Strings
Introduction
You should read pages 28-33 of the course textbook Java Elements: Principles of Programming in Java by
Bailey & Bailey in conjunction with this lecture.
Aims
To distinguish between the use of primitive types and object types
To consolidate knowledge of strings and to introduce String as an object type
To introduce the concept of constructor
Review
You should already have some understanding of the main primitive types supported in Java – Boolean, byte,
short, char, int, long, float, double. You will also have used strings of characters in output statements. This
week you will be introduced to other features of Strings; you will also be introduced to object types, which are a
fundamental feature of object-oriented languages.
Primitive Types and Reference Types (Object Types)
Object types are sometimes known as reference types. The word reference indicates the fundamental difference
between an object on the one hand, and an instance of a primitive type on the other. A variable of an object type
contains an address/reference/pointer (all three terms are used) to the actual object. A variable of a primitive
type contains the actual value.
As a user of object types the main implication of this is the need to ensure that an object is created before you try
to use it. Until you have created an object, you may not use it; if you try to do so a run-time error will result. The
difference in the use of primitive and object types may be summarised as follows:
Primitive types:
1. declare variable
2. give it an initial value
3. perform operations on it
Object types:
1. declare variable
2. create object
3. perform operations on it
String Type
String is actually an object type, but as you will see it is a slightly peculiar object type.
First you should recall how we have used strings. Strings are enclosed in double quotation marks. As well as
alphabetic characters, any character may be enclosed in a string:
"This is a string".
"2002"
"1.76"
"£%$@~#"
",\n" // comma followed by end of line control character
You may declare string constants
Misurata University 2022 Department of Computer Science
OOP Notes: Page 2 of 3
Object Types and Strings
final String line " _____________________ ";
final String message = "Welcome to Java";
You may also declare String variables
String name;
String city;
String stree ;
String country”;
You may (unlike other object types) initialise strings
. name = " Nelson Mandela ";
city = “Manchester”;
street = “Oxford Road”;
country = “”; // empty string
You may concatenate strings, and also concatenate strings with values of any type:
String address = street + city;
int number = 123;
address = number + + address;
Consider the following code:
name = " Nelson Mandela ";
street = "Oxford Road";
city = "Manchester";
address = street + ",\n"+ city;
int number = 123;
address = number + ", " + address;
System.out.println (address);
Exercise 1
What would the above code output?
String Equality
You should not use the = = operator with objects of class String. String has an operator equals (see Bailey and
Bailey p. 299-301) which should be used instead. Please note that it is case sensitive.
String name1,name2,name3,name4;
name1=“William”;
name2=“Bill”;
name3=“William”;
name4 = “william”;
Exercise 2
what values would be output in the following cases?
System.out.println( name1.equals(name2));
System.out.println( name1.equals(name3));
System.out.println( name1.equalsIgnoreCase(name2));
System.out.println( name1.equalsIgnoreCase(name4));
SAQ 1
Study Bailey and Bailey pp 29-30; and explain why there is a problem with comparing Strings in Java; how may
you compare two strings so that strings which contain identical characters in the same sequence will always be
found to be equal?
Misurata University 2022 Department of Computer Science
OOP Notes: Page 3 of 3
Object Types and Strings
The Use of Constructors
In order to allocate memory to instances of reference types you must use constructors. Until an object of a
reference type has been constructed it contains the default value null. This means that the object does not exist,
or, to put it another way, that it has a null address.
You may construct Strings, although as you have already seen it is not usually necessary. If you were to use a
constructor for a string, the alternatives available are shown in the following example:
String country, name;
country = new String();
// this will allocate memory to an empty string
name = new String("Nelson Mandela");
// this will allocate memory to a string and initialise it
new is a reserved word, which indicates that you wish to create a new instance; String() is the constructor:
constructors in Java always have the same name as the class.
SAQ 2
Explain what constructors do; why are they not necessary for primitive types?
Finally consider the section in Bailey & Bailey on Random Numbers. You should note in the example on page 31
how an instance of class Random in instantiated before a random number may be obtained. You might find it
easier to follow if you separate the declaration and the object creation into 2 steps:
Random random; // declares the variable random of type Random
random = new Random(); // creates the object random; it may now be used
as shown in the amended example reproduced below:
import java.util.Random;
Public class RandomSample
{
public static void main ( String[] args)
{
Random random;
int n = 6;
int value;
random = new Random();
value = random.nextInt();
value = Math.abs(value);
value = value % n;
value ++;
System.out.println(value);
}
}
Exercise 3
What does the above example do?
Misurata University 2022 Department of Computer Science