Week 3 Notes and Questions
- There are two types of data in Java: primitive and objects.
- primitive : int, boolean, char, byte, short, long, float, double, void
All these datatypes have defined size and range which are the same for
all JVMs.
- objects : an aggregate of primitives
Object is an instance of a class; a class definition includes a list of
data members, each of which is either a primitive or a class.
- When you declare a variable in Java, it will be either a "primitive"
variable or a *reference* variable. For example:
int x; // x is an integer which you can assign values to
String s; // s is a *reference* to a "String" object
- All variables (primitives and references) exist from the time that you
declare them until the end of the scope.
- A reference has a lot of similarities to a pointer in C, but it can be
unhelpful to think of it this way. A reference can either be "null", or it
can refer to an object.
- Primitive variables "contain" their value; you can never have a variable
that "contains" an object, only variables that "refer" to objects.
- Assignment: for a primitive, copies the data:
int x;
int y;
...
x = 1;
y = 1; // x and y now both contain the value 1
- For a reference, the data isn't copied
String s1;
String s2;
s1 = "hello";
s2 = s1; // s1 and s2 now both refer to the same "hello"
- What happens here?
s1 = "hello";
s2 = s1;
s1 = "goodbye";
// what data does s2 now refer to?
Answer: s2 is still "hello". When we assign "goodbye" to s1, we are NOT
changing the memory that s1 points at, we're making it point to a new piece
of memory. This is a very important distinction.
- To create a new object, use the "new" operator, e.g.:
Date myDate = new Date(2003,5,30);
Q: how long does the variable "myDate" last?
A: until it goes out of scope
Q: how long does the date object we created last?
A: not defined, but at least as long as you still have references to it.
- methods have argument lists - each argument will either be a primitive or a
reference. E.g.
void showPerson(String name, int age)
All arguments to functions are passed by value.
For primitives, the value of the data is copied. So "age" will contain
the same value as the integer supplied by the caller.
For references, the value of the reference is copied. So "name" will
the same value as the *reference* supplied by the caller. I.e. it will
start off pointing to the same String that the caller supplied.
Since all arguments are passed this way:
- passing arguments is very cheap: you never have to copy huge data
structures around. You only copy primitives or references, unlike C and
C++ where the compiler may need to create a complete copy of the
parameter (e.g. C++ copy-constructor)
- Despite the name, this is *NOT* the same as C++ "references" or Pascal
"pass by reference" in which the called function can modify the values of
the caller's variables
- How do references mount up? Partly because you call functions and pass
references to those functions; mainly because new objects get created that
contain references to other objects.
Questions for this week :
- In the following code:
String a;
String b;
String c;
String d;
a = "hello";
b = a;
c = a;
d = b; // 1
a = d; // 2
Q: How many references to "hello" are there after we've executed line "1"?
Q: What effect does line "2" have?
Q: How could you make a,b,c,d *all* contain a reference to the string
"goodbye"?
- write a program which has a function like this:
void changeArgs(String name, int val)
{
name = "a silly name";
val = -1;
System.out.println("I changed the args to : " + name + ", " + val);
}
and call the function using, e.g.
String myName = "nick";
int myAge = 21;
System.out.println("I am " + myName + ", my age is " + myAge); // 1
changeArgs(myName, myAge);
System.out.println("I am " + myName + ", my age is " + myAge); // 2
Q: Explain why "myName" and "myAge" are unchanged by the time you get to
line "2"
Q: Can you alter the "changeArgs" function to make line "2" show myAge is
-1?
Q: Can you alter the "changeArgs" function to make line "2" show myName is
"a silly name"?
- Change the definition line of "changeArgs" to:
void changeArgs(final String name, int val)
Q: What difference does this make?
Q: What practical use would this feature be?
- For the following code, how much memory will be used by the object
"myThing"?
class Thing {
int x;
int y;
char c;
String s;
java.util.Date d;
public Thing(String a) { s = a; d = new java.util.Date(); }
}
...
Thing myThing = new Thing("hello"); // How much memory does myThing use
// up?
Back to index page