Fundamentals of Computing 1
Lecture Title:
Data type, Variables, Expressions, Operators
8/11/2025 Lecture 02
1
Agenda
➢ Variables and Data types
Expressions
Operators
Constants
8/11/2025 Lecture 02
2
Variable - Definition
Variable: A variable in programming is
a named storage location in memory that
can hold a value.
Example: int x = 20;
8/11/2025 Lecture 02
3
Data types
Data type: A category or set of data
values.
There are two kinds of Data types
◼ Primitive Data Types
◼ Reference/Object Data Types (we'll talk
about later)
8/11/2025 Lecture 02
4
Primitive types
Java supports 8 primitive types: byte, short,
int, long, float, double, boolean and char.
They are called primitive types because they are
not classes
8/11/2025 Lecture 02
5
8 primitive types
Numeric Data Types
Name Range Size in Bytes
byte -27 (-128) to 27 - 1(127) 8-bit signed
short -215 (-32768) to 215 - 1(32767) 16-bit signed
int -231 (-2147483648) to 231 - 1(2147483647) 32-bit signed
long -263 to 263 - 1 64-bit signed
float Negative range: -3.4028235E + 38 to -1.4E-45 32-bit IEEE 754
Positive range: 1.4E-45 to 3.4028235E + 38
double Negative range: -1.7976931348623157E+308 to -4.9E-324 64-bit IEEE 754
Positive range: 4.9E-324 to 1.7976931348623157E+308
boolean true/false 1-bit
char Minimum value is '\u0000' (or 0). 16-bit Unicode
Maximum value is '\uffff' (or 65,535 inclusive). character
8/11/2025 Lecture 02
6
Steps for using a variable
Declare it: state its name and type
Initialize it: store a value into it
Use it: print it or use it as part of an
expression
8/11/2025 Lecture 02
7
Variable - Declaration
Variables must be declared before they can be
used.
Syntax:
DataType identifier;
Or
DataType identifier1, identifier2;
◼int x;
◼double myGPA;
8/11/2025 Lecture 02
8
Variable – Declaration and
Initialization
A variable can be declared/initialized in one
statement.
Syntax:
DataType identifier = initialValue;
Example:
◼ int x = 3; //literal value
◼ double myGPA =myGPA = 1.0 + 2.25;
8/11/2025 Lecture 02
9
Using variables
Once given a value, a variable can be used
int x = 3;
System.out.println("x is " + x); // x is 3
You can assign a value more than once:
int x = 3;
System.out.println(x + " here"); // 3 here
x = 11;
System.out.println("now x is " + x); // now x is 11
int age = 32;
age = age + 1; // now, age is 33
System.out.println(age); //33
8/11/2025 Lecture 02
10
Agenda
✓ Variables and Data types
➢ Expressions
Operators
Constants
8/11/2025 Lecture 02
11
Expressions
Consists of operators, operands and
parentheses that evaluate to single
value
◼ Examples: 1 + 4 * 5
(7 + 2) * 6 / 3
42 // literal value
◼ The simplest expression is a literal value.
8/11/2025 Lecture 02
12
Assignment Operator
Syntax:
variable = expression;
Example:
◼ int numberOfPlayers;
numberOfPlayers = 3;
◼ double myGPA;
myGPA = 10 + 2.25;
Expression: is a literal value or a single variable
name
int legalAge = 18;
voterAge = legalAge;
8/11/2025 Lecture 02
13
Compiler errors
A variable can't be used until it is assigned a value.
◼ int x;
System.out.println(x); // ERROR: x has no value
You may not declare the same variable twice.
◼ int x;
int x; // ERROR: x already exists
◼ int x = 3;
int x = 5; // ERROR: x already exists
How can this code be fixed?
8/11/2025 Lecture 02
14
Agenda
✓ Data type
✓ Expressions
➢ Operators
Variable
Constant
8/11/2025 Lecture 02
15
Arithmetic Operators
operator: Combines multiple values or expressions.
◼+ addition
◼- subtraction
◼* multiplication
◼/ division
◼% modulus (remainder after division)
As a program runs, its expressions are evaluated.
◼ System.out.println(1 + 1); //prints 2
◼ System.out.println(3 * 4); // prints 12
8/11/2025 Lecture 02
16
Integer division with /
When we divide integers, the quotient is also an
integer.
◼ 14 / 4 is 3, not 3.5
More examples:
◼ 32 / 5 is 6
◼ 84 / 10 is 8
◼ 156 / 100 is 1
8/11/2025 Lecture 02
17
Integer remainder with %
The % operator computes the remainder from
integer division. What is the result?
◼ 14 % 4 is 2 45 % 6
◼ 218 % 5 is 3 2 % 2
8 % 20
Applications of % operator: 11 % 0
◼ Obtain last digit of a number: 230857 % 10 is 7
◼ Obtain last 4 digits: 658236489 % 10000 is
6489
◼ See whether a number is odd: 7 % 2 is 1, 42 % 2 is 0
8/11/2025 Lecture 02
18
Precedence
precedence: Order in which operators are evaluated.
◼ Generally operators evaluate left-to-right.
1 - 2 - 3 is (1 - 2) - 3 which is -4
◼ But * / % have a higher level of precedence than + -
1 + 3 * 4 is 13
6 + 8 / 2 * 3
6 + 4 * 3
6 + 12 is 18
◼ Parentheses can force a certain order of evaluation:
(1 + 3) * 4 is 16
◼ Spacing does not affect order of evaluation
1+3 * 4-2 is 11
8/11/2025 Lecture 02
19
Precedence examples
1 * 2 + 3 * 5 % 4 ◼ 1 + 8 % 3 * 2 - 9
\_/ ◼ \_/
| |
2 + 3 * 5 % 4 1 + 2 * 2 - 9
\_/ ◼ \___/
| |
2 + 15 % 4 1 + 4 - 9
\___/ ◼ \______/
| |
2 + 3 5 - 9
\________/ ◼ \_________/
|
| -4
5
8/11/2025 Lecture 02
20
Precedence questions
What values result from the following
expressions?
◼9 / 5
◼ 695 % 20
◼7 + 6 * 5
◼7 * 6 + 5
◼ 248 % 100 / 5
◼6 * 3 - 9 / 4
◼ (5 - 7) * 4
◼ 6 + (18 % (17 - 12))
8/11/2025 Lecture 02
21
Operators on real numbers
(type double)
The operators + - * / % () all still work
with double.
◼ 15.0 / 2.0 ?
◼ 7.9 % 3.2 ?
Precedence is the same: () before * / %
before + -
8/11/2025 Lecture 02
22
Real number expression
2.0 * 2.4 + 2.25 * 4.0 / 2.0
\___/
|
4.8 + 2.25 * 4.0 / 2.0
\___/
|
4.8 + 9.0 / 2.0
\_____/
|
4.8 + 4.5
\____________/
|
9.3
8/11/2025 Lecture 02
23
Mixing types
When int and double are mixed, the result is a double.
◼ 4.2 * 3 is 12.6
The conversion is per-operator, affecting only its operands.
• 2.0 + 10 / 3 * 2.5 - 6 / 4
◼ 7 / 3 * 1.2 + 3 / 2
• \___/
◼ \_/ |
| 2.0 + 3 * 2.5 - 6 / 4
2 * 1.2 + 3 / 2
• \_____/
◼ \___/ |
| 2.0 + 7.5 - 6 / 4
2.4 + 3 / 2
• \_/
◼ \_/ |
| 2.0 + 7.5 - 1
2.4 + 1
• \_________/
◼ \________/ |
| 9.5 - 1
3.4
• \______________/
|
◼ 3 / 2 is 1 above, not 1.5. 8.5
8/11/2025 Lecture 02
24
Division by 0
Integer type:
int result = 4 / 0 ;
System.out.println(result); // runtime error
◼ Integer division by 0 generates an error when your
program runs.
Double type:
◼ Double division by 0 doesn’t generates an error, but
the result is not a number
double result1 = 4.3 / 0.0 ;
System.out.println(result1); //prints Infinity
double result2 = 0.0 / 0.0 ;
System.out.println(result1); //prints NaN
8/11/2025 Lecture 02
25
Shortcut Operators
Shortcut Example Equivalent statement
operator
++ a++; or ++a; a = a+1;
-- a-- ; or --a; a = a -1;
+= a+=3; a = a +3;
-= a-=10; a = a - 10;
*= a*=4; a = a*4;
/= a/=7; a = a/7;
%= a%=2; a = a%2;
8/11/2025 Lecture 02
26
Order of Operator Precedence
Operator Order of Same Operation
statement Evaluation
() left to right Parentheses for explicit
grouping
++,-- right to left Shortcut posincrement,
posdecrement
++,-- right to left Shortcut preincrement,
predecrement
*,/,% left to right Multiplication, division, modulus
+,- left to right Addition or String
concatenation, subtraction
=, +=, -=, right to left Assignment and shortcut
*=, /=,%= assignment operators
8/11/2025 Lecture 02
27
Agenda
✓ Data type
✓ Expressions
✓ Operators
➢ Constants
8/11/2025 Lecture 02
28
Constants
The value of a variable may change during the
execution of the program, but a constant
represents permanent data that never changes
Syntax for declaring a constant:
◼ final datatype CONSTANTNAME = VALUE;
Example:
final double PI = 3.14159;
❑ A constant must be declared and initialized in
the same statement.
❑ By convention, constants are named in all
uppercase: PI, not pi or Pi.
8/11/2025 Lecture 02
29
Benefits of using constants
Reliable behavior: By preventing
accidental changes.
Improved code clarity: Through
meaningful names.
Increased code reusability: By sharing
constants across your program.
8/11/2025 Lecture 02
30
Agenda
✓ Data type
✓ Expressions
✓ Operators
✓ Variables
✓ Constants
8/11/2025 Lecture 02
31