DATA TYPES, VARIABLES,
CONSTANTS AND EXPRESSION
LECTURE 2
MOHAMED J. ABEID – ZANZIBAR UNIVERSITY
[email protected]Contents.
• Overview of Data Types
• Introduction to Variables and Identifiers
• Constants
• Expressions and Operators
Introduction to Computer Programming
Data Types
• A data type is a collection of data values and a set of predefined
operations on those values.
• In Programming, Data types are divided into two groups:
• Primitive data types - includes byte, short, int, long, float, double,
Boolean and char
• Non-primitive data types - such as String, Arrays and Classes
Introduction to Computer Programming
Primitive Data Type
• Primitive data types are the building blocks of data manipulation. These are the most basic data
types available in programming languages. Primitive datatypes are predefined by the language
and named by a keyword.
• There are eight primitive data types in Java:
Introduction to Computer Programming
Primitive Data Type
• Integer
• The most common primitive numeric data type
• Floating point
• Floating point type is used for real number values. Most languages include two types of
floating point numbers
• Float
• It is the standard type which is usually stored in 4 bytes
• Double
• Used when larger fractional parts are needed
• Usually occupy twice as much storage as float types
Introduction to Computer Programming
Primitive Data Type
• Boolean
• The range of values for Boolean types has only two elements, One
for true and one for false
• Examples:
• True, false
• 0,1
Introduction to Computer Programming
Primitive Data Type
• Character types
• It is used for string characters (numbers, letters, symbols)
• Examples:
•A
•@
•9
Introduction to Computer Programming
Non-Primitive Data Type
• Non-primitive data types are called reference types because they refer to
objects. The main difference between primitive and non-primitive data types
are:
a) Primitive types are predefined (already defined) in language. Non-primitive types are
created by the programmer.
b) Non-primitive types can be used to call methods to perform certain operations, while
primitive types cannot.
c) A primitive type has always a value, while non-primitive types can be null.
d) A primitive type starts with a lowercase letter, while non-primitive types starts with an
uppercase letter.
e) The size of a primitive type depends on the data type, while non-primitive types have all
the same size.
Introduction to Computer Programming
Non-Primitive Data Type
• A string type is the one in which values consist of sequences of characters
• Examples of strings are: “Dar Es Salaam”, “Juma”, “Book”, etc
Introduction to Computer Programming
VARIABLES & CONSTANTS
Introduction to Variables
Introduction to Computer Programming
Variables
• Variables are containers for storing data values.
• A variable gives us named capacity that our code can control. Every variable in programming has
a particular sort, which decides the size and format of the variable’s memory; the scope of values
that can be put away inside that memory; and the set of operations that can be connected to the
variable. You must make an explicit declaration of all variables before they can be utilized.
• Variables can be declared in the following manner:
Data type <variable name>;
Example
Int age;
String address;
• Variables can be initialized in the following manner:
Data type <variable name> = value;
Examples
String name = "John";
int myNum = 15;
Introduction to Computer Programming
Identifiers
• All variables must be identified with unique names. These unique names are
called identifiers. Identifiers can be short names (like x and y) or more descriptive
names (age, sum, totalVolume).
• It is recommended to use descriptive names in order to create understandable
and maintainable code:
• The general rules for naming variables are:
• Names can contain letters, digits, underscores, and dollar signs
• Names must begin with a letter
• Names should start with a lowercase letter and it cannot contain whitespace
• Names can also begin with $ and _ (but we will not use it in this tutorial)
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be used as names
Introduction to Computer Programming
Constants
• A constant is a variable whose value cannot change once it has been assigned.
• A constant can make our program more easily read and understood by others and
also using a constant can improve performance.
• There is a slight difference in naming constant in various Programming Languages
but their functions are similar. Once created, their values cannot be changed at any
point in the program.
Reserved words
• These are special words in programming language that can not be used in naming
variables, constants, functions, Objects and Classes.
Introduction to Computer Programming
EXPRESSIONS
Expressions in Programming
Introduction to Computer Programming
Expressions
• An expression is a combination of explicit values, constants, variables,
operators, and functions that are interpreted according to the
particular rules of precedence and of association for a particular
programming language.
• There are mainly three types of expressions:
• Arithmetic expressions
• Relational expressions
• Boolean (Logical) Expressions
Introduction to Computer Programming
Expressions
• Arithmetic Expressions
• Expressions that perform arithmetic operations such as addition,
subtraction, multiplication and division.
• Consist of operators, operands, parenthesis and function calls.
• Arithmetic operators
• – +,-,*,/
Introduction to Computer Programming
Expressions
• Relational Expressions
• Expressions that are used for making comparisons. Consist of two
operands and one relational operator
• The value of a relational expression is Boolean.
• Relational operators
• Operators that compare the values of their two operands
• Equal (==)
• Not equal (!= or <>)
• Greater than (>)
• Less than (<)
• Greater than or equal (>=)
• Less than or equal (<=)
Introduction to Computer Programming
Expressions
• Boolean Expressions
• Consist of Boolean variables, Boolean constants, relational
expressions and Boolean operators
• Boolean (Logical) operators
• – AND (&&)
• – OR (||)
• – NOT (!)
Introduction to Computer Programming
Operators
• Operators are symbols that are used to perform operations on variables and
manipulate the values of the operands. Each operator performs specific
operations. Let us consider an expression 5 + 1 = 6; here, 5 and 1 are operands,
and the symbol + (plus) is called the operator.
• The operators are divided in the following categories.
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
Introduction to Computer Programming
Arithmetic Operators
Introduction to Computer Programming
Assignment Operators
Introduction to Computer Programming
Comparison Operators
Introduction to Computer Programming
Logical Operators
Introduction to Computer Programming
End of Lecture 2
• Next - Lecture 3 : Introduction to C Programming
Introduction to Computer Programming