Great coders are todays rock stars Will.I.Am Watch the video at - - PowerPoint PPT Presentation

great coders are today s rock stars will i am watch the
SMART_READER_LITE
LIVE PREVIEW

Great coders are todays rock stars Will.I.Am Watch the video at - - PowerPoint PPT Presentation

Preamble Data types Call-by-value Scope ITI 1121. Introduction to Computing II Marcel Turcotte (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 11, 2015


slide-1
SLIDE 1

Preamble Data types Call-by-value Scope

ITI 1121. Introduction to Computing II†

Marcel Turcotte (with contributions from R. Holte)

School of Electrical Engineering and Computer Science University of Ottawa

Version of January 11, 2015

†Please don’t print these lecture notes unless you really need to! Marcel Turcotte ITI 1121. Introduction to Computing II

slide-2
SLIDE 2

Great coders are today’s rock stars Will.I.Am Watch the video at code.org!

slide-3
SLIDE 3

Preamble Data types Call-by-value Scope

Review

Objectives:

  • 1. Discussing several concepts related to data types
  • 2. Understanding the implications of the differences between

primitive and reference types

  • 3. Reviewing call-by-value
  • 4. Understanding the concept of scope

Lectures:

◮ Pages 597–631 of E. Koffman and P. Wolfgang.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-4
SLIDE 4

Preamble Data types Call-by-value Scope

Plan

  • 1. What are variables and data types
  • 2. Primitive vs reference
  • 3. Comparison operators (primitive vs reference)
  • 4. Auto-boxing/auto-unboxing;
  • 5. Passing parameters
  • 6. Scope
  • 7. Memory management

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-5
SLIDE 5

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Variables

What is a variable?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-6
SLIDE 6

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Variables

What is a variable?

◮ A variable is a place in memory, to hold a value, which we

refer to with help of a label

33 (i)

0,000,123,456

... ...

4,294,967,296 0,000,000,000 0,000,123,457 0,000,123,455

byte i = 33;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-7
SLIDE 7

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Variables

I will be using Greek letters to designate memory locations (addresses) since in Java we don’t know the location of “objects” and should not care!

33 (i) ... ...

α + 1 α − 1 α

byte i = 33;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-8
SLIDE 8

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types

What are data types for?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-9
SLIDE 9

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types

What are data types for?

◮ Yes, it tells the compiler how much memory to allocate:

double formula ; // 8 bytes char c ; // 2 bytes

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-10
SLIDE 10

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types

What are data types for?

◮ Yes, it tells the compiler how much memory to allocate:

double formula ; // 8 bytes char c ; // 2 bytes

◮ But it also?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-11
SLIDE 11

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types

What are data types for?

◮ Yes, it tells the compiler how much memory to allocate:

double formula ; // 8 bytes char c ; // 2 bytes

◮ But it also? It gives information about the meaning

(semantic) of the data: which operations are allowed, which data are compatible.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-12
SLIDE 12

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types

What are data types for?

◮ Yes, it tells the compiler how much memory to allocate:

double formula ; // 8 bytes char c ; // 2 bytes

◮ But it also? It gives information about the meaning

(semantic) of the data: which operations are allowed, which data are compatible. Hence the following statement,

c = f l a g ∗ formula ;

will produce an error at compile time; data types are therefore also useful to help detect errors in programs early on.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-13
SLIDE 13

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types (contd)

◮ To be more precise, there are concrete data types and

abstract data types

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-14
SLIDE 14

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types (contd)

◮ To be more precise, there are concrete data types and

abstract data types

◮ Concrete data types specify both, the allowed operations and

the representation of the data

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-15
SLIDE 15

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data types (contd)

◮ To be more precise, there are concrete data types and

abstract data types

◮ Concrete data types specify both, the allowed operations and

the representation of the data

◮ Abstract Data Types (ADTs) specify only the allowed

  • perations

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-16
SLIDE 16

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-17
SLIDE 17

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans Marcel Turcotte ITI 1121. Introduction to Computing II

slide-18
SLIDE 18

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-19
SLIDE 19

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

◮ References:

◮ Predefined: Marcel Turcotte ITI 1121. Introduction to Computing II

slide-20
SLIDE 20

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

◮ References:

◮ Predefined: ◮ Arrays ◮ Strings ◮ User defined, Marcel Turcotte ITI 1121. Introduction to Computing II

slide-21
SLIDE 21

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

◮ References:

◮ Predefined: ◮ Arrays ◮ Strings ◮ User defined, reference to an instance of a class; Marcel Turcotte ITI 1121. Introduction to Computing II

slide-22
SLIDE 22

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

◮ References:

◮ Predefined: ◮ Arrays ◮ Strings ◮ User defined, reference to an instance of a class; ◮ The value of a reference variable is a memory location,

which points/references to the location of an object; it is a pointer, a “link”, it’s a reference

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-23
SLIDE 23

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Data Types in Java

In Java, we have primitive and reference data types:

◮ Primitive are:

◮ numbers, characters (but not Strings) and booleans ◮ the value is stored at the memory location designated by

the label of the variable

◮ References:

◮ Predefined: ◮ Arrays ◮ Strings ◮ User defined, reference to an instance of a class; ◮ The value of a reference variable is a memory location,

which points/references to the location of an object; it is a pointer, a “link”, it’s a reference

◮ The declaration of a reference variable does not create an

  • bject, does not allocate space for an object, it only

allocates memory to store the address of an object

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-24
SLIDE 24

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

> i n t [ ] a ; a = new i n t [ 5 ] ;

(a) ...

α

null ...

⇒ The declaration of a reference variable only allocates memory to hold a reference (sometimes called pointer or address), null is a special value (literal), which does not reference an object

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-25
SLIDE 25

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

i n t [ ] a ; > a = new i n t [ 5 ] ;

(a) ... ...

α

null ...

β

(instance)

⇒ The creation of a new instance, new int[ 5 ], allocates memory to hold 5 integer values (and the housekeeping information). Each cell of the array is initialized with the default int value, which is 0.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-26
SLIDE 26

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

i n t [ ] a ; > a = new i n t [ 5 ] ;

(a) ... ...

α

...

β β

(instance)

⇒ Finally, the reference of the newly created object is assigned to the location designed by the label a.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-27
SLIDE 27

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following,

(a) ... ... α ... β β (instance)

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-28
SLIDE 28

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following,

(a) ... ... α ... β β (instance)

(a)

α β β

(instance) Marcel Turcotte ITI 1121. Introduction to Computing II

slide-29
SLIDE 29

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following,

(a) ... ... α ... β β (instance)

(a)

α β β

(instance)

a Marcel Turcotte ITI 1121. Introduction to Computing II

slide-30
SLIDE 30

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

In a memory diagram, I want to see:

◮ a box for every reference variable with an arrow pointing a the

designated object

◮ a box for every primitive variable with the value inside the box ◮ a box for every object

i n t [ ] a ; a = new i n t [ 5 ] ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-31
SLIDE 31

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

In a memory diagram, I want to see:

◮ a box for every reference variable with an arrow pointing a the

designated object

◮ a box for every primitive variable with the value inside the box ◮ a box for every object

i n t [ ] a ; a = new i n t [ 5 ] ;

a Marcel Turcotte ITI 1121. Introduction to Computing II

slide-32
SLIDE 32

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Given the following class declaration:

public c l a s s Constant { private f i n a l S t r i n g name ; private f i n a l double value ; public Constant ( S t r i n g name , double value ) { t h i s . name = name ; t h i s . value = value ; } }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-33
SLIDE 33

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Given the following class declaration:

public c l a s s Constant { private f i n a l S t r i n g name ; private f i n a l double value ; public Constant ( S t r i n g name , double value ) { t h i s . name = name ; t h i s . value = value ; } }

Draw the memory diagram for the following statments:

Constant c ; c = new Constant ( ” golden r a t i o ” , 1.61803399 ) ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-34
SLIDE 34

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-35
SLIDE 35

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

1.6180339887

c value name

"golden ratio"

an instance

  • f the class

String an instance

  • f the class

Constant a reference variable of type Constant

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-36
SLIDE 36

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Memory diagram

1.6180339887

c value name

"golden ratio"

an instance

  • f the class

String an instance

  • f the class

Constant a reference variable of type Constant

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-37
SLIDE 37

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Remember

◮ Variables have types ◮ Objects have classes

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-38
SLIDE 38

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Class Integer

In the following examples, we’ll be using our own class Integer:

c l a s s I n t e g e r { i n t value ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-39
SLIDE 39

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Class Integer

In the following examples, we’ll be using our own class Integer:

c l a s s I n t e g e r { i n t value ; }

Usage:

I n t e g e r a ; a = new I n t e g e r ( ) ; a . value = 33; a . value++; System . out . p r i n t l n ( ”a . value = ” + a . value ) ;

We use the dot notation to access the value of an instance variable.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-40
SLIDE 40

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Class Integer

In the following examples, we’ll be using our own class Integer:

c l a s s I n t e g e r { i n t value ; }

Usage:

I n t e g e r a ; a = new I n t e g e r ( ) ; a . value = 33; a . value++; System . out . p r i n t l n ( ”a . value = ” + a . value ) ;

We use the dot notation to access the value of an instance variable. Java has a pre-defined class named Integer. It is called a “wrapper” class; it wraps an int value into an object.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-41
SLIDE 41

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Class Integer

Adding a constructor

c l a s s I n t e g e r { i n t value ; I n t e g e r ( i n t v ) { value = v ; } }

Usage:

I n t e g e r a ; a = new I n t e g e r ( 33 ) ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-42
SLIDE 42

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i ... 33 ...

i n t i = 33;

null i ... null ... alias ...

I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

At compile time the necessary memory to hold the reference is allocated

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-43
SLIDE 43

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i ... 33 ...

i n t i = 33;

null i ... ... 33 null ... alias ... value

α I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

Creating an object (new Integer( 33 ))

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-44
SLIDE 44

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i ... 33 ...

i n t i = 33;

null i ... ... 33 ... alias ... value

α α I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

Assigning the reference of that object to the reference variable i

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-45
SLIDE 45

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i ... 33 ...

i n t i = 33;

i ... ... 33 ... alias ... value

α α α I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

Copying the value of the reference variable i into alias

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-46
SLIDE 46

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i ... 33 ...

i n t i = 33;

i ... ... 33 ... alias ... value

I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

i and alias are both designating the same object!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-47
SLIDE 47

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Primitive vs reference variables

i 33

i n t i = 33;

i ... 33 alias value

I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ;

Using the memory diagram representation

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-48
SLIDE 48

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Wrappers

◮ For every primitive type there is an associated wrapper class

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-49
SLIDE 49

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Wrappers

◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive

type int

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-50
SLIDE 50

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Wrappers

◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive

type int

◮ A wrapper stores a value of a primitive type inside an object

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-51
SLIDE 51

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Wrappers

◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive

type int

◮ A wrapper stores a value of a primitive type inside an object ◮ This will be paramount for stacks, queues, lists and trees

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-52
SLIDE 52

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Wrappers

◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive

type int

◮ A wrapper stores a value of a primitive type inside an object ◮ This will be paramount for stacks, queues, lists and trees ◮ Besides holding a value, the wrapper classes possess several

class methods, mainly to convert values from/to other types, e.g. Integer.parseInt( “33” )

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-53
SLIDE 53

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-54
SLIDE 54

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-55
SLIDE 55

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int, but i is a reference

variable, of type Integer

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-56
SLIDE 56

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int, but i is a reference

variable, of type Integer

◮ In Java 1.4 or older, this would cause a compile-time error!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-57
SLIDE 57

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int, but i is a reference

variable, of type Integer

◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-58
SLIDE 58

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int, but i is a reference

variable, of type Integer

◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-59
SLIDE 59

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Quiz

This is valid Java statement, true or false?

I n t e g e r i = 1;

◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int, but i is a reference

variable, of type Integer

◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement! Why?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-60
SLIDE 60

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Auto-boxing

This is because Java 5, 6 and 7 automagically transform the following statement

I n t e g e r i = 1;

into

I n t e g e r i = new I n t e g e r ( 1 ) ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-61
SLIDE 61

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Auto-boxing

This is because Java 5, 6 and 7 automagically transform the following statement

I n t e g e r i = 1;

into

I n t e g e r i = new I n t e g e r ( 1 ) ;

This is called auto-boxing.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-62
SLIDE 62

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Auto-unboxing

Similarly, the statement i = i + 5

I n t e g e r i = 1; i = i + 5;

is transformed into

i = new I n t e g e r ( i . i n t V a l u e () + 5 ) ;

where the value of the wrapper object designated by i is extracted, unboxed, with the method call, i.intValue().

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-63
SLIDE 63

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Boxing/unboxing

Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-64
SLIDE 64

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Boxing/unboxing

Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class. The automatic conversion from primitive to reference type is called boxing,

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-65
SLIDE 65

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Boxing/unboxing

Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class. The automatic conversion from primitive to reference type is called boxing, and the conversion from reference to primitive type is called unboxing.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-66
SLIDE 66

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Does it matter?

long s1 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s1 = s1 + ( long ) 1 ; } Long s2 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s2 = s2 + ( long ) 1 ; } Marcel Turcotte ITI 1121. Introduction to Computing II

slide-67
SLIDE 67

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Does it matter?

long s1 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s1 = s1 + ( long ) 1 ; } Long s2 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s2 = s2 + ( long ) 1 ; }

49 milliseconds 340 milliseconds

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-68
SLIDE 68

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Does it matter?

long s1 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s1 = s1 + ( long ) 1 ; } Long s2 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s2 = s2 + ( long ) 1 ; }

49 milliseconds 340 milliseconds

◮ Why?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-69
SLIDE 69

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Does it matter?

long s1 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s1 = s1 + ( long ) 1 ; } Long s2 = ( long ) 0 ; f o r ( j =0; j <10000000; j++ ) { s2 = s2 + ( long ) 1 ; }

49 milliseconds 340 milliseconds

◮ Why?

On the right side, s2 is declared as a Long, hence, the line,

s2 = s2 + ( long ) 1;

is rewritten as,

s2 = new Long ( s2 . longValue () + ( long ) 1 ) ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-70
SLIDE 70

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Programming tip: benchmarking your code

long s t a r t , stop , e l a p s e d ; s t a r t = System . c u r r e n t T i m e M i l l i s ( ) ; // s t a r t the c l o c k for ( j =0; j <10000000; j++ ) { s2 += ( long ) 1; // stands f o r ‘ s2 = s2 + ( long ) 1 ’ } stop = System . c u r r e n t T i m e M i l l i s ( ) ; // stop the c l o c k e l a p s e d = stop − s t a r t ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-71
SLIDE 71

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Programming tip: benchmarking your code

long s t a r t , stop , e l a p s e d ; s t a r t = System . c u r r e n t T i m e M i l l i s ( ) ; // s t a r t the c l o c k for ( j =0; j <10000000; j++ ) { s2 += ( long ) 1; // stands f o r ‘ s2 = s2 + ( long ) 1 ’ } stop = System . c u r r e n t T i m e M i l l i s ( ) ; // stop the c l o c k e l a p s e d = stop − s t a r t ;

where System.currentTimeMillis() returns the number of milliseconds elapsed since midnight, January 1, 1970 UTC (Coordinated Universal Time). System.nanoTime() also exists.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-72
SLIDE 72

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Billion-dollar mistake (Null reference)

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” Tony Hoare

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-73
SLIDE 73

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators – primitive data types

Variables of primitive data types can be compared directly

i n t a = 5; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

What will be printed out on the output?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-74
SLIDE 74

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators – primitive data types

Variables of primitive data types can be compared directly

i n t a = 5; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

What will be printed out on the output? ⇒ Prints “a < b”

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-75
SLIDE 75

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators i n t a = 5 ; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

a ... 5 ... 10 b ...

α β

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-76
SLIDE 76

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators: primitive and reference types

What will happen and why?

i n t a = 5; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-77
SLIDE 77

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators: primitive and reference types

What will happen and why?

i n t a = 5; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

slide-78
SLIDE 78

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators i n t a = 5 ; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

a ... 5 ... b ...

α β

5 value

γ γ

References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

slide-79
SLIDE 79

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators i n t a = 5 ; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

a ... 5 ... b ... 5 value

References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

slide-80
SLIDE 80

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators i n t a = 5 ; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

a 5 b 5 value

References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

slide-81
SLIDE 81

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will be the result?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 10 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-82
SLIDE 82

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will be the result?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 10 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } Less.java:14: operator < cannot be applied to MyInteger,MyInteger if ( a < b ) { ^ 1 error

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-83
SLIDE 83

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Remarks

◮ These error messsages are produced by pre-1.5 Java compilers ◮ Starting with Java 1.5, autoboxing masks the “problem” ◮ In order to get same behaviour with the two environments,

let’s use our wrapper, MyInteger

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-84
SLIDE 84

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Class MyInteger

c l a s s MyInteger { i n t value ; MyInteger ( i n t v ) { value = v ; } }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-85
SLIDE 85

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators i n t a = 5 ; MyInteger b = new MyInteger ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; }

a 5 b 5 value

◮ Fix this!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-86
SLIDE 86

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Solution

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-87
SLIDE 87

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Solution

i n t a = 5; MyInteger b = new MyInteger ( 5 ) ; i f ( a < b . value ) { System . out . p r i n t l n ( ”a i s l e s s than b” ) ; } e l s e i f ( a == b . value ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a i s g r e a t e r than b” ) ; }

⇒ Prints “a equals b”.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-88
SLIDE 88

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will happen and why?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-89
SLIDE 89

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will happen and why?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; }

⇒ The result is “a does not equal b”.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-90
SLIDE 90

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; } a ... ... b ... 5 value 5 value ... ... Marcel Turcotte ITI 1121. Introduction to Computing II

slide-91
SLIDE 91

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; } a ... ... b ... 5 value 5 value ... ...

⇒ The result is “a does not equal b”.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-92
SLIDE 92

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; }

a b 5 value 5 value

⇒ The result is “a does not equal b”.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-93
SLIDE 93

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Solution

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-94
SLIDE 94

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Solution

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not b” ) ; }

where equals could have been defined as an instance method:

public boolean e qu a l s ( MyInteger

  • ther

) { r e t u r n s t h i s . value == other . value ; }

⇒ Would print “a equals b”.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-95
SLIDE 95

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

What will happen?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-96
SLIDE 96

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

What will happen?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; }

⇒ Prints “a == b”, why?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-97
SLIDE 97

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

What will happen?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; }

⇒ Prints “a == b”, why? because a and b reference the same

  • bject (instance), in other words, the two memory locations are

the same; we say that b is an alias for a.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-98
SLIDE 98

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will happen?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-99
SLIDE 99

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Comparison operators and reference types

What will happen?

MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; }

⇒ prints “a equals b” because the two values are equal.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-100
SLIDE 100

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; }

a b 5 value

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-101
SLIDE 101

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a . e q u a l s ( b ) ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; }

a b 5 value

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-102
SLIDE 102

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Remarks

◮ Two reference variables designate objects that are “logically

equivalent” if these objects have the same “content”, use the method equals to test for “content or logical equality”‡

i f ( a . e qu a l s ( b ) ) { . . . }

vs

i f ( a == b ) { . . . }

‡We will continue the discussion when

learning about inheritance!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-103
SLIDE 103

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Remarks

◮ Two reference variables designate objects that are “logically

equivalent” if these objects have the same “content”, use the method equals to test for “content or logical equality”‡

◮ In order to test if two reference variables designate the same

  • bject, use the comparison operators ’==’ and ’!=’

i f ( a . e qu a l s ( b ) ) { . . . }

vs

i f ( a == b ) { . . . }

‡We will continue the discussion when

learning about inheritance!

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-104
SLIDE 104

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators

Exercises

Experiment comparing these objects using equals and ==, you might be surprised by the results.

S t r i n g a = new S t r i n g ( ” Hello ” ) ; S t r i n g b = new S t r i n g ( ” Hello ” ) ; i n t c [ ] = { 1 , 2 , 3 }; i n t d [ ] = { 1 , 2 , 3 }; S t r i n g e = ” He llo ” ; S t r i n g f = ” Hell o ” ; S t r i n g g = f + ”” ;

In particular, try a == b and e == f.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-105
SLIDE 105

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Definition: arity

The arity of a method is simply the number of parameters; a method may have no parameter, one parameter or many.

MyInteger () { t h i s . value = 0; } MyInteger ( i n t v ) { t h i s . value = v ; } i n t sum( i n t a , i n t b ) { return a + b ; }

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-106
SLIDE 106

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Definition: formal parameter

A formal parameter is a variable which is part of the definition of the method; it can be seen as a local variable of the body of the method.

i n t sum( i n t a , i n t b ) { return a + b ; }

⇒ a and b are formal parameters of sum.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-107
SLIDE 107

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Definition: actual parameter

An actual parameter is the variable which is used when the method is called to supply the value for the formal parameter.

i n t sum( i n t a , i n t b ) { return a + b ; } . . . i n t midTerm , finalExam , t o t a l ; t o t a l = sum( midTerm , finalExam ) ;

midTerm and finalExam are actual parameters of sum, when the method is called the value of the actual parameters is copied to the location of the formal parameters.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-108
SLIDE 108

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-109
SLIDE 109

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-110
SLIDE 110

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-111
SLIDE 111

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-112
SLIDE 112

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

◮ the value of the actual parameters are copied to the location

  • f the formal parameters

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-113
SLIDE 113

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

◮ the value of the actual parameters are copied to the location

  • f the formal parameters

◮ the body of the method is executed

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-114
SLIDE 114

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

◮ the value of the actual parameters are copied to the location

  • f the formal parameters

◮ the body of the method is executed ◮ a return value or (void) is saved

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-115
SLIDE 115

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

◮ the value of the actual parameters are copied to the location

  • f the formal parameters

◮ the body of the method is executed ◮ a return value or (void) is saved ◮ (the activation frame is destroyed)

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-116
SLIDE 116

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

Concept: call-by-value

In Java, when a method is called, the values of the actual parameters are copied to the location of the formal parameters. When a method is called:

◮ the execution of the calling method is stopped ◮ an activation frame (activation block or record) is created

(it contains the formal parameters as well as the local variables)

◮ the value of the actual parameters are copied to the location

  • f the formal parameters

◮ the body of the method is executed ◮ a return value or (void) is saved ◮ (the activation frame is destroyed) ◮ the execution of the calling method restarts with the next

instruction

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-117
SLIDE 117

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

public c l a s s Test { public s t a t i c void increment ( i n t a ) { a = a + 1; } public s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5; System . out . p r i n t l n ( ” b e f o r e : ” + a ) ; increment ( a ) ; System . out . p r i n t l n ( ” a f t e r : ” + a ) ; } }

What will printed?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-118
SLIDE 118

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

public c l a s s Test { public s t a t i c void increment ( i n t a ) { a = a + 1; } public s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5; System . out . p r i n t l n ( ” b e f o r e : ” + a ) ; increment ( a ) ; System . out . p r i n t l n ( ” a f t e r : ” + a ) ; } }

What will printed?

b e f o r e : 5 a f t e r : 5

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-119
SLIDE 119

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types p u b l i c s t a t i c void increment ( i n t a ) { a = a + 1; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { > i n t a = 5 ; increment ( a ) ; }

5 a args

activation frame for main

Each method call has its own activation frame, which holds the parameters and local variables (here, args and a)

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-120
SLIDE 120

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types p u b l i c s t a t i c void increment ( i n t a ) { a = a + 1; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5 ; > increment ( a ) ; }

5 a args

activation frame for main activation frame for increment

a

When increment is called a new activation frame is created

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-121
SLIDE 121

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types p u b l i c s t a t i c void increment ( i n t a ) { a = a + 1; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5 ; > increment ( a ) ; }

5 a 5 args

activation frame for main activation frame for increment

a

The value of the actual parameter is copied to the location of the formal parameter

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-122
SLIDE 122

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types p u b l i c s t a t i c void increment ( i n t a ) { > a = a + 1; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5 ; increment ( a ) ; }

5 a 6 args

activation frame for main activation frame for increment

a

The execution of a = a + 1 changes the content of the formal parameter a, which is a distinct memory location the local variable a of the main method

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-123
SLIDE 123

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types p u b l i c s t a t i c void increment ( i n t a ) { a = a + 1; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { i n t a = 5 ; increment ( a ) ; > }

5 a args

activation frame for main

Control returns to the main method, the activation frame for increment is destroyed

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-124
SLIDE 124

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types

References and method calls

c l a s s MyInteger { i n t v a l u e ; MyInteger ( i n t v ) { v a l u e = v ; } } c l a s s Test { p u b l i c s t a t i c void increment ( MyInteger a ) { a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { MyInteger a = new MyInteger ( 5 ) ; System . out . p r i n t l n ( ” b e f o r e : ” + a . v a l u e ) ; increment ( a ) ; System . out . p r i n t l n ( ” a f t e r : ” + a . v a l u e ) ; } }

What will be printed out?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-125
SLIDE 125

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types s t a t i c void increment ( MyInteger a ) { a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { > MyInteger a = new MyInteger ( 5 ) ; increment ( a ) ; }

a args 5 value

The local variable a of the main method is a reference to an instance of the class MyInteger

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-126
SLIDE 126

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types s t a t i c void increment ( MyInteger a ) { a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { MyInteger a = new MyInteger ( 5 ) ; > increment ( a ) ; }

a args a 5 value

Calling increment, creating a new activation frame

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-127
SLIDE 127

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types s t a t i c void increment ( MyInteger a ) { a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { MyInteger a = new MyInteger ( 5 ) ; > increment ( a ) ; }

a args a 5 value

Copying the value of the actual parameter into the formal parameter of increment

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-128
SLIDE 128

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types s t a t i c void increment ( MyInteger a ) { > a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { MyInteger a = new MyInteger ( 5 ) ; increment ( a ) ; }

a args a 6 value

Executing a.value++

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-129
SLIDE 129

Preamble Data types Call-by-value Scope Definitions Primitive data types Reference data types s t a t i c void increment ( MyInteger a ) { a . v a l u e++; } p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { MyInteger a = new MyInteger ( 5 ) ; increment ( a ) ; >}

a args 6 value

Returning the control to the main method

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-130
SLIDE 130

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-131
SLIDE 131

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name The Java Language Specification, Third Edition, Addison Wesley, p. 117.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-132
SLIDE 132

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope of a local variable in Java

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-133
SLIDE 133

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope of a local variable in Java

The scope of a local variable declaration in a block is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement The Java Language Specification, Third Edition, Addison Wesley, p. 118. ⇒ A.K.A. static or lexical scope

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-134
SLIDE 134

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope of a parameter Java

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-135
SLIDE 135

Preamble Data types Call-by-value Scope Definitions Examples

Definition: scope of a parameter Java

The scope of a parameter of a method or constructor is the entire body of the method or constructor The Java Language Specification, Third Edition, Addison Wesley, p. 118. ⇒ A.K.A. static or lexical scope

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-136
SLIDE 136

Preamble Data types Call-by-value Scope Definitions Examples

public c l a s s Test { public s t a t i c void d i s p l a y () { System . out . p r i n t l n ( ”a = ” + a ) ; } public s t a t i c void main ( S t r i n g [ ] args ) { i n t a ; a = 9; // v a l i d access , w i t h i n the same block i f ( a < 10) { a = a + 1; // another v a l i d a cc e s s } d i s p l a y ( ) ; } }

Is this a valid program?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-137
SLIDE 137

Preamble Data types Call-by-value Scope Definitions Examples

public c l a s s Test { public s t a t i c void main ( S t r i n g [ ] args ) { System . out . p r i n t l n ( sum ) ; for ( i n t i =1; i <10; i++ ) { System . out . p r i n t l n ( i ) ; } i n t sum = 0; for ( i n t i =1; i <10; i++ ) { sum += i ; } } }

Is this a valid program?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-138
SLIDE 138

Preamble Data types Call-by-value Scope Definitions Examples

public c l a s s Test { public s t a t i c void main ( S t r i n g [ ] args ) { for ( i n t i =1; i <10; i++ ) { System . out . p r i n t l n ( i ) ; } i n t sum = 0; for ( i n t i =1; i <10; i++ ) { sum += i ; } } }

Is this a valid program?

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-139
SLIDE 139

Preamble Data types Call-by-value Scope Definitions Examples

Memory management

What happens to objects when they are not referenced? Here what happens to the object that contains the value 99?

MyInteger a = new MyInteger ( 7 ) ; MyInteger b = new MyInteger ( 99 ) ; b = a ;

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-140
SLIDE 140

Preamble Data types Call-by-value Scope Definitions Examples

Memory management

What happens to objects when they are not referenced? Here what happens to the object that contains the value 99?

MyInteger a = new MyInteger ( 7 ) ; MyInteger b = new MyInteger ( 99 ) ; b = a ;

◮ The JVM recuperates the memory space ◮ This process is called garbage collection ◮ Not all programming languages manage memory automatically

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-141
SLIDE 141

Preamble Data types Call-by-value Scope Definitions Examples

Memory management

What happens to objects when they are not referenced? Here what happens to the object that contains the value 99?

MyInteger a = new MyInteger ( 7 ) ; MyInteger b = new MyInteger ( 99 ) ; b = a ;

◮ The JVM recuperates the memory space ◮ This process is called garbage collection ◮ Not all programming languages manage memory automatically

Java is not immune to memory leaks as will see in a few weeks. . .

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-142
SLIDE 142

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-143
SLIDE 143

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early ◮ Comparison operators always compare the value of the

expressions

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-144
SLIDE 144

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early ◮ Comparison operators always compare the value of the

expressions

◮ When comparing references we check that two references

designate the same object or not

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-145
SLIDE 145

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early ◮ Comparison operators always compare the value of the

expressions

◮ When comparing references we check that two references

designate the same object or not

◮ The method equals should be used to compare the content

  • f the objects

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-146
SLIDE 146

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early ◮ Comparison operators always compare the value of the

expressions

◮ When comparing references we check that two references

designate the same object or not

◮ The method equals should be used to compare the content

  • f the objects

◮ In Java, call-by-value is the mechanism that is used for

method calls

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-147
SLIDE 147

Preamble Data types Call-by-value Scope Definitions Examples

Summary

◮ Strong typing help detecting certain kinds of errors early ◮ Comparison operators always compare the value of the

expressions

◮ When comparing references we check that two references

designate the same object or not

◮ The method equals should be used to compare the content

  • f the objects

◮ In Java, call-by-value is the mechanism that is used for

method calls

◮ The scope of variable and parameter names is static in Java

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-148
SLIDE 148

Preamble Data types Call-by-value Scope Definitions Examples

Next lecture

◮ Introduction to object-oriented programming

◮ Role of abstractions ◮ Activities of software development ◮ UML – Unified Modeling Language ◮ Example: Counter Marcel Turcotte ITI 1121. Introduction to Computing II

slide-149
SLIDE 149

Preamble Data types Call-by-value Scope Definitions Examples

References I

  • E. B. Koffman and Wolfgang P. A. T.

Data Structures: Abstraction and Design Using Java. John Wiley & Sons, 2e edition, 2010.

  • P. Sestoft.

Java Precisely. The MIT Press, second edition edition, August 2005. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. Java Language Specification. Addison Wesley, 3rd edition, 2005.

Marcel Turcotte ITI 1121. Introduction to Computing II

slide-150
SLIDE 150

Preamble Data types Call-by-value Scope Definitions Examples

Please don’t print these lecture notes unless you really need to!

Marcel Turcotte ITI 1121. Introduction to Computing II