Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects as Variables Featuring the Date Object

Similar presentations


Presentation on theme: "Objects as Variables Featuring the Date Object"— Presentation transcript:

1 Objects as Variables Featuring the Date Object
Copyright © Curt Hill

2 Introduction Recall the five basic types:
Number String Boolean Undefined Null If it is not one of these, what is it? One of the possibilities is an object Copyright © Curt Hill

3 Handles Objects are always referred to by a handle A handle is just a pointer Although perhaps safer than pointers in C/C++ The handle is on the stack and refers to the value which is on the heap An object must be initialized with the new operator Copyright © Curt Hill

4 The new operator Three functions performed by new:
Allocate memory on the heap Invokes memory manager Generate the address to this memory This is usually received by a handle variable Call the constructor for the object Constructors initialize Copyright © Curt Hill

5 Form of New new object_name(parm) new is an operator
One of the few that is a word object_name is the name of the kind of object parm is the parameter to the constructor May be left out in case of default constructor Usually result is assigned to variable Copyright © Curt Hill

6 The Date object A Date represents a date and time Stored as a number
This number may represent any date within 100 million days of January 1, 1970 This is approximately 285,616 years It handles this as an extrapolated Gregorian calendars Thus it believes in October 10, 1582 October 5-14 were skipped in 1582 when calendar was started Copyright © Curt Hill

7 A constructor example Suppose that we have the following code: var d; // Set to undefined d = new Date(); d2 = new Date; document.write("<P> ",d); Both d and d2 use the default constructor Result is: Wed Jan :51:01 GMT-0600 (Central Standard Time) Copyright © Curt Hill

8 What do I get? The default constructor for Date initializes it to the current time from the system clock Most objects convert to a string in an object specific fashion With Date you get the string given above, which indicated date, time and timezone Copyright © Curt Hill

9 Date constructors Date may be initialized with a succession of information: Year, Month, Day, Hour, Minute, Second, Milliseconds Month is 0-11, not 1-12 Provided in this order – as many or as few A copy constructor also exists Copyright © Curt Hill

10 Examples d1 = new Date; d2 = new Date(); d3 = new Date(2008,2,1); // March 1 d4 = new Date(d3); d5 = new Date(2008); // Jan 1 d3 = new Date(2008,2,1,10,30); Copyright © Curt Hill

11 Operators Generally operators work on the handles
Methods should be used for object access or manipulation Assignment: d2 = d1; // copies handle d3 = new Date(d2); // copies // contents Copyright © Curt Hill

12 Date Methods There are a number of get and sets that allow access and manipulation Setting a value setYear – 1 to 3 parameters setMonth – 1 or 2 parameters setDate setHours – 1 to 4 parameters setMinutes – 1 or 3 parameters setSeconds– 1 or 2 parameters setMilliseconds Copyright © Curt Hill

13 Getting a value The expected methods:
getYear getMonth getDate etc All take no parameters and return a number A few less expected: getDay getTimeZoneOffset Copyright © Curt Hill

14 Examples Four days ago: d2.setDate(d2.getDate()-4)
Particular date d2.setYear(2007,11,15); Copyright © Curt Hill

15 What can be done to any object?
Execute any of the objects methods Assign to a similar object Direct comparison However, assignment and comparison are on the handle not the actual object Copyright © Curt Hill

16 Assignment Assignment of two object does not necessarily create new instances It merely copies the handle This can cause problems because of side effects Consider d1 = new Date(2007,0,21); d2 = d1; // Handle assign d2.setMonth(0,2); Copyright © Curt Hill

17 Date Example d1 = new Date (2007,0,21); d1 January 21, 2007 d2
d2 = d1; d1 January 21, 2007 d2 d2.setMonth (0,2); d1 January 2, 2007 d2 Copyright © Curt Hill

18 One more thought on assignment
Since direct assignment on objects only copies the handle it is better to use the copy constructor Provided it exists Thus avoid: d2 = d1; and use: d2 = new Date(d1); where possible Also use this for value parameters Copyright © Curt Hill

19 Comparisons Same problem with comparisons on handles
So with: d3 = new Date(d2); d2 will not compare equal with d2 d2 == d3 is false because the handles are different, not the object contents Copyright © Curt Hill

20 valueOf method Returns the number that is the basis of date
Comparing this number will give reasonable results Thus d3 = new Date(d2); d3.valueOf() == d2.valueOf() will be true In most other cases the number is of dubious value Copyright © Curt Hill

21 Memory partitions The memory for programs is divided into three chunks
The code memory Machine language instructions The stack memory Basics and handles The heap memory Objects Copyright © Curt Hill

22 Basic types are on the Stack
Allocated at declaration or first use Deallocated at end of method that contains declaration Quick allocation and deallocation Only primitives and handles on stack Stack also includes return addresses, function results and temporary results Copyright © Curt Hill

23 Objects are on the Heap Allocated by specific use of new keyword
Deallocated by the delete operator Requires a handle (pointer) All objects are on heap and nothing but objects on heap Copyright © Curt Hill

24 delete Disposes of a handle Form is: delete variable;
It returns a boolean value Copyright © Curt Hill

25 Memory Heap Stack s = 5; 5 January … t=null; null u= new Date()
c = ‘ABC’; ABC Copyright © Curt Hill

26 Java and JavaScript Java has primitives, but JavaScript has Basic types The JavaScript basics are really objects They just do not need a new or delete They all have methods and properties Copyright © Curt Hill


Download ppt "Objects as Variables Featuring the Date Object"

Similar presentations


Ads by Google