Lecture 10 Slides
Download
Report
Transcript Lecture 10 Slides
Programming for Beginners
Lecture 10: Constructors; The String Class Revisited
Martin Nelson
Elizabeth FitzGerald
Revision of session 9
Arrays are useful for storing large amount of data.
To create an array of ints:
int[] myFavouriteNumbers = new int[4];
This array can hold 4 integers.
Elements are indexed from 0:
myFavouriteNumbers[0]=13;
myFavouriteNumbers[1]=42;
This will give an error:
myFavouriteNumbers[4]=28;
Why?
Session 10 – aims & objectives
Introduction to constructors
Useful for creating objects more efficiently.
Using constructors to create Strings
Constructors – 1
A constructor is a special method that is invoked when an
instance of a class is created.
All classes have constructors, whether you define one or
not.
Java automatically creates a default constructor if you
don't include one.
Once you define your own constructor, the default one is
no longer used.
The constructor is responsible for creating the object in
the computer’s memory.
Constructors – 2
The constructor always has the same name as the name
of the class.
It is the only method which does not have a return type.
The constructor is run every time you use the keyword
‘new’.
Can only run the constructor once for each object.
The constructor can be useful to initialise object variables
Could even use it set default values.
This helps keep variable declarations and assignments
separate
Good programming style, less code!
An example of a constructor
class Car
{
String colour;
int doors;
Car( )
{
colour = “silver”;
doors = 2;
}
}
Constructor:
contains
default values
for colour and
doors variables
Constructors – 3
Constructors can also take in arguments just like normal
methods.
Arguments are declared in brackets after the constructor
name.
These arguments are passed to the constructor and can
be used in the class containing the constructor.
Remember: Constructors do not have a return type!
Example: Defining object variables
class Car
{
String colour;
int doors;
Car(String c, int d)
{
colour = c;
doors = d;
}
}
Example: Defining object variables
With the appropriate constructor defined, we can now
create objects like this:
Car fiesta = new Car(“silver”,5);
Code is now much more compact!
Constructors – 4
Classes can have more than one constructor, provided
that the each receive arguments of different types.
The argument list allows the compiler to decide which
constructor you wish to use, without you having to do any
extra work!
Take a look at the Core Class Documentation for the
String class:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
The String class has 11 constructors!
The String class revisted
String has 11 constructors: we have at least 11 ways of
creating a String… actually in this case there are 12!
Method 1: Treat String as a literal variable…
String name = “Martin”;
Method 2: Pass the contents of the String to the
constructor…
String name = new String(“Liz”);
The String class revisted
Method 3: Pass the constructor a char array…
char[] letters = new char[3];
letters[0]=‘L’;
letters[1]=‘i’;
letters[2]=‘z’;
String name = new String(letters);
There are other ways too! See the documentation…
Coming up in Session 11...
Reading data from files.
Writing data to files.
Error handling.
What happens if you have an error in your code?
In many languages, the code just crashes or starts behaving
wrongly.
Java can help you detect errors and handle them properly!