Java Oop2
Java Oop2
Lecture 2
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.
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 concatenate strings, and also concatenate strings with values of any type:
String address = street + city;
int number = 123;
address = number + + address;
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?
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:
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:
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?