Data Abstraction: Status of an Object

Similar documents
From Noodle-Oriented Programming ( ) to Object-Oriented Programming

VARIABLES AND TYPES CITS1001

Polymorphism. Hsuan-Tien Lin. OOP Class, April 8, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/ / 26

Java Identifiers, Data Types & Variables

Introduction to Programming Using Java (98-388)

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

1 Shyam sir JAVA Notes

DECLARAING AND INITIALIZING POINTERS

Motivation was to facilitate development of systems software, especially OS development.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Motivation was to facilitate development of systems software, especially OS development.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

CS 231 Data Structures and Algorithms, Fall 2016

a data type is Types

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Java Basic Datatypees

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Declaration and Memory

An overview of Java, Data types and variables

Computational Expression

Compiling Techniques

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Programming with Java

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 11 java track: lecture 1

CST112 Variables Page 1

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Key Differences Between Python and Java

Overview. Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays. Initialization Searching

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

An Introduction to Processing

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Goals of this Lecture

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Full file at

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Getting started with Java

Fundamentals of Programming Data Types & Methods

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Deconstructing Java TM

Special Topics: Programming Languages

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS558 Programming Languages

Lecture Set 2: Starting Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS1 Lecture 3 Jan. 18, 2019

Short Notes of CS201

Lecture Set 2: Starting Java

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

CS201 - Introduction to Programming Glossary By

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

1007 Imperative Programming Part II

1B1b Classes in Java Part I

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Lecture 3: C Programm

CS112 Lecture: Primitive Types, Operators, Strings

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Midterms Save the Dates!

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Assumptions. History

Computer Programming

CS558 Programming Languages

Memory, Data, & Addressing II CSE 351 Spring

B.V. Patel Institute of BMC & IT, UTU 2014

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Chapter 2: Using Data

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

13 th Windsor Regional Secondary School Computer Programming Competition

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

Question. 1 Features of Java.

ECE 122 Engineering Problem Solving with Java

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CSC Java Programming, Fall Java Data Types and Control Constructs

CHAPTER 7 OBJECTS AND CLASSES

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

I. Variables and Data Type week 3

Getting started with Java

Unit 2: Java in the small

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Exercise: Using Numbers

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Examining the Code. [Reading assignment: Chapter 6, pp ]

2 rd class Department of Programming. OOP with Java Programming

A few notes on using pointers in the C language

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

Announcements for this Lecture

Transcription:

Data Abstraction: Status of an Object Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 10, 2009 H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 0 / 50

What We Should Have Known very serious consequences when violating the principle write-once, use forever; not use-once, dump forever Java is not only a type of coffee, but also a language Java and C share many similarities, and some differences noodle-oriented programming is not (always) the best solution POP: organized CODE (procedure) + data OOP: organized DATA + organized code (ACTION) one class, many instances H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 1 / 50

Class versus Instances (courtesy of Prof. Chuen-Liang Chen) Are they the same? instance 個體 (object) with different status representation of status (in high-level language): variable instance: set of instance variables class 類別 it is no way & unnecessary to write program for instances one by one OO programming = class (interface) declarations OOP Chuen-Liang Chen, NTU CS&IE / 9 H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 2 / 50

Class versus Instances 1 class Record { / / class 2 S t r i n g name ; / / v a r i a b l e d e c l a r a t i o n 3 S t r i n g ID ; / / v a r i a b l e d e c l a r a t i o n 4 p u b l i c bool isb86 ( ) { / / a c t i o n 5 r e t u r n ID. s t a r t s W i t h ( " B86 " ) ; 6 / / here ID i s an instance of the class S t r i n g 7 / / and performs an a c t i o n ( method ) s t a r t s W i t h ( ) 8 } 9 } 10 11 Record r1 = new Record ( ) ; / / r1 i s an instance 12 / / with r1. name and r1. ID 13 / / as i t s data ( v a r i a b l e s ) 14 Record r2 = new Record ( ) ; / / r2 i s another instance 15 Record [ ] r a r r a y = new Record [ 3 ] ; 16 17 i f ( r2. isb86 ( ) ) { } / / r2 performs an a c t i o n ( method ) H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 4 / 50

An OO Design of the RandomIndex class DATA: a randomly permuted index array of size N ACTION: setsize, initializeindex, permuteindex, getnext see RandomIndex.java now you can use it for name calling in class, distributing cards in POO games, etc. H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 6 / 50

You Have Seen Some Classes/Instances the java.lang.string class and its instances "abc", "def" the java.lang.system class, but no instances the java.io.printstream class, and one instance System.out Read the API, Guess, and Write the progam you want H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 7 / 50

What is Data? H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 9 / 56

What is Data? (Wikipedia) Data refer to a collection of facts usually collected as the result of experience, observation or experiment, or processes within a computer system, or a set of premises. This may consist of numbers, words, or images, particularly as measurements or observations of a set of variables. Data are often viewed as a lowest level of abstraction from which information and knowledge are derived. data (in execution): memory interpretations data (in language): variables H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 11 / 50

Data in Execution: Memory Interpretation (1) content before interpretation: e.g. bits 01010000010011110100111100000000 type of interpretation: e.g. a little-endian integer (that occupies 32 bits) value after interpretation: e.g. 1347374848 H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 12 / 50

Data in Execution: Memory Interpretation (2) content before interpretation: e.g. bits 01010000010011110100111100000000 type of interpretation: e.g. a big-endian integer (that occupies 32 bits) value after interpretation: e.g. 5197648 H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 13 / 50

Data in Execution: Memory Interpretation (3) content before interpretation: e.g. bits 01010000010011110100111100000000 type of interpretation: e.g. a 0-terminated character array value after interpretation: e.g. "POO" ( P, O, O, 0) H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 14 / 50

Data in Language: Variables variables: a (named) representation of data in language variable declaration: set type (and name) e.g. int a; double b; String s; variable assignment: alter content e.g. a = 3; variable evaluation: obtain value e.g. if (4 == a + 2)...; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 15 / 50

Type: Defining Memory Interpretation primitive type: what the language supports as a basic building block extended type: e.g. String (as character array in C) e.g. structures in C, classes in Java Data Abstraction: don t care (much) about what the bytes contain, care about what the type means H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 16 / 50

Extended Type: What It Means? 1 class Record { 2 S t r i n g name ; 3 i n t score ; 4 } we intend to extract a String type memory space, and a int type memory space from a Record type variable H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 18 / 50

Eight Java Primitive Types primitive type: defining direct memory interpretations byte, short, int, long: 8/16/32/64 bit (big-endian) integers float, double: 32/64 bit floating point numbers boolean: true or false char: 16 bit unicode all (except boolean) very similar to C H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 19 / 50

Many Java Extended Types class WhatEverYouWant class 2DPoint class Record class java.io.printstream H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 20 / 50

One Java Extended Type with Native Support class String the same as any extended type you see native operation support (e.g. +) literals "abc" recognized by the language (much like 3.14) some other special handling H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 21 / 50

But Wait! we intend to extract a String type memory space, and a int type memory space from a Record type variable What REALLY happens in the memory of JVM? H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 22 / 50

Declaration/Allocation/Assignment primitive types: 1 i n t i ; / / d e c l a r a t i o n and a l l o c a t i o n 2 i = 3; / / assignment H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 24 / 50

Declaration/Allocation/Assignment extended types: 1 Record r ; / / d e c l a r a t i o n 2 r = new Record ( ) ; / / a l l o c a t i o n and 3 / / assignment ( of instance ) 4 r. score = 10; / / assignment ( of member) Java has no pointer?! H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 26 / 50

Declaration/Allocation/Assignment String type: 1 S t r i n g s ; / / d e c l a r a t i o n 2 s = " 123 " ; / / assignment 3 / / ( of instance and member) H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 28 / 50

Extended Type Revisited each extended-type variable holds a reference (more restricted type of pointer) to the actual memory space at declaration time the extended-type variable won t point to a legitimate memory space unless we do allocation and reference assignment the extended-type variable does not contain meaningful data unless we do member assignment Java: no pointer arithmetic, but yes pointer! (with nickname reference)! H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 29 / 50

Primitive Type Revisited each primitive-type variable holds a allocated memory slot at declaration time thus, the primitive-type variable always associates with a legitimate memory space the primitive-type variable does not contain meaningful data unless we do value assignment H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 30 / 50

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 32 / 50

Fun Time (2) What happens in memory? 1 Record r1 ; / / r1. name, r1. score 2 Record r2 ; 3 r1 = new Record ( ) ; 4 r2 = r1 ; 5 r1. name = " HTLin " ; 6 r2. score = 98; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 34 / 50

Fun Time (4) What happens in memory? 1 class Person { S t r i n g name ; Person spouse ; } 2 3 Person George ; 4 Person Marry ; 5 George = new Person ( ) ; 6 George. name = " George " ; 7 Marry = new Person ( ) ; 8 Marry. name = " Marry " ; 9 Mary. spouse = George ; 10 George. spouse = Marry ; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 36 / 50

Fun Time (5) What happens in memory? 1 class Person { S t r i n g name ; Person spouse ; } 2 3 Person George ; 4 George = new Person ( ) ; 5 George. name = " George " ; 6 George. spouse = new Person ( ) ; 7 George. spouse. name = " Marry " ; 8 George. spouse = new Person ( ) ; 9 George. spouse. name = " Lisa " ; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 38 / 50

Life Cycle of a Primitive Variable (C/Java) declared and created 1 i n t count ; used and modified 1 count += 1; destroyed automatically (when out of scope) H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 40 / 50

Life Cycle of an Object Instance (Java) reference declared 1 Record r ; instance created 1 r = new Record ( ) ; used and modified 1 System. out. p r i n t l n ( r. name ) ; destroyed automatically (when out of use) H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 42 / 50

Constructor (1/3) 1 r = new Record ( ) ; the new operator allocates memory for the instance often you will do this: 1 r = new Record ( ) ; 2 r. name = " HTLin " ; 3 r. score = 99; out of laziness, you want to do this: 1 r = new Record ( " HTLin ", 9 0 ) ; How? H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 44 / 50

Constructor (2/3) 1 class Record { 2 S t r i n g name ; 3 i n t score ; 4 p u b l i c Record ( S t r i n g init_name, 5 i n t i n i t _ s c o r e ) { 6 name = init_name ; 7 score = i n i t _ s c o r e ; 8 } 9 } 10 11 r = new Record ( " HTLin ", 9 0 ) ; H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 46 / 50

Constructor (3/3) constructor: called by new to initialize name: same as class name remember the public (will come back to this later) default constructor (if you didn t write any code): same as 1 p u b l i c Record ( ) { 2 } constructor without argument ( replace the default one): 1 p u b l i c Record ( ) { 2 score = 60; 3 } H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 48 / 50

What Happens Here? 1 class Record { 2 i n t t o t a l _ r e c ; 3 p u b l i c Record ( ) { 4 t o t a l _ r e c += 1; 5 } 6 p u b l i c void t o t a l _ r e c ( ) { 7 System. out. p r i n t l n ( t o t a l _ r e c ) ; 8 } 9 } 10... 11 Record r 1 = new Record ( ) ; 12 r1. t o t a l _ r e c ( ) ; 13 Record r 2 = new Record ( ) ; 14 r2. t o t a l _ r e c ( ) ; What is the output? H.-T. Lin (NTU CSIE) Data Abstraction: Status of an Object OOP(even) 03/10/2009 50 / 50