Lecture04 Annotated
Lecture04 Annotated
int b= licensePlate.length();
if(Math.abs(a-b)<TOLERANCE){
}
Comparing variables - chars
• 16 bit unicode for all characters
• 38885 characters currently are defined with
Unicode values.
• The low 8 bits can be used for ASCII.
if('0'<'L'){
}
Comparing variables - Strings
• Remember, String variables are references.
• References CAN be compared with the
"==" operator, but the result won't be what
you expect. Why?
String name1 = new String("Lewis");
String name2 = new String("Lewis");
if(name1==name2){
//condition will be evaluated to false
}
public void changeParameters(Car c)
{
//changes the car’s speed outside this method!
c.speed=30;
}
Linking objects (Lab 3)
• A variable can hold a reference to another
object. When one object contains a variable
that refers to another object, we think of the
objects as being "linked" by the reference.
• Any variable that can contain a reference to
an object can also contain the special value
null, which refers to nowhere.
Linking objects (Lab 3)
• Example: A class designed to represent
employees at a company. Suppose that
every employee except the CEO has a
supervisor. CEO is another employee of the
company. Then the Employee class would
naturally contain an instance variable of
type Employee that points to the employee's
supervisor:
The Employee Class
class Employee {
// An object of type Employee holds data about one employee.
String name; // Name of the employee.
Employee supervisor; // The employee's supervisor.
Employee (String name, Employee supervisor)
{ this.name = name;
this.supervisor = supervisor;
}
// other methods
}
The Employee Class
class EmployeeTest{
public static void main(String[] args){
Employee emp, emp1;
emp1 = new Employee("Stacey", null);
emp = new Employee("Gavin", emp1); .
if ( emp.supervisor == null) {
System.out.println(emp.name " is the CEO!");}
else {//Since we are checking emp, this line will be printed