Variables and Operators
Variables and Operators
Lecture Outline
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
3
3
❖ Their values can be changed at any point over the course of a program.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
4 4
❖ The rest of the identifier can include any character except those used as operators
in Java such as + , - , * , /, %.
❖ In addition, there are certain keywords reserved (e.g., "class") in the Java language
which can never be used as identifiers.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
5 5
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
6 6
Pop Quiz
❖ Which of the following are valid variable names?
1. $amount Valid
4. salary Valid
5. _score Valid
7. total# Valid
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
8
8
❖ Each variable's declared type does not change over the course of the program.
❖ If you try to perform an operation on an illegal data type (like multiplying Strings),
the compiler will report an error.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
10 10
Data Types
❖ There are eight built-in (primitive) data types in the Java language
❖ 4 integer types (byte, short, int, long)
❖ Boolean (boolean)
❖ Character (char)
**see Appendix II: Summary of Primitive Data Types for a complete table of sizes and formats**
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
11
Integer Data Types
❖ There are four data types that can be used to store integers.
❖ The one you choose to use depends on the size of the number that we
want to store.
Data Type Value Range
byte -128 to +127
short -32768 to +32767
int -2147483648 to +2147483647
long -9223372036854775808 to +9223372036854775807
❖ In this course, we will always use int when dealing with integers.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT 11
Tuesday, June 1, 2021
12 12
❖ smallValue = -55;
❖ Note: By adding an L to the end of the value in the last example, the program is
“forced” to consider the value to be of a type long even if it was small enough to be an
int.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
13 13
❖ The one you choose to use depends on the size of the number that we want to store.
❖ In this course, we will always use double when dealing with decimal values.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
14 14
❖ Note: in the last example we added an F to the end of the value. Without the F,
it would have automatically been considered a double instead.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
15 15
❖ Example:
❖ boolean switchOn = true;
❖ boolean fileOpen = false;
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
16 16
❖ Example:
❖ char firstLetterOfName = 'e’ ;
❖ Note that you need to use singular quotation marks when assigning
char data types.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
17 17
Introduction to Strings
❖ Strings consist of a series of characters inside double quotation marks.
❖ Examples statements assign String variables:
❖ String coAuthor = "John Smith";
❖ String password = "swordfish786";
❖ Strings are not one of the primitive data types, although they are very
commonly used.
❖ Strings are constant; their values cannot be changed after they are
created.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
18 18
Pop Quiz
What data types would you use to store the following types of
information?
1. Population of Ethiopia int
2. Approximation of π double
3. Open/closed status of a file Boolean
4. Your name String
5. First letter of your name char
6. $237.66 double
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
19 19
Operators
❖ Operators are special symbols used for
❖ mathematical functions
❖ assignment statements
❖ logical comparisons
❖ Examples:
❖ 3+5 // uses + operator
❖ 14 + 5 – 4 * (5 – 3) // uses +, -, * operators
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
21 21
Arithmetic Operators
❖ Java has 6 basic arithmetic operators
+ add
- subtract
* multiply
/ divide
% modulo (remainder)
(10 + 15) / 5 = 5
10 + (15 / 5) = 13
Integer Division
❖ In the previous example, we were lucky that (10 + 15) / 5 gives an exact integer
answer (5).
❖ Depending on the data types of the variables that store the numbers, we will get
different results. int i = 63; double x = 63;
❖ The result of integer division is just the integer part of the quotient!
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
24 24
Assignment Operator
❖ The basic assignment operator (=) assigns the value of var to expr.
❖ var = expr ;
❖ Java allows you to combine arithmetic and assignment operators into a single operator.
❖ Examples:
❖ x = x + 5; is equivalent to x += 5;
❖ y = y * 7; is equivalent to y *= 7;
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
25 25
++count; or count++;
--count; or count--;
int x = 3;
int y = 5;
boolean result;
1. result = (x > y); now result is assigned the value false because 3 is not
greater than 5
2. result = (15 == x*y); now result is assigned the value true because the
product of 3 and 5 equals 15
3. result = (x != x*y); now result is assigned the value true because the
product of x and y (15) is not equal to x (3)
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
29
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
32 32
❖ Java will evaluate these expressions from left to right and so will evaluate
a before (b++ > 3)
x before y
❖ Java performs short-circuit evaluation:
❖ it evaluates && and || expressions from left to right and once it finds the result,
it stops.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
33 33
❖ Java will not evaluate the right-hand expression (b++ > 3) if the left-hand operator a is false,
since the result is already determined in this case to be false. This means b will not be
incremented!
❖ (x || y)
❖ Similarly, Java will not evaluate the right-hand operator y if the left-hand operator x is true,
since the result is already determined in this case to be true.
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
34 34
Pop Quiz
1) What is the value of number?
-12
int number = 5 * 3 – 3 / 6 – 9 * 3;
2) What is the value of result?
int x = 8;
int y = 2; false
boolean result = (15 == x * y);
3) What is the value of result?
int x = 7;
boolean result = (x < 8) && (x > 4); true
REFERENCES
• Summary of Java operators
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html
1. Parentheses
2. Exponents
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
36
Appendix I: Reserved Words
The following keywords are reserved in the Java language. They can
never be used as identifiers:
abstract assert boolean break byte
case catch char class const
continue default do double else
extends final finally float for
goto if implements import instanceof
int interface long native new
package private protected public return
short static strictfp super switch
synchronized this throw throws transient
try void violate while
2. Variables and Operators, Object Oriented Programming,
36
Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
37 37
The following tables show all of the primitive data types along with
their sizes and formats:
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021
39 39
Questions???
2. Variables and Operators, Object Oriented Programming, Electrical and Computer Engineering, DMiT Tuesday, June 1, 2021