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

ITI 1121. Introduction to Computing II – subtitle

ITI 1121. Introduction to Computing II
Data types: primitive and reference types

by

Marcel Turcotte

Version January 9, 2020

Preamble

Preamble

Overview

Data types: primitive and reference types

We examine the advantages of strongly typed languages. We compare primitive types and
reference types. We introduce memory diagrams.

General objective :
This week, you will be able to contrast primitive types and reference types.

Preamble

Learning objective

Learning objective

Name predefined primitive types and references.
Illustrate associations between objects using memory diagrams.

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

2 34

Preamble

Plan

Plan

1 Preamble

2 Theory

3 Prologue

3 34

Theory

Definition: Variable

What is a variable?
A variable is an abstraction for a memory location, which is referred to using a
label in a high-level programming language.

33(i) 0,000,123,456

4,294,967,296

0,000,000,000

0,000,123,457

0,000,123,455

i = 33 ;

4 34

Convention

I will use Greek letters to designate memory (addresses) since in Java we don’t know the
location of objects and shouldn’t worry about it.

33(i)

α + 1

α− 1
α

i = 33 ;

5 34

Definition: Data type

What are data types?
A data type provides information on the representation in memory of the data
(e.g. range of possible values) as well as the operations that are defined for this data.

6 34

Discussion: Data types

But then again, who benefits from data types?
The compiler to reserve the necessary memory space for the data.
The compiler, but also to the programmer, in order to detect certain errors at
compile time — applying an operation undefined for a particular data type.

7 34

Discussion: Data types

Give examples of data types?
byte, short, int, long
float, double
boolean
char

8 34

Predefined types

Type Size Maximum Examples
boolean 1 true,false
char 16 ’\uFFFF’ ’a’, ’A’, ’1’, ’*’
byte 8 127 -128, -1, 0, 1, 127
short 16 32767 -128, -1, 0, 1, 127
int 32 2147483647 -128, -1, 0, 1, 127
long 64 9223372036854775807 -128L, 0L, 127L
float 32 3.4028235E38 -1.0f, 0.0f, 1.0f
double 64 1.7976931348623157E308 -1.0, 0.0, 1.0

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

https://docs.oracle.com/javase/specs/jls/se12/html/jls-4.html

9 34

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/specs/jls/se12/html/jls-4.html

Java : Data types

Java is a strongly typed language. Which means that every variable and every
expression have a type known at the time of compilation.
One must declare the type of each variable and parameter, as well as the type of
the return value of the methods.

type

int age ;
identifier

10 34

Compile time error: «cannot find symbol»

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 ) {

age = 21 ;
}

}

In the above example, the variable age was not declared.

Test.java:3: error: cannot find symbol
age = 21;
^

symbol: variable age
location: class Test

1 error

11 34

Solution

One must declare the type of the variable, here int (line 3), before using it (line 4).

1 pub l i c c l a s s Test {
2 pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
3 i n t age ;
4 age = 21 ;
5 }
6 }

12 34

Type declaration: methods

type type type

public int sum( int a , int b) {
return a+b ;

}
One must declare the type of each parameter, as well as the type of the return
value of the methods.

13 34

Compile time error:
return value and parameters

pub l i c c l a s s Test {
pub l i c sum( a , b ) {

re tu rn a+b ;
}

}

Test.java:2: error: invalid method declaration; return type required
public sum(a, b) {

^
Test.java:2: error: expected

public sum(a, b) {
^

Test.java:2: error: expected
public sum(a, b) {

^
3 errors

14 34

Type of the return value: void

Some methods do not return any result, this is the case of the method swap
below, the type of the return value is then void (“returns nothing”).

pub l i c s t a t i c vo id swap ( i n t [ ] x s ) {
i n t tmp ;
tmp = xs [ 0 ] ;
x s [ 0 ] = xs [ 1 ] ;
x s [ 1 ] = tmp ;

}

15 34

Type of the return value
(compile time error)

pub l i c c l a s s Test {
pub l i c s t a t i c swap ( i n t [ ] x s ) {

i n t tmp ;
tmp = xs [ 0 ] ;
x s [ 0 ] = xs [ 1 ] ;
x s [ 1 ] = tmp ;

}
}

> javac Test.java
Test.java:2: error: invalid method declaration; return type required

public static swap(int [] xs) {
^

1 error

16 34

Type and assignment
(compile time error)

pub l i c c l a s s Test {
pub l i c s t a t i c vo id t e s tType s ( ) {

boolean b ;
b = ” t r u e ” ;

}
}

> javac Test.java
Test.java:4: error: incompatible types: String cannot be converted to boolean

b = “true”;
^

1 error

17 34

Solution

pub l i c c l a s s Test {
pub l i c s t a t i c vo id t e s tType s ( ) {

boolean b ;
b = t rue ;

}
}

18 34

Types and expressions

pub l i c c l a s s Test {
pub l i c s t a t i c vo id t e s tType s ( ) {

i f (3 < 4 && 0) { System . out . p r i n t l n ( " Bingo ! " ) ; } } } > javac Test.java
Test.java:3: error: bad operand types for binary operator ’&&’

if (3 < 4 && 0) { ^ first type: boolean second type: int 1 error 19 34 Solution pub l i c c l a s s Test { pub l i c s t a t i c vo id t e s tType s ( ) { i f (3 < 4 && ’ a ’ == ’ a ’ ) { System . out . p r i n t l n ( " Bingo ! " ) ; } } } 20 34 Java: Data types (continued) There are primitive types and reference types. What is a primitive type? What is a reference type? 21 34 Java: data types (continued) The primitive types are: numbers (byte, int, long, float, double), characters (char, but not the strings) and boolanns (booleans) the value of a variable of a primitive type is found at the address designated by the label (identifier). References: Predefined: Arrays Strings Types defined by the user, references to objects. The value of a reference type variable is the address of the memory location of the object designated by the variable — it is said that the variable points, designates or references the object. 22 34 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} ; 23 34 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 designates 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. > i n t [ ] a ;
a = new i n t [ 5 ] ;

(a)


α null

The declaration of a variable of type reference does not create the object (instance),
the compiler will reserve enough space to contain the reference (pointer), null is a literal
which means: does not designate any object.

26 34

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

0

0
0

(a)

0

α null

0

β(instance)

The creation of an object, new int[ 5 ], reserves a portion of memory for 5 integers (and
for internal management — housekeeping). Each cell in the array behaves as a variable of
type int and receives the initial value 0.

27 34

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

0

0
0

(a)

0

α

0

β

β

(instance)

Finally, the reference of the new object is saved at the address designated by the label a.

28 34

Theory

Memory representation

Memory diagrams

Since we don’t know the location of objects in memory (and shouldn’t worry about it),
we’ll use memory diagrams (rightmost image).

0

0
0

(a)

0

α

0

β

β

(instance) ⇒
0

0
0

(a)

0

α

0

β

β

(instance)


0

0
0

a

0
0

29 34

Memory diagram

Rules for your memory diagrams:
One box for each reference variable and one arrow to the designated object.
A box for each variable of type primitive type and its value in the box itself.

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

0

0
0

a

0
0

30 34

Prologue

Summary

A variable is an abstraction for a memory location that is referred to using a label.
A data type provides information on the representation in memory of the data
(e.g. range of possible values) as well as the operations that are defined for this data.
The value of a variable of a primitive type is found at the address designated by
the label (identifier).
The value of a variable of reference type is the address of the memory location of
the object designated by the variable.
In Java, you have to declare the type of variables.

31 34

Next module

Data types (part 2)

32 34

References I

E. B. Koffman and Wolfgang P. A. T.
Data Structures: Abstraction and Design Using Java.
John Wiley & Sons, 3e edition, 2016.

D. J. Barnes and M. Kölling.
Objects First with Java: A Practical Introduction Using BlueJ.
Prentice Hall, 4e edition, 2009.
P. Sestoft.
Java Precisely.
The MIT Press, second edition edition, August 2005.

33 34

Marcel Turcotte
Marcel.

School of Electrical Engineering and Computer Science (EECS)
University of Ottawa

34 / 34

Marcel.

Preamble
Overview
Learning objective
Plan

Theory
Memory representation

Prologue