Objects as Variables Featuring the Date Object

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Advertisements

Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
JavaScript, Third Edition
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.
OOP Languages: Java vs C++
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Copyright © Curt Hill Structured Data What this course is about.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
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 ◦
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
“Unboxing” means taking an Integer object and assigning its value to a primitive int. This is done using the.intValue( ) method. Example; Integer z = new.
Memory Management in Java Mr. Gerb Computer Science 4.
Memory Management.
Object Lifetime and Pointers
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Part of the Assembler Language Programmers Toolbox
A Review or Brief Introduction
Object-Oriented Programming Part 1 07_classes.ppt
Distinguishing logic from data type
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming: Guided Learning with Early Objects
Chapter 10: Void Functions
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Scope, Objects, Strings, Numbers
JavaScript for C++ and Java Programmers
JavaScript Syntax and Semantics
Pointers and references
An Automated Testing Framework
Java Review: Reference Types
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Constants in Java Why and How Copyright © Curt Hill.
Java Classes and Objects 3rd Lecture
Defining Classes I Part A.
A Different Kind of Variable
Object Oriented Programming in java
A Robust Data Structure
Defining Classes and Methods
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Introduction of Programming
The Java switch Statement
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Data Structures & Algorithms
How Dynamic Memory Works with Memory Diagram
Pointers and references
The IF Revisited A few more things Copyright © Curt Hill.
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Methods Coding in Java Copyright © Curt Hill.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects as Variables Featuring the Date Object Copyright © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 30 2008 20:51:01 GMT-0600 (Central Standard Time) Copyright © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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 © 1997 - 2015 Curt Hill

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

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

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 © 1997 - 2015 Curt Hill