Lecture 02 Variables and Operators
Lecture 02 Variables and Operators
Lecture 2
Outline
• Variables are places where information can be stored while a program is running.
• Their values can be changed at any point over the course of a program
4
Creating Variables
• To create a variable, declare its name and the type of information
that it will store.
• The type is listed first, followed by the name.
int highScore ;
type name
5
highScore = 98;
String studentName;
boolean gameOver;
6
Naming Variables
• The name that you choose for a variable is called an identifier.
In Java, an identifier can be of any length, but must start with:
a letter (a – z),
a dollar sign ($),
or, an underscore ( _ ).
Naming (Continued)
• Java is a case-sensitive language – the capitalization of
letters in identifiers matters.
A rose is not a Rose is not a ROSE
pageCount/page_count
loadFile/load_file
anyString/any_string
threeWordVariable/three_word_variable
9
POP QUIZ
• Which of the following are valid variable names?
1)$amount
2)6tally
3)my*Name
4)salary
5)_score
6)first Name
7)total#
10
Statements
• A statement is a command that causes something to
happen.
• Example:
System.out.println(“Hello, World”);
**see Appendix II: Summary of Primitive Data Types for a complete table of
sizes and formats**
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
float 1.4×10-45 to 3.4×1038
double 4.9×10-324 to 1.7×10308
• In this course, we will always use double when dealing with decimal
values.
17
• Example:
boolean monsterHungry = true;
boolean fileOpen = false;
19
• Example:
▫ char firstLetterOfName = 'e' ;
▫ char myQuestion = '?' ;
Introduction to Strings
• Strings consist of a series of characters inside double quotation marks.
• 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.
21
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
6)$237.66 double
22
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
Arithmetic Operators
Order of Operations
• Example: 10 + 15 / 5;
(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).
• double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8
Assignment Operator
• The basic assignment operator (=) assigns the value of var to expr
var = expr ;
y = y * 7; is equivalent to y *= 7;
29
Increment/Decrement Operators
count = count + 1;
can be written as:
++count; or count++;
count = count - 1;
can be written as:
--count; or count--;
-- is called the decrement operator.
30
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = ++numOranges + numApples;
numFruit has value 16
numOranges has value 6
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = numOranges++ + numApples;
numFruit has value 15
numOranges has value 6
Relational (Comparison) Operators
• Relational operators compare two values
• Produces a boolean value (true or false)
depending on the relationship
operation is true when . . .
a > b a is greater than b
a >= b a is greater than or equal to b
a == b a is equal to b
a != b a is not equal to b
a <= b a is less than or equal to b
a < b a is less than b
31
32
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)
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
33
Truth Table for Conditional Operators
x y x && y x || y !x
34
35
(x || y) evaluates to true
(true && x) evaluates to true
• Java will evaluate these expressions from left to right and so will
evaluate
a before (b++ > 3)
x before y
Short-Circuit Evaluations
(x || y)
POP QUIZ
1) What is the value of number?
int number = 5 * 3 – 3 / 6 –-12
9 * 3;
References
2. Exponents
The following keywords are reserved in the Java language. They can never be used as
identifiers:
40
41
Real Numbers