What do you call the program that runs Java byte-code instructions
interpreter
scanner take input from text file
Scanner fileIn = new Scanner(new FileInputStream(“data.txt”)
Programmers of robots need to ensure?
the robot does not harm humans
the robot obeys orders
the robot protects itself
moral agency
Moral agency requires that all three criteria of causality, knowledge and
choice apply to the agent/person involved
Categorical Imperative
fundamental principle behin deontology
static variable
variable that belongs to the class as a whole, and not just to one object
▪ There is only one copy of a static variable per class, unlike instance variables where each object has its own copy
▪ All objects of the class can read and change a static variable
▪ Although a static method (discussed shortly) cannot access an instance variable, a static method can access a static variable
▪ A static variable is declared like an instance variable, with the addition of the modifier static
static variable should always be defined private, unless it is also a
defined constant
value cannot be altered
In addition to static, the declaration for a static defined constant must include modifier final, which indicates that its value cannot be changed
▪ When referring to such a defined constant outside its class, use the name of its class in place of a calling object
Static variables in base class are inherited by any of its derived classes
data type default values
boolean static variables are initialized to false
Other primitive types static variables are initialized to the zero of their type
▪ Class type static variables are initialized to null
static methods
one that can be used without a calling object
▪ A static method still belongs to a class, and its definition is given inside the class definition
Static methods are invoked using the class name in place of a calling
object
static method cannot refer to an instance variable of the
class, and it cannot invoke a nonstatic method of the class
has no this, so it cannot use an instance variable or
method that has an implicit or explicit this for a calling object
wrapper classes
provide a class type corresponding to each of the primitive types
▪ This makes it possible to have class types that behave somewhat like
primitive types
▪ The wrapper classes for the primitive types byte, short, long,
float, double, and char are (in order) Byte, Short, Long, Float, Double, and Character
▪ Wrapper classes also contain a number of useful predefined
constants and static methods
have static methods to convert: eg Integer.parseInt , Double.toString, Character.toUpperCase(char)
boxing
process of going from a value of a primitive type to an object of its wrapper class
▪ To convert a primitive value to an “equivalent” class type value, create
an object of the corresponding wrapper class using the primitive value as an argument
▪ The new object will contain an instance variable that stores a copy of the primitive value
▪ Unlike most other classes, a wrapper class does not have a no-
argument constructor
unboxing
process of going from an object of a wrapper class to the corresponding value of a primitive type
▪ The methods for converting an object from the wrapper classes Byte, Short, Integer, Long, Float, Double, and Character to their
corresponding primitive type are (in order) byteValue, shortValue,
intValue, longValue, floatValue, doubleValue, and charValue
▪ None of these methods take an argument
int i = integerObject.intValue()
secondary vs main memory
2nd: hold files for permanent storage
main: used by computer program when it is running a program, keeps values stored in program’s variables
bytes
long list of numbers for location
contains 8 bits
data item can be stored in byes
identified by a number called its address, used to find the data when its needed
memory location
entire chunk of memory that holds data
address of the first byte of this memory location is used as the
address for the data item
storing primative variables
When the variable is a primitive type, the value of the variable
is stored in the memory location assigned to the variable
Each primitive type always require the same amount of memory to
store its values
storing class type variables
When the variable is a class type, only the memory address (or
reference) where its object is located is stored in the memory location
assigned to the variable
The object named by the variable is stored in some other location in memory
value of class variable is a fixed size
the value of a class variable is a memory address or reference
object can be any size
references
Two reference variables can contain the same reference, and
therefore name the same object
The assignment operator sets the reference (memory address) of one
class type variable equal to that of another
Any change to the object named by one of theses variables will
produce a change to the object named by the other variable, since
they are the same object
variable2 = variable1;
paramters gen
call-by-value parameters
▪ A parameter is a local variable that is set equal to the value of its
argument
▪ Therefore, any change to the value of the parameter cannot change
the value of its argument
class parameters
value plugged into a class type parameter is a reference
(memory address)
▪ Therefore, the parameter becomes another name for the argument
▪ Any change made to the object named by the parameter (i.e.,
changes made to the values of its instance variables) will be made
to the object named by the argument, because they are the same
object
▪ Note that, because it still is a call-by-value parameter, any change
made to the class type parameter itself (i.e., its address) will not
change its argument (the reference or memory address)
primitive vs class type parameters
A method cannot change the value of a variable of a primitive type that is an argument to the method
▪ In contrast, a method can change the values of the instance variables of a class type that is an argument to the method
== vs equals
== checks that two class type variables have the same memory addres
Two objects in two different locations whose instance variables have exactly the same values
would still test as being “not equal”
defensive copying
The private modifier only prevents direct field access—it doesn’t prevent indirect modification through shared object references
this.birthDate = new Date(birthDate.getTime());
use copy rather then actual reference which can create data leaks
copy constructor
constructor with a single argument of the same type as the class
should create an object that is a
separate, independent object, but with the instance variables
set so that it is an exact copy of the argument object
creates completely new and independent copy
check null then if not assign variables from parameter to this
immutable class
A class that contains no methods (other than constructors) that
change any of the data in an object of the class is called an
immutable class
Objects of such a class are called immutable objects
safe to return a reference to an immutable object
object cannot be changed in any way
the contents of an immutable object cannot be modified.
immutable object contains no mutator methods.