ITI 1121. Introduction to Computing II – subtitle
ITI 1121. Introduction to Computing II
Java Memory Aid
by
Marcel Turcotte
Version January 9, 2020
!
Preambule
Preambule
Overview
Overview
Java Memory Aid
A concise overview the Java language focusing on types declarations, control structures
and method calls. Object oriented programming, interface, and generics will be presented
later.
General objective:
By the end of this module, you should be able to write Java programs with a level of
complexity similar to those from the course ITI1120.
1 43
Preambule
Learning objectives
Learning Objectives
Name Java primitive types
Use Java predefined types
Declare variables
Develop applications that are using arrays
Use control structures to solve problems
Decompose large problems into smaller ones to create structured applications
Lectures:
Koffman & Wolfgang Appendice A (pages 541-555)
2 43
Types
Primitive types
The table below lists the characteristics of the primitif types. These types are also
predefined. In class, we discuss the differences between primitive and reference 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
3 43
Reference types
Reference types are: classes, interfaces, enum types, or arrays.
Reference types will be presented in later modules.
4 43
Type declaration
type
int age ;
identifier
One must declare the type of each variable and parameter, as well as that of the
returned value. Above, we are declaring that the variable age is of type int.
5 43
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 has not been declared.
Test.java:3: error: cannot find symbol
age = 21;
^
symbol: variable age
location: class Test
1 error
6 43
Solution
One must declare the type of the variable, here int (line 3), before using the
variable (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 }
7 43
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 that of the returned value.
8 43
Compile-time error: returned 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:
public sum(a, b) {
^
Test.java:2: error:
public sum(a, b) {
^
3 errors
9 43
Type of the returned value: void
Some methods do not return a result, this is the case for the method swap below,
the type must then be 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 ;
}
10 43
Variables
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.
11 43
Definition: scope of local variables en 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
12 43
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
13 43
Arrays
Reference to an array
type identifier
int [ ] xs ;
Declaring a reference variable pointing to an array.
The syntax [] is used to declare an array. The type that precedes the brackets is type
for each element of the array.
In the example above, xs is a reference to an array of integers.
Important. Declaring a reference to an array does not create the array. Only the
reference variable∗.
∗Consult the module on reference types
14 43
Creating an array
type
xs = new int [ 5 ] ;
size
Creating an array of integers of size 5.
Arrays are objects.
The keyword new is used to create an object
(which is created during the execution of the program).
The type of the object is array of integers.
The size of the array is 5.
The reference of the array is saved in the referencee variable named xs.
15 43
Accessing the content of an array
index
value = xs [ 0 ] ;
Accessing the content of an array.
One uses the name of the reference variable followed by index surrounded by square
brackets.
16 43
Declaring, creating, and accessing the
content of an array
The method main declares a local variable of type reference to an array of integers
(line 3), creates an array of integers of size 5 (line 4), and assigns the value 100 to
the position 0 of the array designed by xs (line 5).
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 [ ] x s ;
4 xs = new i n t [ 5 ] ;
5 xs [ 0 ] = 100 ;
6 }
7 }
17 43
Initializing an array
Line 4 shows how to create and initialize an array with values.
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 [ ] x s ;
4 xs = new i n t [ ] {0 , 1 , 2 , 3 , 4 , 5} ;
5 }
6 }
18 43
Declaring a reference variable for a
two-dimensional array (line 2).
Creating a two-dimensional array (line 3).
Assigning a value to one of the cells of a two-dimensional array
(lines 8 and 10).
1 i n t s i z e = 4 ;
2 double [ ] [ ] m;
3 m = new double [ s i z e ] [ s i z e ] ;
4
5 f o r ( i n t i =0; i , >= Greater than, greater than or equal
8 ==, != Equal, not equal
Examples:
i f ( age < 3) {
p r i c e = 0 . 0 ;
} e l s e i f ( age <= 13 | | age >= 65) {
p r i c e = 1 1 . 9 9 ;
} e l s e {
p r i c e = 1 4 . 5 0 ;
}
24 43
Statements
Statements
Each statement is terminated by a semi-colon (;)
The empty statement comprises only the semi-colon (;)
Zero or more statements can be grouped to form a bloc using parentheses. Such
bloc can be used everywhere a statement can be used.
25 43
Statements: body of a method
public int sum( int a , int b) {
int value ;
va lue = a+b ;
return value ;
}
The body of a method is a bloc of statements
26 43
Statement: if
i f ( age >= 18) {
vo t e r s = vot e r s + 1 ;
System . out . p r i n t l n ( ”Can vote ” ) ;
}
If the true-branch of an if statement comprises more than one statement, a bloc of
statements must be used.
It is recommended to always use a bloc with the branches of the if statement.
27 43
Statement: if-else
i f (100 .0⇤ exams /65 .0 < 50 . 0 ) {
grade = 100.0⇤ exams /65 . 0 ;
} else {
grade = l a b o r a t o r i e s + ass ignments + exams ;
}
If the false-branch of an if-else statement comprises more than one statement, a
bloc of statements must be used.
In the above example, the parentheses are not needed, but it is recommended to
put them.
28 43
Statement: for
for ( int i =0; i <10; i++) {
System . out . p r i n t l n ( i ) ;
}
initialization test increment
The for loop comprises three expressions: initialization, test, and increment.
It is recommended to declare the type of the variable controlling the loop within the
initialization of the loop to limit the scope of the variable to the body of the loop only.
29 43
Statement: while
i n t i ;
i = 0 ;
whi le ( i <10) {
System . out . p r i n t l n ( i ) ;
i ++;
}
The while statement.
The body of the loop is executed as long as the condition is true, here, as long as i<10
is true.
The code produces the same result as the previous example (for statement).
30 43
Statement: return
1 pub l i c s t a t i c i n t i ndexOf ( S t r i n g word , S t r i n g [ ] words ) {
2 f o r ( i n t i =0; i
It worked. But, we cannot run it:
> java Work
Error: Main method not found in class Work, please define
the main method as:
public static void main(String[] args)
Getting started
“main” method
Adding a main() method
We need to have a main method, a starting point.
pub l i c c l a s s Work {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
}
}
Now we can run it:
> javac Work.java
> java Work
It worked. But it does nothing . . .
Getting started
Printing something
Printing something
We can print something:
pub l i c c l a s s Work {
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 ( ” ye s ! ” ) ;
}
}
> javac Work.java
> java Work
yes!
Variables and Methods
Variables and Methods
Primitive Data Types
Primitive data types
Primitive Range Size Wrapper
byte −128 . . . 127 8 Byte
short −32, 768 . . . 32, 767 16 Short
int −231 . . . 231 − 1 32 Integer
long −263 . . . 263 − 1 64 Long
float roughly ±10−38 . . .± 1038 32 Float
double roughly ±10−308 . . .± 10308 64 Double
boolean “true” and “false” 1 (8?) Boolean
char Unicode character set 16 Character
Variables and Methods
Primitive Variables
Primitive variables
Let’s add some (primitive) variables in our code
Declaring a variable:
long myLong ;
Declaring and initializing a variable:
byte i = 33 ;
Accessing, modifying variables:
i n t myInt = 5 ;
i n t myOther Int = myInt ;
myOther Int = myOther Int + 5 ;
myInt++;
Primitive variables
pub l i c c l a s s Work {
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 myInt = 0 ;
long myLong ;
boolean myBool = f a l s e ;
char myChar = ’ c ’ ;
myLong = 3L ;
System . out . p r i n t l n ( ” myInt : ” + myInt + ” myLong : ” + myLong +
” myChar : ” + myChar + ” myBool : ” + myBool ) ;
}
}
> javac Work.java
> java Work
myInt: 0 myLong: 3 myChar:c myBool: false
Variables and Methods
Strings
Using Strings
pub l i c c l a s s Work {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ){
S t r i n g s = ” t h i s i s a s t r i n g ” ;
S t r i n g s2 = ” t h i s i s ano the r s t r i n g . ” ;
S t r i n g s3 = s+s2 ;
s=s . concat ( ” . ” ) ;
S t r i n g s4 = s2 . s u b s t r i n g ( 8 , 1 5 ) ;
System . out . p r i n t l n ( s ) ;
System . out . p r i n t l n ( s2 ) ;
System . out . p r i n t l n ( s3 ) ;
System . out . p r i n t l n ( s4 ) ;
System . out . p r i n t l n ( ” s c o n t a i n s \” s t r i n g \ ” : ” +
s . c o n t a i n s ( ” s t r i n g ” ) ) ;
System . out . p r i n t l n ( ” s c o n t a i n s \” s t r o n g \ ” : ” +
s . c o n t a i n s ( ” s t r o n g ” ) ) ;
}
}
Using Strings
> javac Work.java
> java Work
this is a string.
this is another string.
this is a stringthis is another string.
another
s contains “string”: true
s contains “strong”: false
>
See https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Variables and Methods
Adding and Calling a Method
Methods
pub l i c c l a s s Work {
/∗ This i s a method ∗/
pub l i c s t a t i c vo id do ( ) {
System . out . p r i n t l n ( “Hey , l ook ! ” ) ;
}
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
do ( ) ; // c a l l i n g do
}
}
> javac Work.java
> java Work
Hey, look!
Syntax
Syntax
The “For” loop
Loop: for (;;) {}
pub l i c c l a s s U t i l s {
pub l i c s t a t i c vo id do ( ) {
f o r ( i n t i =0; i < 10 ; i ++) {
System . out . p r i n t l n ( " I t e r a t i o n : " + ( i +1)) ;
}
}
}
pub l i c c l a s s Work {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ){
U t i l s . do ( ) ;
}
}
Loop: for (;;) {}
> javac Work.java
> java Work
Iteration : 1
Iteration : 2
Iteration : 3
Iteration : 4
Iteration : 5
Iteration : 6
Iteration : 7
Iteration : 8
Iteration : 9
Iteration : 10
Syntax
The “While” loop
Loop: while () {}
pub l i c c l a s s U t i l s {
pub l i c vo id do ( ) {
i n t i = 0 ;
whi le ( i <10) {
System . out . p r i n t l n ( " I t e r a t i o n : " + ( i +1)) ;
i ++;
}
}
}
pub l i c c l a s s Work {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ) {
U t i l s . do ( ) ;
}
}
Loop: while () {}
> javac Work.java
> java Work
Iteration : 1
Iteration : 2
Iteration : 3
Iteration : 4
Iteration : 5
Iteration : 6
Iteration : 7
Iteration : 8
Iteration : 9
Iteration : 10
Syntax
The “If.. Else” statement
Statement: if() {} else {}
pub l i c c l a s s U t i l s {
pub l i c vo id do ( boolean f o rma lPa ramete r ) {
i f ( fo rma lParamete r ) {
System . out . p r i n t l n ( ” I went through the \” i f \” c l a u s e ” ) ;
} e l s e {
System . out . p r i n t l n ( ” I went through the \” e l s e \” c l a u s e ” ) ;
}
}
}
pub l i c c l a s s Work {
pub l i c s t a t i c vo id main ( S t r i n g [ ] a r g s ){
U t i l s . do ( f a l s e ) ;
}
}
Statement: if () {} else {}
> javac Work.java
> java Work
I went through the “if” clause
I went through the “else” clause
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.
Marcel Turcotte
(Marcel. )
Guy-Vincent Jourdan
( )
School of Electrical Engineering and Computer Science (EECS)
University of Ottawa
Marcel.
Preambule
Overview
Learning objectives
Types
Variables
Arrays
Expressions
Statements
Scanner
Convention
Prologue
Appendix
Prerequisites
Source Code Editor
Command prompt
Getting started
Simplest Class
“main” method
Printing something
Variables and Methods
Primitive Data Types
Primitive Variables
Strings
Adding and Calling a Method
Syntax
The “For” loop
The “While” loop
The “If.. Else” statement