Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public.

Similar presentations


Presentation on theme: "More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public."— Presentation transcript:

1 More on Objects Static and equals.

2 Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public class Person { private String name = null; private int age = 0; Each Person object stores a string and an integer.

3 static Suppose something is the same for all objects E.g. absolute zero is the same for everyone. public class Thermometer { //stores the current temperature private double currentTemperature = 0;// degrees Celsius private double ABSOLUTE_ZERO = 273.15; //// degrees Celsius

4 The keyword static – just allows one to exist. We do not want to store absolute zero for every thermometer. public class Thermometer { //this variable stores the current temperature private double currentTemperature = 0;// degrees Celsius private static double ABSOLUTE_ZERO = 273.15; //// degrees Celsius

5 Blood pressure Each person has a blood pressure e.g. 150/100 Normal blood pressure is 120/80. What is systolic and diastolic blood pressure? systolic blood pressure, is the pressure in your blood vessels when your heart beats. diastolic blood pressure, is the pressure in your blood vessels when your heart rests between beats.

6 Store normal blood pressure once private int systolic; private int diastolic; static final int NORMAL_SYSTOLIC = 120; static final int NORMAL_DIASTOLIC = 80;

7 (diastolic > NORMAL_DIASTOLIC) boolean highDiastolicPressure() { if (diastolic > NORMAL_DIASTOLIC) { System.out.println("high Diastolic Pressure"); return true; } else { System.out.println("you are not at risk"); return false; }

8 (this.systolic > Person.NORMAL_SYSTOLIC) boolean highSystolicPressure() { if (this.systolic > Person.NORMAL_SYSTOLIC) { System.out.println("high systolic Pressure"); return true; } else { System.out.println("you are not at risk"); return false; }

9 equals =, ==, equal(Object obj) mean different things. They are a source of errors to learner of java. Objects (like person, counter) behave differently to primitive data types (int, double, boolean).

10 = Think of = as e.g. “int x=5; ” as the value of variable x becomes the value 5” or “variable x is assigned the value 5” Think of person p = new person(“John”, 99); as “variable p points to the person object containing name John and age 99” What will the code below print out? int y, x = 3; System.out.println(y=x);

11 = Think of = as e.g. “int x=5; ” as the value of variable x becomes the value 5” or “variable x is assigned the value 5” Think of person p = new person(“John”, 99); as “variable p points to the person object containing name John and age 99” What will the code below print out? int y, x = 3; System.out.println(y=x); ANSWER IS 3, NOT TRUE.

12 What does this do boolean a = true; boolean b = false; if (a != b) { System.out.println("a and b are equal"); }

13 What about this do boolean a = true; boolean b = false; if (a =!b) { System.out.println("a and b are equal"); }

14 a and b are equal Instead of != (not equal), we do something else by mistake Be careful.

15 == int x, y=3; x==y means, do these variables have the same value (x and y are primitive data types). Person p1, p2; p1==p2 means do these variables point to the same object

16 equals(object obj) int x, y; if (x.equals(y)) //we cannot say this in java Person p1, p2; if(p1.equals(p2)) //this checks the contents of p1 and p2 to see if they are the same. if(p1== p2) //this checks if they are the same object.

17 Properties of equals (Think about “=“ in maths) For any non-null object reference (pointer) Reflexive: x.equals(x) is true. Symmetric: x.equals(y) and y.equals(x) return the same Transitive: if x.equals(y) and y.equals(z) then x.equals(z) Consistent: x.equals(y) returns the same value when called multiple times. x.equals(null) returns false

18 Guide to writing a good equals method If you do not override equals – an object is only equal to itself. i.e. the default is “==” Check to see if the other object is null if(obj==null) return false Maybe the superclass already has an equals method Compare all relevant values in the field (I would just compare all non-static fields). Why not compare static fields too?

19 e.g. equals in person class public boolean equals(Object object) { if (object == null) { return false; } else if (!(object instanceof Person)) { return false; } else { Person person = (Person) object; return this.name.equals(person.getName()) && this.getAge() == age; }

20 When not to compare values- contacts Compare all relevant fields e.g. A personalContacts class, contains name and address are the same, email is same JackAndJill@hotmail.com same person JackAndJill@hotmail.com You could also have notes, where you met them – these could be different) – but is it still the same contact. You need to think about what equals means in the context you are working

21 When not to compare values - cars A car hire company may rent cars. A customer does not care which instance of a car he gets (e.g. he is fussy about the make, but not the registration), but if he crashes a car he wants one of equal specification (e.g. a BMW, or a Mercedes, not a Ford or a Vauxhall) i.e. you do not compare on registration plate. The police might!!!

22 Equality of Rabbits In a comedy show – you are house sitting and the pet rabbit dies so you rush to pet store to buy a replacement. To you they are the same but to the owner they are different. The joke it of course – why?

23 Equality of Rabbits In a comedy show – you are house sitting and the pet rabbit dies so you rush to pet store to buy a replacement. To you they are the same but to the owner they are different. The joke it of course – why? The new rabbit is a different gender!!!!

24 What will the code below print? String name1 = "John"; String name2 = "John"; System.out.println(name1==name2); name1 = "Mary"; System.out.println(name1==name2); //note – do not write code like this

25 What will the code below print? String name1 = "John"; String name2 = "Mary"; System.out.println(name1 = name2); //note – do not write code like this

26 What will the code below print? String name1 = "John"; String name2 = "Mary"; System.out.println(name1 = name2); //note – do not write code like this

27 What will the code below print? String name1 = "John"; String name2 = "Mary"; System.out.println(name1 = name2); //note – do not write code like this Mary

28 Think like a compiler. What does the following print. System.out.println(1 + 2 + "three");

29 Think like a compiler. What does the following print. System.out.println(1 + 2 + "three"); 3three

30 Think like a compiler. What does the following print. System.out.println("three" + 4 + 5);

31 Think like a compiler. What does the following print. System.out.println("three" + 4 + 5); three45

32 Think like a compiler. What does the following print. System.out.println(1 + 2 + "three" + 4 + 5);

33 Think like a compiler. What does the following print. System.out.println(1 + 2 + "three" + 4 + 5); 3three45

34 Lazy and eager evaluation (also called short circuit) myfalse()&&mytrue();//lazy evaluation myfalse()&mytrue();//eager evaluation mytrue()||myfalse();//lazy evaluation mytrue()|myfalse();//eager evaluation

35 if (f() && t()) { System.out.println("1"); } if (f() & t()) { System.out.println("2"); } if (t() || f()) { System.out.println("3"); } if (t() | f()) { System.out.println("4"); } static boolean t() { System.out.println("in t()"); return true; } static boolean f() { System.out.println("in f()"); return false; }

36 if (f() && t()) { System.out.println("1"); }

37 if (f() && t()) { System.out.println("1"); } Output in f()

38 if (f() & t()) { System.out.println("2"); }

39 if (f() & t()) { System.out.println("2"); } Output in f() in t()

40 if (t() || f()) { System.out.println("3"); }

41 if (t() || f()) { System.out.println("3"); } Output in t() 3

42 if (t() | f()) { System.out.println("4"); }

43 if (t() | f()) { System.out.println("4"); } Output in t() in f() 4


Download ppt "More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public."

Similar presentations


Ads by Google