Computer Programming
Applications
تطبيقات فى برمجة الحاسب
Lecture #3
Course Presentation by
Dr. Mohamed Ali
Learning Objectives
• Section I: Using the Building Blocks: Variables,
Values, and Types.
2
Section I: Using the Building Blocks: Variables, Values, and Types
• Introduction,
• Variables, Values, and Types,
• Definitions,
• Expressions,
• Java Constant.
3
Introduction
• Back in 1946, John von Neumann wrote a groundbreaking paper about
the newly emerging technology of computers and computing.
• Among other things, he established one fundamental fact: For all their
complexity, the main business of computers is to move data from one
place to another.
• Take a number the balance in a person’s bank account. Move this
number from the computer’s memory to the computer’s processing unit.
• Add a few dollars to the balance and then move it back to the computer’s
memory.
• The movement of data . . . that’s all there is; there ain’t no more.
• This lecture shows you how to move around your data.
4
Variables, Values, and Types
• What is the variable:
• Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in the
memory.
• Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.
• Therefore, by assigning different data types to variables, you can store
integers, decimals, or characters in these variables.
class OrderCalculation {
public static void main(String args[]) {
double amount;
amount = 5.95;
amount = amount + 25.00; //adding taxes here
[Link]("We will bill $");
[Link](amount);
[Link](" to your credit card.");
}
}
5
Variables & Data type
• Variables are reserved memory locations to store values. This
means that when you create a variable you reserve some space in
the memory.
• A data type is a set of values and a set of operations defined on
those values.
• There are two data types available in Java:
• Primitive: Which include Integer, Character, Boolean, and Floating
Point.
• Non-primitive: Which include Classes, Interfaces, and Arrays.
6
Variables
• There are two data types available in Java:
• Primitive Data Types.
• Reference/Object Data Types.
• Java Variables, Following are the types of variables in Java:
• Local Variables
• Class Variables (Static Variables)
• Instance Variables (Non-static Variables)
7
Variables
• When programming in Java, you must always be aware of the variables and
it's type of data that your program is processing.
• You are familiar with various types of numbers, such as integers and real
numbers, and with operations defined on them, such as addition and
multiplication.
• There are eight primitive types of data in Java, mostly for different kinds of
numbers.
• int for integers, double for real numbers, and boolean for true-false values,
type String for strings of characters, Byte, Long , Short and float.
8
Primitive Data type
Data Type Size in Bits Range of Values
boolean 1 true or false
Default value is false
Example: boolean one = true
Byte 8 –128 to +127
Default value is 0
Example: byte a = 100, byte b = -50
Char 16 Minimum value is '\u0000' (or 0)
Maximum value is '\uffff' (or 65,535 inclusive)
Example: char letterA = 'A'
Short 16 –32,768 to +32,767
Default value is 0
Example: short s = 10000, short r = -20000
9
Primitive Data type
Data Type Size in Bits Range of Values
Int 32 –2,147,483,648 (-2^31) to +2,147,483,647 (2^31 -1)
The default value is 0
Example: int a = 100000, int b = -200000
Long 64 –9,223,372,036,854,775,808 (-2^63) to
+9,223,372,036,854,775,807 (2^63 -1)
Default value is 0L
Example: long a = 100000L, long b = -200000L
float 32 Approximately 10–38 to 10+38; 7 significant digits
Default value is 0.0f
Example: float f1 = 234.5f
double 64 Approximately 10–308 to 10+308; 15 significant digits
Default value is 0.0d
Example: double d1 = 123.4
10
Definitions
• When talking about variables and data types, we need to introduce some
terminology.
int a, b, c;
a = 1234;
b = 99;
c = a + b;
• The first line is a declaration that declares the names of three variables to be the
identifiers a, b, and c and their type to be int.
• The next three lines are assignment statements that change the values of the
variables, using the literals 1234 and 99, and the expression a + b, with the end
result that c has the value 1333.
• When we talk about variables in programming we have to understand the following
definitions
• Identifiers, Literals and Variables.
• Declaration statements.
• Assignment statements.
• Initialization.
11
Identifiers, Literals and Variables
Identifiers:
• We use identifiers to name variables or many other things, in Java (Ex.
method or class).
• An identifier is a sequence of letters, digits, _, and $, the first of which is
not a digit.
• The sequences of characters abc, Ab$, abc123, and a_b are all legal Java
identifiers.
• But Ab*, 1abc, and a+b are not valid identifiers.
• Identifiers are case-sensitive, so Ab, ab, and AB are all different names.
You cannot use certain reserved words, such as public, static, int, double, and
so forth to name variables.
int a, b, c; // Identifiers for variables a, b and c
a = 1234;
b = 99;
c = a + b;
12
Identifiers, Literals and Variables
Literals:
• Is a source-code representation of a data-type value.
• We use strings of digits like 1234 or 99 to define int literal values, and add a
decimal point as in 3.14159 or 2.71828 to define double literal values.
• To specify a boolean value, we use the keywords true or false, and to specify
a String, we use a sequence of characters enclosed in quotes, such as "Hello,
World".
• We will consider other kinds of literals as we consider each data type in more
detail.
int a, b, c;
a = 1234; // Literals for variables a
b = 99; // Literals for variables b
c = a + b; // Literals for variables c
13
Identifiers, Literals and Variables
Variables:
• A variable is a name that we use to refer to a data-type value.
• We use variables to keep track of changing values as a computation unfolds.
For example, we use the variable n in many programs to count things.
• We create a variable in a declaration that specifies its type and gives it a
name.
• We compute with it by using the name in an expression that uses operations
defined for its type.
• Each variable always stores one of the permissible data-type values.
int a, b, c; // create 3 variables a, b and c with a data type integers
a = 1234;
b = 99;
c = a + b;
14
Declaration & Assignment Statements
• Declaration statements: A declaration statement associates a variable name
with a type at compile time.
• Java requires us to use declarations to specify the names and types of
variables. By doing so, we are being explicit about any computation that we
are specifying.
• Java is said to be a strongly-typed language, because the Java compiler can
check for consistency at compile time (for example, it does not permit us to
add a String to a double).
• This situation is precisely analogous to making sure that quantities have the
proper units in a scientific application (for example, it does not make sense to
add a quantity measured in inches to another measured in pounds).
• Declarations can appear anywhere before a variable is first used—most often,
we put them at the point of first use.
15
Declaration & Assignment Statements
• Assignment statements: An assignment statement associates a data-type
value with a variable.
• When we write c = a + b in Java, we are not expressing mathematical
equality, but are instead expressing an action: set the value of the variable
a to be the value of a plus the value of b.
• It is true that c is mathematically equal to a + b immediately after the
assignment statement has been executed, but the point of the statement is to
change the value of c (if necessary).
• The left-hand side of an assignment statement must be a single variable;
the right-hand side can be an arbitrary expression that produces values of
the type. For example, we can say discriminant = b*b - 4*a*c in Java, but we
cannot say a + b = b + a or 1 = a.
• In short, the meaning of = is decidedly not the same as in mathematical
equations. For example, a = b is certainly not the same as b = a, and while the
value of c is the value of a plus the value of b after c = a + b has been
executed, that may cease to be the case if subsequent statements change the
values of any of the variables.
16
Declaration & Assignment Statements
17
Declaration & Assignment Statements, the Initialization
• Initialization: In a simple declaration, the
initial value of the variable is undefined.
• For economy, we can combine a
declaration with an assignment statement
to provide an initial value for the variable.
• Initialization refers to the "assignment"
of a value, at construction time. For a
generic object of type T, it's often in the
form: T x = i;
• To initialize a variable. It can be done at
the time of declaration.
18
Understanding Assignment Statements
• You should get into the habit of reading assignment statements from right to
left. For example, the first assignment statement, Assign 5.95 to the amount
variable.
• The second assignment statement is just a bit more complicated. Reading the
second assignment statement from right to left, you get “Add 25.00 to the value
that’s already in the amount variable and make that number (30.95) be the new
value of the amount variable.”
19
Understanding Assignment Statements
• In an assignment statement, the thing being assigned a value is always on the
left side of the equal sign.
20
Combining Variable Declarations
• The code has only one variable, as if variables are in short supply. You can get
the same effect with several variables.
• The new code has three declarations, one for each of the program’s three
variables. Because all three variables have the same type, the type double.
• So which is better, one declaration or three declarations?
Neither is better. It’s a matter of personal style.
class SnitSoft { class SnitSoftNew {
public static void main(String args[]) { public static void main(String args[]) {
double amount; double cdPrice;
amount = 5.95; double shippingAndHandling;
amount = amount + 25.00; double total;
[Link]("We will bill $"); cdPrice = 5.95;
[Link](amount); shippingAndHandling = 25.00;
[Link](" to your credit card."); total = cdPrice + shippingAndHandling;
}
} [Link]("We will bill $");
[Link](total);
[Link](" to your credit card.");
}
}
21
Combining Variable Declarations
• Which of the following is a valid variable declaration and assignment
statements?
double cdPrice;
double shippingAndHandling;
double total;
double cdPrice, shippingAndHandling, total;
shippingAndHandling = 25.00;
double cdPrice, shippingAndHandling = 25.00, total;
22
Java operators
• Arithmetic Operators.
• Relational Operators.
• Bitwise Operators.
• Logical Operators.
• Assignment Operators.
• Misc Operators.
23
The Arithmetic Operators
• Assume this
int a = 10; int b = 20; int c = 25; int d = 25;
Operator Description Example
+ (Addition) Adds values on either side of the operator. A + B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand A - B will give -10
operand.
* (Multiplication) Multiplies values on either side of the A * B will give 200
operator.
/ (Division) Divides left-hand operand by right-hand B / A will give 2
operand.
% (Modulus) Divides left-hand operand by right-hand B % A will give 0
operand and returns remainder.
++ (Increment) Increases the value of operand by 1. B++ gives 21
-- (Decrement) Decreases the value of operand by 1. B-- gives 19
24
Expressions
• An expression is a literal, a variable, or a sequence of operations on literals
and/or variables that produces a value.
• For primitive types, expressions look just like mathematical formulas, which
are based on familiar symbols or operators that specify data-type operations to
be performed on one or more operands. Each operand can be any expression.
• Most of the operators that we use are binary operators that take exactly two
operands, such as x + 1 or y / 2.
• An expression that is enclosed in parentheses
is another expression with the same value.
• For example, we can write 4 * (x - 3) or 4*x - 12
on the right-hand side of an assignment
statement and the compiler will understand
what we mean.
25
Expressions & Precedence
• Precedence: Such expressions are shorthand for specifying a sequence of
computations: in what order should they be performed?
• Java and all programming languages has natural and well-defined precedence
rules, that fully specify this order.
• For arithmetic operations, multiplication and division (* & /) are performed
before addition and subtraction (+ & -).
• So that a-b*c and a-(b*c) represent the same sequence of operations.
• When arithmetic operators have the same precedence, the order is
determined by left-associativity, so that a-b-c and (a-b)-c represent the same
sequence of operations.
• You can use parentheses to override the rules, so you should not need to
worry about the details of precedence for most of the programs that you write.
26
What is a Constant?
• Constants are basically variables whose value can't change. In
C/C++, the keyword const is used to declare these constant
variables. In Java, you use the keyword final.
• A constant in Java is used to map an exact and unchanging value
to a variable name.
• The constant value cannot change once it has been assigned.
• Constants are used in programming to make code a bit more robust
and human readable.
• Imagine you are creating a program that needs to calculate areas
and volumes of different shapes, it could look something like this.
27
What is a Constant?
• The Java language doesn't have built-in support for constants, but the
variable modifiers static and final can be used to effectively create
one.
public class AreasAndVolumes {
// here we've declared a new variable called PI and assigned
// the value of 3.14159 to this new variable.
public static void main(String args[]) {
final double PI = 3.14159;
double radius = 3, height = 2;
[Link]("The volumn Of Sphere: " + (4 / 3)
* [Link](PI * radius, 3));
[Link]("The volume Of Cylinder: " + [Link](radius * PI, 2)
* height);
[Link]("The area Of Circle: " + [Link](radius * PI, 2));
}
}
28
Thank you
29