Presentation 5
Presentation 5
Object
Oriented
Programming
Dr. Abdulaziz Saleh Algablan
Email: [email protected]
2023
Topics
Arrays
Java Collection Framework
Null Value
Enumeration
Garbage Collection
Object Class
Array
• An array is a collection of data values of the same type.
• The following declares an array of double:
double[] rainfall;
• The square brackets indicate the array declaration.
• The brackets may be attached to a variable instead of the data type.
• For example, the declaration
double rainfall[];
is equivalent to the previous declaration
An Array of String
• We can also declare and allocate
memory for an array in one
statement, as in:
String[] monthName = new String[12]; monthName[0] = "January";
monthName[1] = "February";
monthName[2] = "March";
monthName[3] = "April";
• Then, elements of the array can be monthName[4] = "May";
monthName[5] = "June";
initilaized using the index: monthName[6] = "July";
monthName[7] = "August";
monthName[8] = "September";
monthName[9] = "October";
monthName[10] = "November";
monthName[11] = "December";
An Array of String
• Java allows you to initialize an array at the time of
declaration:
String[] monthName = { "January", "February", "March","April", "May",
"June", "July",
"August", "September", "October","November", "December" };
Length of Array
• An array has a public constant length for the size of an array.
String array[] = new String[] {"Toyota", "Mercedes", "BMW", "Volkswagen",
"Skoda" };
int arrayLenght = array.length;
• For example:
// create an array
int[] numbers = {1, 3, 5, 15};
Person[] personArray;
personArray = new Person[20];
Person[] personArray;
personArray = new Person[3];
• Properites of Enum:
• Improves type safety.
• Easily used in switch.
• Easily traversed.
• can have fields, constructors and methods.
• By defining an enumerated type, a variable of that type can only accept the associated
enumerated constants.
• Any violation will be detected by the compiler.
• Eliminate the possibility of assigning a nonsensical value as seen in the case for the numerical
constants.
Enum
• For example:
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
enum Month {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER}
enum Gender {MALE, FEMALE}
enum SkillLevel {NOVICE, INTERMEDIATE, ADVANCED, EXPERT}
• Here the Day enum is public, it should be defined in its own file.
• The file name should be “Day.java”
Enum - Improves type safety
//=== version 1
enum Fruit {APPLE, ORANGE, BANANA};
Fruit f1, f2, f3;
f1 = Fruit.APPLE;
f2 = Fruit.BANANA;
f3 = f1;
//=== version 2
final int APPLE = 1; final int ORANGE = 2; final int BANANA = 3;
int fOne, fTwo, fThree;
fOne = APPLE;
fTwo = ORANGE; fThree = fOne;
• The code may look comparable to the one that uses enumerated constants, but what will happen if we write the following?
fOne = 45;
Enum - Easily used in switch
Fruit fruit;
fruit = ...;
switch (fruit) {
case APPLE: . . .;
break;
Enum
• The Java compiler internally adds some methods when it creates an enum:
• values()
• valueOf(..)
• Ordinal()
• values() :
• returns an array containing all the values of the enum.
• valueOf() :
• returns the value of given constant enum.
• ordinal() method in the enum:
• returns the index of the enum value.
Enum - valueOf(…)
• The method accepts one String argument and returns the
enumerated constant whose value matches the given argument.
• For example:
Fruit fruit = Fruit.valueOf("APPLE");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your favorite fruit " +"(APPLE, ORANGE,
BANANA): ");
String fruitName = scanner.next( );
Fruit favoriteFruit = Fruit.valueOf(fruitName);
Traversing the
Values of Enum
Values of the Enum Constants
Memory Management
:++ Example in C
• What happen for variables after definitions and
assignment?
• Is some programming languages, there is explicit
way to free the memory (if no use for variables).
}
public static void m1()
{
String bString = “This is m1”;
}
}
GC Example – Cont.
Stack Heap
String
aString “Happy”
GC Example – Cont.
Stack Heap
String
aString “Happy”
GC Example – Cont.
Stack Heap
String
aString “Happy”
GC Example – Cont.
Stack Heap
51
The Class Object, cont.
• An object of any class can be substituted as a
parameter of Object is expected.
• Every class inherits some methods from the
class Object:
• equals()
• toString()
• hashCode()
• getClass()
• These methods can be overridden by the
derived class or by an intermediate ancestor
class.52
Method toString
• Inherited method toString() takes no
arguments.
• Typically, method toString() is coded to
produce and return a string which contains
everything of interest about the object.
53
Method toString, cont.
• Example:
public String toString();
{
return (“Name: “ + getName()+ \n
+ “Number: “ + getNumber());
}
• Whenever a new class is created, a suitable
toString method should be defined.
54
Method toString, cont.
• Method toString can be called my the
conventional means, Object_Name.toString,
or by using only Object_Name.
• Example:
String s = new String(“Hi”);
System.out.println(s.toString());
or
System.out.println(s);
55
Method clone()
• Method clone also is inherited from the class
Object.
• Method clone takes no arguments and returns
a copy of the calling object.
• Even though the data is identical, the
objects are distinct.
• Typically, method clone needs to be
overridden to function properly.
56
Method equals()
class Car { public class TestObjectClass {
int vin; // vehicle identification number. public static void main(String []
sr)
MyCar(int a){ {
vin = a;} Car i1 = new Car(2121);
Car i2 = new Car(2121);
public boolean equals(Object obj) { Car i3 = new Car(66443);
if(
obj.getClass().getName().equals(this.getClass().getName())) System.out.println(i1.equals(i1));
System.out.println(i1.equals(i2));
{ System.out.println(i1.equals(i3));
Car objAsMyCar = (Car)obj; }
}
return objAsMyCar.vin == this.vin;
} true
true
else false
return false;
}
}
57
The End