rather object

advertisement
Big Java Chapter 2
Types and variables (chapter 2.1-2.2)
What is a variable?
 From our perspective; a “box” in which to store data
 We can also invoke operations on a variable, depending on its type
 + - * on integers and doubles
 Call methods on variables with a more advanced type (a class)
 Syntax:
typeName variableName;
 or
typeName variableName = value;
names for variables
Rules:
 Can include letters, number underscore (_) and $
 Cannot start with a digit
 No spaces allowed
 No use of reserved words
 Names are case sensitive
Recommendations:
 Name should describe purpose of variable
 Name should start with lowercase letter
 Long names should be made more readable using camelCase
 Add a comment to the variable if needed
Assigment to variables
 Changing the value of a variable is done using the “=” operator
 This is different than the mathematical use of “=”
 “=” is an action, not a comparison
 We can use “=” for many other types than just numerical types
 ALWAYS initialise a variable before using it!!
Big Java Chapter 2
Objects, Classes and Methods (chapter 2.3)
 A variable can be of a simple (primitive) type, like int or double
 A variable can also be a reference to an object, which has a type
defined by some specific class
 A class is a type definition, so we build objects according to a class
definition
 A class definition usually contains a number of methods
 A method contains a sequence of instructions to execute, when the
method is called/invoked
 We define methods on classes
 We invoke methods on objects
 A method has a name, a set of parameters, and a return type
 The set of “public” methods define the interface to the class
 Interface is the way users interact with objects of this particular type
 A class may also define a number of variables, if we need to
maintain a “state” in objects of this type
 Usually, we do not allow users direct access to any variables in the
class – this in encapsulation
 Encapsulation allows us to change the way a state is stored – and
the way a method is implemented – without bothering the user
 This does however only work if we do not change the interface!!
 The notation for invoking a method on an object is the “dot”
notation, like adderObject.add(2,5);
 If we type a “.” after an object in NetBeans, the editor will usually
show us a list of methods that we can call on that particular object
Big Java Chapter 2
Method parameters and return values (chapter 2.4)
 A method has a name, a set of parameters, and a return type
 The set of parameters will contain data that the method will need in
order to perform its function – no reason to provide parameters that
are never used
 When we call a method, the parameters we provide to the method
must match the definition of the method, i.e. the type of each
parameter must be correct
 It is however fine to provide an expression, which evaluates to the
correct type, like add(1+5,6+8);
 The expression may also be another method call, which returns a
value of the correct type, like add(add(4,5),add(6,8));
 These parameters are called explicit parameters
 Opposed to this is the implicit parameter, which is the object itself
on which we invoke the method (example Figure 6, pp 41)
 A method call can return a value, the type of which can be seen from
the method definition, like int add(int first, int second);
 This a why we can use a method call as a parameter, if the return
type matches the type the called method expects
 Some methods do however not return a value; they are said to return
a special “value” called void (void means “nothing” or “empty”)
 We may often need to do the same operation on objects of many
different types, like invoking a println(…) method for integers,
strings, etc.
 Instead of defining methods with different names for each type of
possible parameter, we can define multiple instances of methods with
the same name, but with different parameters (overloading)
Big Java Chapter 2
Object construction, method types (chapter 2.6-2.7)
 Objects are built according to a specification, given by a class
definition
 Syntax for building an object is: new ClassName(parameters);
 For instance, if we have defined a class Adder, we create a new
object of the type Adder like new Adder();
 In this case, no parameters – depends on definition
 Every class has a special method called a constructor – this method
is invoked when the statement like new Adder(); is called
 The name of the constructor method is always the same as the name
of the class itself
 The constructor itself has no return type, but the statement new
ClassName(…) returns a object of type ClassName
 The constructor can be overloaded – more versions with different
parameter sets
 Why do we need constructors?
 If the object has some kind of state, we can set the initial state of the
object during construction, i.e. when the constructor runs
 No reason for multiple constructors if the method is stateless, i.e.
does not contain any data





Methods are generally divided into accessor and mutator methods
Accessor methods do not change the state of the object
Mutator methods can change the state of the object
A constructor can thus be a mutator method
Often we hide the access to the variables in an object behind accessor
and mutator methods, often called get(…) and set(…)
Big Java Chapter 2
Object references (chapter 2.10)
 When we create a new object of some type, the syntax looks like:
Adder myAdder = new Adder();
 The variable myAdder does actually not have the type Adder; it is a
reference to an object of type Adder
 What is an object from the perspective of the computer? Just a piece
of allocated memory
 The variable itself just contains the address of the memory location
where the object is stored (drawing, also Figure 16)
 Why this difference? An object might take up quite a lot of memory,
and it is much more efficient to use the memory address as e.g. a
parameter to a method, than use the object itself
 A very important consequence of this fact concerns assignment of
objects, or rather, object references
 If we set one object reference variable equal to another object
reference variable, they will point to the same object!!
 This means that if I change the object through one object reference,
the result can also be seen through the other object reference
(drawing, also Figure 19 + 20)
 Object assignment works differently than number assignment!!
 Do you have to think much about this in practice? No, not really
 But is important if you have multiple object references pointing to
the same object
 What do I do if I really want a true copy of an existing object? Then
you have to make a brand new object, and copy the state from the
existing object into the new object
 Just copying the reference is often called shallow copying
 Copying the entire object is often called deep copying
Big Java Chapter 2
The Java library (chapter 2.9)
 Now that we can use objects, what classes are then available to use?
 The Java Library contains thousands of classes which are ready to
use – how can I get to know them?
 Impossible to memorise – do not even try!
 Use the documentation!
 Where do I find the documentation? Use the link found in BB
 Is quite overwhelming…
 Top level is a number of packages, which in turn contains a number
of classes
 Where do I start…? Try to read through the Overview page, even
though it contains about 100 packages…
 For instance java.awt: contains classes for creating graphical user
interfaces
 Other examples
 java.util: collections, data and time, random numbers
 javax.xml: handling of XML data
 java.text: handling and manupulation of text
 …and so on
 If you need to use a library, simply import it into your code:
 import javax.swing.JFrame;
 Simply need practice in order to get a grip on the library
Big Java Chapter 2
Exercises
Chapters 2.1 – 2.2
Self check 1,2 and 4
R 2.5 and 2.6
Chapter 2.3
Self check 8
R 2.3
P 2.7
Chapter 2.4
Self check 9, 11
P 2.4
Chapter 2.6-2.7
Self check 16, 17
R 2.7, 2.8, 2.9, 2.10
Chapter 2.10
Self check 25
R 2.2
Chapter 2.9
None – but try to browser around in the documentation
Download