1 ssd3 - unit 2 java tostring & equals presentation 3.2.1 class website:
TRANSCRIPT
![Page 1: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/1.jpg)
1
SSD3 - Unit 2Java toString
& EqualsPresentation 3.2.1
Class Website: http://www.bscheele.com/ssd3
![Page 2: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/2.jpg)
2
Class Visibility
• We have two choices with respect to the manner in which we declare class visibility:– As public:
public class Student {// Attributes and methods ...
}
– With no explicit visibility:class Professor {
// Attributes and methods …}
• The implications of these two choices are illustrated on the diagrams that follow
![Page 3: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/3.jpg)
3
Package A Package B
public class W{ … }
class X { … }
public class Y{ … }
class Z { … }
W.java Y.java
![Page 4: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/4.jpg)
4
Package A Package B
public class W{ can create objects of type W and X, but not of Y or Z }
class X { ditto … }
public class Y{ can create objects of type Y and Z, but not of W or X }
class Z { ditto ... }
W.java Y.java
![Page 5: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/5.jpg)
5
Package A Package B
import B.*;
public class W{ can create objects of type W, X, and Y, but still not of Z }
class X { ditto … }
public class Y{ can create objects of type Y and Z, but still not of W or X }
class Z { ditto … }
W.java Y.java
![Page 6: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/6.jpg)
6
Feature Visibility, Revisited
• We learned about three types of feature visibility early on: public, private, protected
• We hadn't talked about class visibility or packages at that time, and so we oversimplified what these three forms of visibility really entail - we'll revisit that now
![Page 7: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/7.jpg)
7
Feature Visibility, Cont.
• Also, if the access type for a given feature is omitted entirely, as in the following example:
public class Person { public String name; private String ssn; protected String address; int age;
then the feature in question is said to have package visibility by default
![Page 8: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/8.jpg)
8
Feature Visibility, Cont.
• Here is the effective visibility for a given feature of public class 'A' with respect to a class 'B', depending on where the two classes live:
(Person)
![Page 9: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/9.jpg)
9
Feature Visibility, Cont.
hmmm ...
(Person)(Student)
![Page 10: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/10.jpg)
10
Feature Visibility, Cont.
hmmm ...
whoa!!!
(Person)(Student)(Course)
![Page 11: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/11.jpg)
11
Feature Visibility, Cont.
(assumingimport
of Person)
(Person) (Course)(Student)
![Page 12: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/12.jpg)
12
Feature Visibility, cont.
• For same-package classes, the bottom line is that if we don’t explicitly declare something to be private, then it essentially becomes public:
public class Person { // This first attribute is truly private. private String ssn;
// But, all the rest of these attributes are // effectively public from the perspective of
// other classes in the same package. public String name; protected String address; // Exception: subclasses cannot see the next one!
int age;
![Page 13: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/13.jpg)
13
Determining the Class that an Object Belongs To
• Another method that all classes inherit from the Object class is:
Class getClass()
• The Class class, in turn , has a method with signature:
String getName()
• Used in combination, we can use these two methods to interrogate an object to find out what class it belongs to
![Page 14: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/14.jpg)
14
Determining the Class that an Object Belongs To, cont.
Student s = new Student();Professor p = new Professor();Vector v = new Vector();v.add(s);v.add(p);for (int i = 0; i < v.size(); i++) { // Note that we are not casting the objects here! // We're pulling them out as generic objects. Object o = v.elementAt(i); System.out.println(o.getClass().getName());}
This program produces as output:Student
Professor
![Page 15: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/15.jpg)
15
Determining the Class that an Object Belongs To, cont.
• Another way to test whether a given object reference belongs to a particular class is via the instanceof operator – This is a boolean operator which allows us to determine if
some reference variable X is an object/instance of class YStudent x = new Student();
if (x instanceof Professor) … // will evaluate to false
if (x instanceof Person) … // will evaluate to true
– The classname should be the fully qualified name of the class if it belongs to a package, e.g., java.lang.String
![Page 16: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/16.jpg)
16
The toString() Method
• We mentioned earlier that all Java classes are descended from the Object class
• One of the features that all classes inherit from Object is a method with signature: public String toString();
• As inherited, however, the method may not prove to be very useful, as the following example illustrates (cont.)
![Page 17: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/17.jpg)
17
toString(), cont.
Student s1 = new Student(); s1.setName("Harvey"); System.out.println(s1); // Prints out an object ID … not too useful!
Student@71f71130
• It is a good idea to explicitly override the toString() method for any class that you invent
public String toString() { return getName(); }
// Prints "Fred" (or whatever). System.out.println(s1);
![Page 18: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/18.jpg)
18
Testing for Equality
• We've seen the use of a double equal sign (==) to test for equality of two values; e.g.,int x = 3;int y = 4;if (x == y) do something …
• We can also use == to test the "equality" of two objects:Person p1 = new Person("Joe");Person p2 = new Person("Mary");if (p1 == p2) do something ...– What does "equality" mean in the case of two object
references?– As we saw from our discussion of String as objects, the ==
operator tests to see if two references are referring to the same object
![Page 19: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/19.jpg)
19
Testing for Equality, cont.
public class Person {private String name;
// Constructor.public Person(String n) {
name = n;}
}
// Client code:Person p1 = new Person("Joe");Person p2 = p1; // Second handle on SAME object.Person p3 = new Person("Joe"); // New object, same NAME.
if (p1 == p2) System.out.println("p1 equals p2");if (p1 == p3) System.out.println("p1 equals p3");
Prints out: p1 equals p2
![Page 20: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/20.jpg)
20
The equals() Method
• Another of the features that all classes inherit from Object is a method with signature: public boolean equals();
• This method is used to test the "equality" of two objects in a different way: namely, based on whatever criteria that we establish by overriding the method – Let's look at an example
![Page 21: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/21.jpg)
21
The equals() method, cont.
public class Person {private String name;
// Constructor.public Person(String n) {
name = n;}
// Overridden from Object.public boolean equals(Object o) { boolean answer = false; try {
Person p = (Person) o; // Note cast.if (p.getName().equals(name)) answer = true;else answer = false;
} catch (ClassCastException e) {
answer = false; }
return answer;}
![Page 22: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/22.jpg)
22
The equals() method, cont.
public String getName() {return name;
}}
// Client code.
Person p1 = new Person("Joe");Person p2 = new Person("Mary");Person p3 = new Person("Joe"); // Different object, same name.Object o = new Object();
System.out.println("Does p1 equal p2? " + p1.equals(p2));System.out.println("Does p1 equal p3? " + p1.equals(p3));System.out.println("Does p1 equal o? " + p1.equals(o));
Produces as output: Does p1 equal p2? falseDoes p1 equal p3? trueDoes p1 equal o? false
![Page 23: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/23.jpg)
23
Reading from the Command Line
• Now that we’ve learned about Java Strings and arrays, we can appreciate how to pass data into a Java program when invoking it from the command line
• When we invoke a program, we can type data after the name of the program on the command line; e.g.
java Simple ABC 123
![Page 24: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/24.jpg)
24
Command Line Input, cont.
• Such data gets handed to the main() method as a String array called ‘args’ (or whatever else we wish to name it), as indicated by the main() method's argument signature:
public static void main(String[] args) { … }
• Inside the main() method, we can do with 'args' whatever we’d do with any other array– E.g, determine its length, manipulate individual String
items within the array, and so forth
![Page 25: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/25.jpg)
25
Command Line Input, cont.
public class FruitExample {
// If this program is run from the
// command line as follows:
//
// java FruitExample apple banana cherry
//
// then the args array will be automatically
// initialized with THREE String values
// "apple", "banana", and "cherry", which
// will be stored in array 'cells' args[0],
// args[1], and args[2], respectively.
public static void main(String[] args) {
// Let's print out a few things.
System.out.println("The args array contains "
+ args.length + " entries.");
![Page 26: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/26.jpg)
26
Command Line Input, cont.
// Only execute this next block of code if // the array isn't empty. if (args.length > 0) { int i = args.length - 1; System.out.println("The last entry is: " + args[i]);
System.out.println("It is" + args[i].length() +
" characters long."); }
// etc. }}
![Page 27: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/27.jpg)
27
Command Line Input, cont.
• When this program is run from the command line as: java FruitExample apple banana cherry
it produces the following output:
The args array contains 3 entries.
The last array entry is: cherry
It is 6 characters long.
![Page 28: 1 SSD3 - Unit 2 Java toString & Equals Presentation 3.2.1 Class Website:](https://reader030.vdocument.in/reader030/viewer/2022032517/56649c995503460f94956072/html5/thumbnails/28.jpg)
28
Questions?