Lecture5-2024
Lecture5-2024
SCIENCE
CSI141
PROGRAMMING
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l PRINCIPLES
Programming Principles T ALLMAN NKGAU
L ECTURE 5 – F UNDAMENTAL
D ATA TYPES
‣ Everyday data
‣ Number Types, variables, &
assignment, output
http://horstmann.com/bjlo/index.html
L EARNING OUTCOMES
to:
expressions/statements
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
number types
‣ Everyday data
‣ Number Types, variables, &
assignment, input/output
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
Everyday data
• We work with numbers, text (strings), single characters, & a
combination of characters
Numbers
Numbers/Date?/String
String – a sequence of
characters
5
M ONEY IS AN ABSTRACT HUMAN
HAPPINESS , SO WHO IS NO
LONGER ABLE TO APPRECIATE THE
TRUE HUMAN HAPPINESS , IS
COMPLETELY DEDICATED TO IT . –
A RTHUR SCHOPENHAUE
‣ Everyday data
‣ Number Types, variables, &
assignment, input/output
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
Abstract View of RAM
https://en.wikipedia.org/wiki/Random-access_memory#/media/File:Swissbit_2GB_PC2-5300U-555.jpg
0
❑ We view memory as a sequential list of cells; 1
each 1 byte in size 2
❑ Each cell has an address associated with it Byte Address 3
❑ The example shown here will correspond to have 4
4096 bytes of RAM (= 4KB of RAM). [1KB = 1024 ⋮
bytes] 1023
4095
7
Integer data type
❑ Java provides a means to work with whole numbers.
❑ Java uses named memory locations to hold numbers.
— they make it easy to access the values stored – just use the
name. A variable is a storage
— they are called variables location with a name
— they can’t hold all the possible numbers. Not enough memory
for that!
long 8 integers +, -, /, %, * −𝟐 𝟔𝟑 𝟐 𝟔𝟑 − 𝟏
8
Integer data type, declaration
❑ A data type is a set of values and operations on them.
— For example: integers (…,-2,-1,0,1,2,…) and operations (add,
subtract, multiply, remainder, and divide)
❑ For a Java program to store an integer value, it MUST declare its intent to
the compiler – this is called static typing
❑ Declaration statements follow the following syntax:
<< type>> variableName;
9
Java identifiers
❑ Identifiers are case sensitive
• mySalary and mysalary are different identifiers
10
Java keywords
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
11
POP QUIZ
Which of these are valid Java variable names?
A. catch
B. lean
C. 1stBorn
D. last$
E. PI
F. annual_salary
G. Int
H. Long
I. top!
J. “aws”
K. some body Page 12
POP QUIZ
Which of these are valid Java variable names?
A. catch
B. lean
C. 1stBorn
D. last$
E. PI
F. annual_salary
G. Int
H. Long
I. top!
J. “aws”
K. some body Page 13
Java Variable Naming Convention – Lower Camel Case
14
Pause…
N = 674532
t = 0
while N > 0
r = N % 10 // Remainder operation
if r % 2 == 0
t = t + r
N = N / 10 // Integer division
return t
numberOfChildren
byte numberOfChildren;
short numOfSchools; numOfSchools
int population;
long numOfPlanes; population
numOfPlanes
Q. How much memory is used
by the 4 variables?
A. 1 + 2 + 4 + 8 = 15 bytes Memory diagram showing the
meaning of a declaration
16
Assignment statement – used to assign a value to a
variable
variableName = expression;
17
Assignment statement – examples
Declaration Assignment
population 2500000
Memory diagram
18
Expressions
❑ Expressions are your normal arithmetic expressions,
expressed as Java expressions.
❑ Expressions can involve other variables.
int divisor = 5;
int groupsOfFive;
int total = 250;
Page 20
Expressions
𝒙𝟐 +𝒙+𝟏
❑ The Math expression
𝟐+𝒚
is written as
int divisor = 5;
int groupsOfFive;
int total = 250;
Page 27
Other operators
Operator Example Meaning Comment
++ b = i++; b = i; i = i + 1; post-increment
c = ++i; i = i + 1; b = i; pre-increment
-- b = i--; b = i; i = i - 1; post-decrement
c = --i; i = i - 1; b = i; pre-decrement
+= b += x + y; b = b + (x + y);
-= b -= x + y; b = b - (x + y);
*= b *= x + y; b = b * (x + y);
/= b /= x + y; b = b / (x + y);
%= b %= x + y; b = b % (x + y);
Page 28
POP QUIZ
Given c = 5; i = 3, x = -3 (all integer variables)
Evaluate the following (independently).
A. i++;
B. y = ++c – x++; // assume y is int
C. c %= 2*i + x / 2;
D. y = ++c + --i * x++; // assume y is int
Page 29
Summary
• Know the number data types: byte, short, int, long, float, double
• Know how to declare variables to hold each of the different number data
Page 30
Exercises
1. R2.1 – R2.5
Page 31