Data types Expressions Variables Assignment · Data types Data come in different types.! ......
Transcript of Data types Expressions Variables Assignment · Data types Data come in different types.! ......
![Page 1: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/1.jpg)
Data types Expressions
Variables Assignment
COMP1400 – Week 2
![Page 2: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/2.jpg)
Data types
Data come in different types.
The type of a piece of data describes:
• What the data means.
• What we can do with it.
![Page 3: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/3.jpg)
Primitive types
Java defines a selection of “primitive” data types.
They are called primitive because they are the basic building blocks we can use to construct more complex types.
![Page 4: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/4.jpg)
Binary Numbers
• All data in a computer are stored as binary numbers, e.g.
11012= 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
= 8+4+0+1= 13
![Page 5: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/5.jpg)
Computer Memory
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 byte = 8 bits
byte
short int
int and float
long int and double
![Page 6: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/6.jpg)
Primitive typesType Keyword Meaning Examples
integer int whole numbers
0, 1, 100, -3322
floating point
numberfloat fractional
numbers
0.0f, 1.5f, -32.7f, 3.14159f
![Page 7: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/7.jpg)
Extended typesType Keyword Meaning Examples
long integer
long whole numbers
0L, 9223372036854775807L
double-precision floating point
number
double fractional numbers
0.0000000000000000000000000000000000000000000000001
!100000000000000000000000000000000000000000000000000
![Page 8: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/8.jpg)
Primitive typesType Keyword Meaning Examples
boolean boolean true or falsetrue false
character char
characters !
(letters, numerals, symbols)
'a' 'A' '1' ' '
![Page 9: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/9.jpg)
Representing numbers
There are two types for representing numbers.
Use int when you know you are deal with whole numbers (e.g. when counting things)
Use double when you have to deal with fractions.
![Page 10: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/10.jpg)
Representing numbers
Numbers have internal limitations on their precision.
An int can only represent values between -2,147,483,648 and 2,147,483,647.
A double can represent larger values but significant digits are lost.
10000000000000000.0 + 1 = 10000000000000000.0
![Page 11: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/11.jpg)
Memory can encode anything
• characters, strings, database records, music, images, video, weather models, etc
• numbers can be addresses to other locations in memory
• instructions in computer program
![Page 12: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/12.jpg)
Expressions
An expression is a formula for computing a value.
Examples:
1 + 1 1.0 / 3 (1.5 * 3) + 9 - 2
![Page 13: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/13.jpg)
Integer expressionsThere are five basic operators you can use to make integer expressions:
Symbol Purpose Example+ addition 3 + 4 = 7- subtraction 3 - 4 = -1* multiplication 3 * 4 = 12/ integer division 14 / 3 = 4 % remainder 14 % 3 = 2
![Page 14: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/14.jpg)
Float expressionsThe operators have a slightly different meaning when applied to doubles and floats:
Symbol Purpose Example+ addition 3.5 + 4.0 = 7.5- subtraction 3.5 - 4.0 = -0.5* multiplication 3.0 * 4.0 = 12.0/ division 14.0 / 3.0 = 4.66666 % remainder 7.5 % 3 = 1.5
![Page 15: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/15.jpg)
Order of operationsWhen computing an expression the operators *, / and % take precedence over + and -.
Otherwise order is left-to-right.
1 + 4 * 3 = 13
2 - 5 / 3 = 1
2 + 4 % 3 = 3
5 - 2 + 3 = 6
![Page 16: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/16.jpg)
Order of operationsUse parentheses ( and ) to vary the order:
So:
(1 + 4) * 3 = 15
(2 - 5) / 3 = -1
(2 + 4) % 3 = 0
5 - (2 + 3) = 0
![Page 17: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/17.jpg)
Code Pad
BlueJ has a Code Pad tool to allow you to test snippets of code.
![Page 18: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/18.jpg)
Code Pad
Expression
Result (Type)
![Page 19: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/19.jpg)
VariablesWhen we compute a value we usually want to store it somewhere so we can use it later.
A variable is a named container for storing a single piece of data of a specified type.
Name Type Value
count int 0
age double 37.5
![Page 20: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/20.jpg)
VariablesThe value of a variable can change. When a new value is put into a variable, the old one is forgotten.
The type of a variable cannot change.
Name Type Value
count int 17
age double 38.0
![Page 21: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/21.jpg)
Variable declarationBefore we use a variable we must declare it, specifying its name and type.
We can optionally specify an initial value. Otherwise it is set to zero (for ints and floats).
int count; // initially 0 double age = 37.5;
type name initial value
semicolon
![Page 22: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/22.jpg)
AssignmentOnce we have declared a variable we can change its value as often as we like, using an assignment statement:
count = 3; // count is now 3
count = 4 + 5; // count is now 9
count = count + 1; // count is now 10
![Page 23: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/23.jpg)
AssignmentThe format of an assignment statement:
count = 4 + 5;
!
The value of the expression on the right is the new value for the variable.
It must have the same type as the variable.
variable equals expression
semicolon
![Page 24: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/24.jpg)
Semicolons
Notice that any single statement in Java has to end with a semicolon (;).
This is the way Java knows you have finished one statement and are starting the next.
![Page 25: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/25.jpg)
Variable naming
A variable name can be any sequence of letters and numbers or the “_” (underscore) character.
Good naming:
1. Is meaningful.
2. Has a consistent style.
![Page 26: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/26.jpg)
Variable namingThe recommend style is ‘bumpyCaps’ style (aka ‘camelCase’).
Start with a lowercase letter.
String words together with an uppercase letter starting each word:
age, numberOfChildren, taxFileNumber, oldLength
![Page 27: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/27.jpg)
Good Style
Good style makes code readable by providing meaningful names:
age = 37.5;
numChildren = 2;
distToTarget = 50;
![Page 28: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/28.jpg)
Bad Style
Bad style makes code unreadable by providing obscure names or mismatched styles:
a = 37.5;
n_c = 2;
Dt = 50;
![Page 29: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/29.jpg)
Variables and expressions
Once a variable has been declared, it can be used in expressions to compute new values:
int count = 7;
int size = 3;
int spaceNeeded = count * size;
// spaceNeeded is now 21
![Page 30: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/30.jpg)
Variables and expressions
A variable can even be used in an expression that is assigned to itself:
int count = 7;
count = count * 2;
// count is now 14
![Page 31: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/31.jpg)
Variables and expressions
This kind of operation is so common that there are shorthands:
count += 2; // count = count + 2
count -= 2; // count = count - 2
![Page 32: Data types Expressions Variables Assignment · Data types Data come in different types.! ... Primitive types Java defines a selection of “primitive” data types. ! They are called](https://reader034.fdocuments.us/reader034/viewer/2022042601/5f6c32003c937f50e911fd75/html5/thumbnails/32.jpg)
Variables and expressions
count *= 2; // count = count * 2
count /= 2; // count = count - 2
count++; // count = count + 1
count--; // count = count - 1