CS2514
Introduction to Java
Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork
Primitive types and reference types
- A variable may have one of Java's primitive types:
- these are types that are built into the language
int, double, boolean
(and long, short, byte,
char, float)
- these variables hold values that have direct machine representations
- Or, a variable may have a reference type:
- with one exception, they are not built into the language: each class
that a programmer defines gives us one such type, e.g.
String, Scanner, Random,
Dog,...
- the one that is built-in is arrays
- to create a 'value' belonging to a reference type, you use
new
(except when exploiting the short-cut for String)
- variables of these types hold either
null or
references to objects (i.e. 'pointers' or
memory addresses)
They hold different kinds of things
- Primitive type variables:
int daysInWeek = 7;
double pi = 3.142;
- Reference type variables:
String s1 = null;
Dog d1 = null;
String s2 = "Hi";
Dog d2 = new Dog("Nobby", 3, "Norfolk Long Pig");
- Q: In fact, the last diagram is simplified. In what way?
Arrays of reference types
Dog[] my_sled_team = null;
my_sled_team = new Dog[3];
my_sled_team[0] = new Dog("Charles", 2, "Charlywawa");
my_sled_team[1] = new Dog("Charlene", 7, "Charlywawa");
Printing the contents of a variable
- If there were no
toString method, the following would display
a long hexadecimal number:
Dog d = new Dog("Nobby", 3, "Norfolk Long Pig");
System.out.println(d);
Q: What is this number?
- So it's a good idea to include a
toString method in every class
definition (like we did in Dog and Card), e.g.
public String toString() {
return name + " is a " + age + " year old " + breed;
}
print and println call toString.
So these are equivalent:
System.out.println(d.toString());
System.out.println(d);
Assignment copies contents
int x = 10;
int y = x;
–
x contains 10, so a copy of this value is put into
y
Dog myDog = new Dog("Nobby", 3, "Norfolk Long Pig");
Dog yourDog = myDog;
–
myDog contains a reference, so a copy of this reference
is put into
yourDog, giving two references to the same
object (not two objects)
Consequences
- Mostly this causes no problems. But sometimes it does...
- E.g. your sled team contains three six-year old Darth Vaders
called Tic, Tac and Toe.
- You decide to create it
this way:
Dog[] team = new Dog[3];
team[0] = new Dog("Tic", 6, "Darth Vader");
team[1] = team[0];
team[2] = team[0];
team[1].setName("Tac");
team[2].setName("Toe");
instead of this way:
Dog[] team = new Dog[3];
team[0] = new Dog("Tic", 6, "Darth Vader");
team[1] = new Dog("Tac", 6, "Darth Vader");
team[2] = new Dog("Toe", 6, "Darth Vader");
- Q: Why is the former wrong?
- We will see that many class definitions contain a
clone method,
specially designed to copy objects in a proper way
Equality testing using == compares contents
int x = 10;
int y = x;
int z = 10;
if (x == y) {
System.out.println("Yes, x == y");
}
if (x == z ) {
System.out.println("Yes, x == z");
}
String s1 = "Hi";
String s2 = s1;
String s3 = "Hi";
String s4 = new String("Hi");
if (s1 == s2) {
System.out.println("Yes, s1 == s2");
}
if (s1 == s3) {
System.out.println("Yes, s1 == s3");
}
if (s1 == s4) {
System.out.println("Yes, s1 == s4");
}
Dog d1 = new Dog("Nobby", 3, "Norfolk Long Pig");
Dog d2 = d1;
Dog d3 = new Dog("Nobby", 3, "Norfolk Long Pig");
if (d1 == d2) {
System.out.println("Yes, d1 == d2");
}
if (d1 == d3) {
System.out.println("Yes, d1 == d3");
}
Using a method instead of ==
- Since
== doesn't do what we want/expect, the interface of
the Java String class definition includes an equals
method
- So don't write this:
if (s1 == s2) ...
which is true if s1 and s2 contain
references to the same String object
- Write this instead:
if (s1.equals(s2)) ...
which returns true if s1 and s2 refer to
String objects that contain the same characters
Defining an equals method
Summary
| Primitive types | Reference types |
| Examples |
int, double, boolean |
arrays, String, Scanner, Dog |
| Contents |
| Contains a value |
Contains null or a reference ('pointer') to an object |
System.out.println(...) |
| Displays the value |
Runs toString. By default, toString displays
the pointer. But you can supply your own definition to display
something more meaningful
|
| Assignment |
| Copies the value |
Copies the reference (hence two pointers to the same object) |
| Equality testing |
== tests for equal values |
== tests for equal references (pointing to the
same object
- define your own
equals method to compare the
objects' instance variables
|
| Passing a parameter to a method |
| Copies the value, so changes made in the method do not affect the original |
Copies the reference (hence two pointers to the same object), so changes
to the object made in the method do affect the original
|
Advanced stuff: Python
- Python does not distinguish primitive types and reference types
- In Python, everything is an object
x = None
y = 3
z = "Hi"
- You might like to investigate the difference between
== and
is in Python