CS计算机代考程序代写 prolog data structure jvm compiler Java ITI 1121. Introduction to Computing II – subtitle

ITI 1121. Introduction to Computing II – subtitle

ITI 1121. Introduction to Computing II
Data types: operator and method calls

by

Marcel Turcotte

Version January 19, 2020

Preamble

Preamble

Overview

Overview

Data types: operator and method calls

We examine the advantages of strongly typed programming languages. We compare
primitive types and reference types at the level of comparison operators and method calls.

General objective :
This week you will be able to contrast primitive types and reference types at the level
of comparison operators and method calls.

1 73

Preamble

Learning objectives

Objectifs d’apprentissage

Compare the evaluation of primitive and reference type expressions.
Enumerate the steps in a method call.
Compare method calls depending on the case where the parameters are of primitive
type and the case where the parameters are of reference type.

Readings:
Pages 545-551, 571-572 of E. Koffman & P. Wolfgang.

2 73

Preamble

Plan

Plan

1 Preamble

2 Memory diagrams

3 « Wrappers »

4 Operators

5 Method call

6 Scope

7 Prologue

3 73

Reminder : Primitive vs reference and the
TC-1101

i n t pos ;
pos = 5 ;

i n t [ ] x s ;
x s = new i n t [ ] {1 , 2 , 3} ;

4 73

00
91

08
10
39

09
00

64
5
00

90
12

1
2
3

27
91

12

ALU

Control

MAR

MDR

OpCode OpAddr A

PC

H
Z
N

R/W

0000

0001

0002

0003

0004

0005

0006

0007

0008

0009

0010

0011

0012

0013

0014

0015

0016

0017

9999

Memory Microprocessor

The variable pos is of type int, a primitive type, if pos designates the address 00 08,
then the value 5 is saved at the address 00 08.

00
91

08
10
39

09
00

64
5
00

90
12

1
2
3

27
91

12

ALU

Control

MAR

MDR

OpCode OpAddr A

PC

H
Z
N

R/W

0000

0001

0002

0003

0004

0005

0006

0007

0008

0009

0010

0011

0012

0013

0014

0015

0016

0017

9999

Memory Microprocessor

The variable xs is of type reference to an array of integers, if xs is the address 00 09,
then the value of the cells 00 09 and 00 10, is the address where the array was saved
in memory, 00 12. At address 00 12 is the array, with its three values 1, 2, and 3.

Memory diagrams

Memory diagram

Given the following class declaration:
c l a s s Constant {

S t r i n g name ;
double v a l u e ;
Constant ( S t r i n g n , double v ) {

name = n ;
v a l u e = v ;

}
}

Draw the memory diagram that corresponds to these statements:
Constant c ;
c = new Constant ( ” go lden r a t i o ” , 1 . 61803399 ) ;

7 73

Memory diagram

1.6180339887

c

value

name “golden ratio”

an instance
of the class
String

an instance
of the class
Constant

a reference
variable of type
Constant

8 73

Memory diagram

1.6180339887

c

value

name “golden ratio”

an instance
of the class
String

an instance
of the class
Constant

a reference
variable of type
Constant

9 73

Classe Integer

For the following few examples, we will use a class named Integer:
c l a s s I n t e g e r {

i n t v a l u e ;
}

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

Dot notation is used to access the instance variables of an object.

10 73

Class Integer

Adding a constructor:
c l a s s I n t e g e r {

i n t v a l u e ;
I n t e g e r ( i n t v ) {

v a l u e = v ;
}

}

Utilisation:
I n t e g e r a ;
a = new I n t e g e r ( 3 3 ) ;

11 73

Primitive and reference types

i

33

i n t i ;
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 ( 3 3 ) ;
a l i a s = i ;

On the right, during compilation, a portion of memory is reserved for the reference
variables i and alias.

12 73

Primitive and reference types

i

33

i n t i ;
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 ( 3 3 ) ;
a l i a s = i ;

Creating an object during the execution “new Integer(33)”.

13 73

Primitive and reference types

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 ( 3 3 ) ;
a l i a s = i ;

Save the reference of this object in the reference variable i.

14 73

Primitive and reference types

i

33

i n t i ;
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 ( 3 3 ) ;
a l i a s = i ;

Copy the value of the reference variable i into the variable alias.

15 73

Primitive and reference types

i

33

i n t i ;
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 ( 3 3 ) ;
a l i a s = i ;

i and alias refer to the same object!

16 73

Primitive and reference types

i 33

i n t i ;
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 ( 3 3 ) ;
a l i a s = i ;

Diagramme de mémoire.

17 73

« Wrappers »

“Wrapper” classes

For each primitive type there is an associated wrapper class.
Integer is the wrapper class for the type int.
A wrapper object “stores” a value of a primitive type in an object.
We’ll use them with stacks, queues, lists and trees.
Wrapper classes also have several methods for data conversion, e.g.
Integer.parseInt(“33”).

18 73

“Quiz”

Are those Java statements valid, true or false?
I n t e g e r i ;
i = 1 ;

If they’re valid, what conclusions do you draw from them?
1 is a value of a primitive type, int, but i is a reference variable of type Integer.
For Java 1.2 or 1.4, this statement will produce a compilation compile time error.
However, for Java 5, or newer version, the statement is valid! Why?

19 73

Auto-boxing

Java 5, or newer, transform automagically the statement
I n t e g e r i = 1 ;

into this one
I n t e g e r i = I n t e g e r . va lueOf ( 1 ) ;

Where valueOf “[r]eturns an Integer instance representing the specified int value”∗. We
call this auto-boxing.

∗https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Integer.html
20 73

https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Integer.html

Auto-unboxing

Likewise, the statement i = i + 5:
I n t e g e r i = 1 ;
i = i + 5 ;

is transformed into this one:
i = I n t e g e r . va lueOf ( i . i n t V a l u e ( ) + 5 ) ;

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

21 73

Boxing/unboxing

Primitive Reference
byte Byte
short Short
int Integer
long Long
float Float
double Double
bool Boolean
char Character

The eight primitive types have their associated wrapper. The automatic conversion from
a primitive type to a reference type is called boxing,and the conversion from a reference
type to a primitive type is called unboxing.

22 73

Should I be concerned?

i n t s1 ;
s1 = 0 ;
f o r ( i n t j =0; j <1000; j++) { s1 = s1 + 1 ; } I n t e g e r s2 ; s2 = 0 ; f o r ( i n t j =0; j <1000; j++) { s2 = s2 + 1 ; } 9 093 nanosecondes 335 023 nanosecondes Pourquoi? On the right side, s2 is of type Integer, so the statement s2 = s2 + 1 ; is rewritten (automatically) like this s2 = I n t e g e r . va lueOf ( s2 . i n t V a l u e ( ) + 1 ) ; 23 73 Programming Tip: performance testing 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 f o r ( i n t j =0; j <10000000; j++) { s2 += 1 ; // s t a n d s f o r ‘ s2 = s2 + 1 ’ } s top = System . c u r r e n t T i m e M i l l i s ( ) ; // s top 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 seconds that have elapsed since midnight, January 1, 1970 UTC (Coordinated Universal Time). System.nanoTime() also exists! 24 73 Operators Operators Comparison operators Comparison operators: primitive data types Comparison operators compare the values! 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 result will be printed out?

⇒ Affiche « a < b » 25 73 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

10b

α

β

26 73

Comparison operators:
primitive and reference types
What’s the result?
i n t a = 5 ;
I n t e g e r b = I n t e g e r . va lueOf ( 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,Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,Integer else if (a == b) ^ 2 errors 27 73 i n t a = 5 ; I n t e g e r b = I n t e g e r . va lueOf ( 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

5value

References.java:7: operator < cannot be applied to int,Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,Integer else if (a == b) ^ 2 errors 28 73 i n t a = 5 ; I n t e g e r b = I n t e g e r . va lueOf ( 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

5value

References.java:7: operator < cannot be applied to int,Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,Integer else if (a == b) ^ 2 errors 29 73 i n t a = 5 ; I n t e g e r b = I n t e g e r . va lueOf ( 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

α

β

5value

γ

γ

References.java:7: operator < cannot be applied to int,Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,Integer else if (a == b) ^ 2 errors 30 73 Remarks These error messages are produced by pre-1.5 compilers. For 1.5 and up, autoboxing will (possibly) hide the “problem”. To get the same behavior for both environments, let’s use our own wrapper class, MyInteger. 31 73 Classe MyInteger 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 ; } } 32 73 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

5value

Correct these statements!

33 73

Solution

i n t a = 5 ;
MyInteger b = new MyInteger ( 5 ) ;

i f ( a < b . v a l u e ) { 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 . v a l u e ) { 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 i s g r e a t e r than b" ) ; } ⇒ Prints « a equals b » 34 73 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 q u a l s b" ) ; } e l s e { System . out . p r i n t l n ( " a does not equa l b" ) ; } ⇒ Prints « a does not equal b » 35 73 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 ... 5value 5value ... ... ⇒ Prints « a does not equal b » 36 73 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 5value 5value ⇒ Prints « a does not equal b » 37 73 Solution MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a . e q u a l s ( 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 b" ) ; } where the equals method would have been defined as follows pub l i c boolean e q u a l s ( MyInteger o t h e r ) { boolean answer = f a l s e ; i f ( v a l u e == o t h e r . v a l u e ) { answer = t rue ; } r e t u r n s answer ; } ⇒ Prints « a equals b » 38 73 What’s the result? 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" ) ; } ⇒ Displays " a == b ", why? because the references a and b designate the same object, the same instance, in other words, the two memory addresses are identical; we say that a and b are aliases. 39 73 Comparison operators and reference types What’s the result? 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 e q u a l s b" ) ; } e l s e { System . out . p r i n t l n ( " a does not equa l b" ) ; } ⇒ Prints « a equals b » 40 73 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 5value 41 73 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 5value 42 73 Comparison operators and reference types What’s the result? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 1 0 ) ; 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 equa l b" ) ; } Less.java:14: operator < cannot be applied to MyInteger,MyInteger if (a < b) { ^ 1 error 43 73 Remarks To compare the contents of the designated objects, “content equivalence” or “logical equivalence”, we use equals† To know if two reference variables designate the same object, we use the comparison operators ’==’ and ’!=’. i f ( a . e q u a l s ( b ) ) { . . . } vs i f ( a == b ) { . . . } †To be continued. . . 44 73 Exercises Compare objects two by two using either equals or ==, you could be surprised. S t r i n g a = new S t r i n g ( " H e l l o " ) ; S t r i n g b = new S t r i n g ( " H e l l o " ) ; i n t c [ ] = { 1 , 2 , 3 } ; i n t d [ ] = { 1 , 2 , 3 } ; S t r i n g e = " H e l l o " ; S t r i n g f = " H e l l o " ; S t r i n g g = f + " " ; In particular, try a == b and e == f. 45 73 Method call Method call Definitions Definition: arity The arity of a method is the number of parameters; a method has none, one or more parameters. MyInteger ( ) { v a l u e = 0 ; } MyInteger ( i n t v ) { v a l u e = v ; } i n t sum( i n t a , i n t b ) { re tu rn a + b ; } 46 73 Definition: formal parameter A formal parameter is a variable that is part of the signature of the method; it can be seen as a local variable in the body of the method. i n t sum( i n t a , i n t b ) { re tu rn a + b ; } ⇒ a and b are the formal parameters of sum. 47 73 Definition: actual parameter The actual parameter is the value that is used during the method call and provides the initial value to the formal parameter. i n t sum( i n t a , i n t b ) { re tu rn a + b ; } . . . i n t midTerm , f ina lExam , t o t a l ; t o t a l = sum( midTerm , f i na lExam ) ; midTerm and finalExam are the actual parameters of the call to the method sum, during the call the value of the actual parameter is copied to the location of the formal parameter. 48 73 Concept: call-by-value In Java, during a method call, the value of the actual parameter is copied to the location of the formal parameter. When a method is called: the execution of the calling method is interrupted a block of working memory is created ‡ (which contains the formal parameters and local variables) The values of the actual parameters are assigned to the formal parameters. the body of the method is executed the return value is saved (the working memory block is destroyed) the execution of the calling method continues with the instruction that follows the method call ‡activation frame 49 73 Method call Primitive types pub l i c c l a s s Test { pub l i c s t a t i c vo id i n c r ement ( i n t a ) { a = a + 1 ; } pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) { i n t a = 5 ; System . out . p r i n t l n ( " b e f o r e : " + a ) ; i n c r ement ( a ) ; System . out . p r i n t l n ( " a f t e r : " + a ) ; } } What’s the result? b e f o r e : 5 a f t e r : 5 50 73 pub l i c s t a t i c vo id i n c r ement ( i n t a ) { a = a + 1 ; } pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) { > i n t a = 5 ;
i nc r ement ( a ) ;

} 5a
args activation

frame for
main

Each method call has its own working memory (activation block), which serves to save
the parameters and local variables for this call (here, args and a).

51 73

pub l i c s t a t i c vo id i n c r ement ( i n t a ) {
a = a + 1 ;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t a = 5 ;
> inc r ement ( a ) ;

} 5a
args activation

frame for
main

activation
frame for
increment

a

When calling the method increment a new block of working memory is created.

52 73

pub l i c s t a t i c vo id i n c r ement ( i n t a ) {
a = a + 1 ;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t a = 5 ;
> inc r ement ( a ) ;

} 5a

5

args activation
frame for
main

activation
frame for
increment

a

The value of each actual parameter is copied to the location of the corresponding
formal parameter.

53 73

pub l i c s t a t i c vo id i n c r ement ( i n t a ) {
> a = a + 1 ;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t a = 5 ;
i nc r ement ( a ) ;

} 5a

6

args activation
frame for
main

activation
frame for
increment

a

The execution of the statement a = a + 1 changes the value of the formal parameter a,
a memory location distinct from that of the local variable a of the method main.

54 73

pub l i c s t a t i c vo id i n c r ement ( i n t a ) {
a = a + 1 ;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t a = 5 ;
i nc r ement ( a ) ;

> } 5a
args activation

frame for
main

The control returns to the method main, the working memory for the method increment
is destroyed.

55 73

Method call

Reference variables

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 {

pub l i c s t a t i c vo id i n c r ement ( MyInteger a ) {
a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

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 ) ;
i n c r ement ( a ) ;
System . out . p r i n t l n ( ” a f t e r : ” + a . v a l u e ) ;

}
}

What’s the result?

56 73

s t a t i c vo id i n c r ement ( MyInteger a ) {
a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

> MyInteger a = new MyInteger ( 5 ) ;
i n c r ement ( a ) ;

} a
args

5value

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

57 73

s t a t i c vo id i n c r ement ( MyInteger a ) {
a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

MyInteger a = new MyInteger ( 5 ) ;
> inc r ement ( a ) ;

} a
args

a

5value

Calling increment, creating an activation block.

58 73

s t a t i c vo id i n c r ement ( MyInteger a ) {
a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

MyInteger a = new MyInteger ( 5 ) ;
> inc r ement ( a ) ;

} a
args

a

5value

Copy the value from the actual parameter to the location of the formal parameter.

59 73

s t a t i c vo id i n c r ement ( MyInteger a ) {
> a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

MyInteger a = new MyInteger ( 5 ) ;
i n c r ement ( a ) ;

} a
args

a

6value

Executing a.value++

60 73

s t a t i c vo id i n c r ement ( MyInteger a ) {
a . v a l u e ++;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

MyInteger a = new MyInteger ( 5 ) ;
i n c r ement ( a ) ;

>} a
args

6value

Return the control to the method main.

61 73

pub l i c c l a s s TestSwapArray Int {
pub l i c s t a t i c vo id swap ( i n t [ ] x s ) {

i n t [ ] y s ;
y s = new i n t [ 2 ] ;
y s [ 0 ] = xs [ 1 ] ;
y s [ 1 ] = xs [ 0 ] ;
x s = ys ;

}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t [ ] x s ;
x s = new i n t [ 2 ] ;
x s [ 0 ] = 15 ;
xs [ 1 ] = 21 ;
swap ( xs ) ;
System . out . p r i n t l n ( xs [ 0 ] ) ;
System . out . p r i n t l n ( xs [ 1 ] ) ;

}
}

Scope

Scope

Definitions

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.

63 73

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

64 73

Définition: scope of a parameter in Java

The scope of a formal parameter of a method (§8.4.1) or constructor (§8.8.1)
is the entire body of the method or constructor.

The Java Language Specification,
Third Edition, Addison Wesley, p. 118.

⇒ A.K.A. portée statique ou lexicale

65 73

Scope

Examples

pub l i c c l a s s Test {
pub l i c s t a t i c vo id d i s p l a y ( ) {

System . out . p r i n t l n ( ” a = ” + a ) ;
}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {

i n t a ;
a = 9 ; // v a l i d acce s s , w i t h i n the same b l o ck
i f ( a < 10) { a = a + 1 ; // ano the r v a l i d a c c e s s } d i s p l a y ( ) ; } } Is this a valid Java program? 66 73 pub l i c c l a s s Test { pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) { System . out . p r i n t l n ( sum ) ; f o r ( i n t i =1; i <10; i ++) { System . out . p r i n t l n ( i ) ; }x i n t sum = 0 ; f o r ( i n t i =1; i <10; i ++) { sum += i ; } } } Is this a valid Java program? 67 73 pub l i c c l a s s Test { pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) { f o r ( i n t i =1; i <10; i ++) { System . out . p r i n t l n ( i ) ; } i n t sum = 0 ; f o r ( i n t i =1; i <10; i ++) { sum += i ; } } } Is this a valid Java program? 68 73 Memory management What happens to the objects when there’s no reference to them? Here, what happens to the object holding the value 99? MyInteger a = new MyInteger ( 7 ) ; MyInteger b = new MyInteger ( 9 9 ) ; b = a ; The JVM will recover the associated memory space! This process is called garbage collection Some programming languages do not automatically handle memory allocations and de-allocations. Java, however, is not immune to memory leaks, to be continued. . . 69 73 Prologue Summary Comparison operators compare the values of the variables. In particular, if a and b are reference variables, then a == b returns true ssi a and b designates the same object. Method calls are per value in Java. 70 73 Next module Object-oriented programming 71 73 References I E. B. Koffman and Wolfgang P. A. T. Data Structures: Abstraction and Design Using Java. John Wiley & Sons, 3e edition, 2016. 72 73 Marcel Turcotte Marcel. School of Electrical Engineering and Computer Science (EECS) University of Ottawa 73 / 73 Marcel. Preamble Overview Learning objectives Plan Memory diagrams « Wrappers » Operators Comparison operators Method call Definitions Primitive types Reference variables Scope Definitions Examples Prologue