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.

Slides:



Advertisements
Similar presentations
Effective Java, Chapter 3: Methods Common to All Objects.
Advertisements

1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 TCSS 143, Autumn 2004 Lecture Notes Creating Java Classes.
Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Java Type System and Object Model Horstmann ch , 7.7.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Objects Real and Java COMP T1 3
Chapter 2 Variables.
Searching.
Data Structures and Algorithms revision
Object-oriented Programming in Java
Announcements Quiz 1 Posted on blackboard
Lecture 17: Polymorphism (Part II)
CS230 Tutorial Week 3.
Primitive Data, Variables, Loops (Maybe)
COMP 110 Some notes on inheritance, review
Non-static classes.
Testing, cont., Equality and Identity
null, true, and false are also reserved.
Building Java Programs
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Chapter 2 Variables.
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Overriding methods from the Object class: equals, toString
Lecture 18: Polymorphism (Part II)
Searching.
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Special instance methods: toString
Building Java Programs
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Chapter 2 Variables.
design OO words Debug Variables Data types
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Review for Midterm 3.
Building Java Programs
Chapter 7 Java Object Model
Presentation transcript:

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 class Person { private String name = null; private int age = 0; Each Person object stores a string and an integer.

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 = ; //// degrees Celsius

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 = ; //// degrees Celsius

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.

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

(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; }

(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; }

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).

= 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);

= 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.

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

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

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

== 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

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.

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

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?

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; }

When not to compare values- contacts Compare all relevant fields e.g. A personalContacts class, contains name and address are the same, is same same person 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

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!!!

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?

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!!!!

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

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

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

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

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

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

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

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

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

Think like a compiler. What does the following print. System.out.println( "three" ); 3three45

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

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; }

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

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

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

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

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

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

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

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