Chapter 6:
Variables
Data Types
Programs use 2 general types of data:
• Constants
- Values remain constant and do not change.
- Literals are unnamed constants.
• Variables
- Can hold different values. The values can be changed anywhere in the program
Variable Definition
• Variables are named memory locations whose contents may vary or differ over time. The
name serves as an address to the location and gives access to the value that the variable
holds.
• At any given time, a variable holds one value
• Unlike literals, variables have to be declared
Variable Declaration
• A variable declaration is a statement that contains the variable’s data type and identifier
• The data type describes the following:
– What values can be held by the variable
– How the variable is stored in memory
– What operations can be performed on the variable
• To declare a variable:
< data type > < identifier or variable name > [= initial value];
• The variable name should comply with naming conventions for identifiers
• Examples of variable
declaration: String name;
int age;
• Example of variable declaration and initialization:
String name = “Ana”;
int age = 18;
Types of Variable
• Variables can also be classified according to its contents and/or how they will be stored
in memory. In general, programs use the following types of variables:
Numeric variables
Holds numbers or digits and can be used for mathematical computations.
Textual data
Holds text, such as letters of the alphabet, and other characters
Boolean data
Holds a value of true or false
Primitive Data Types
• The Java programming language defines eight primitive data types for each of the variable
types:
– For logical: boolean
– For textual: char
– For numerical:
Integers: byte, short, int,
long Floating point: float, double
Boolean Variables
• The boolean literals true or false can be assigned as values to any boolean variable.
• A boolean variable can hold any of the boolean literals: true or false. To declare
variables of type boolean:
boolean < identifier or variable-name >
• Example:
boolean result = true;
• The example declares a variable named result that is of boolean data type and assigns it
the boolean literal true
Textual Variables and Literals
char
– is a single Unicode character. A Unicode character is represented by 16-bits and includes
symbols and other special characters
– character literals are enclosed in single quotes (‘ ‘).
– Example of char literals: ‘A’ ‘1’ ‘b’ ‘\’’
– If the single quote is included in the string, add the escape character \ before the single
quote
– To declare variables of type char:
char < identifier or variable-name >
Example:
char gender = ‘F’;
Numeric Variables and Literals
Integers
• Are whole numbers with a sign (positive or negative)
• Example of integer literals:
23
100
123456789
50L
Note: no commas are used when coding integer literals; 50L is an integer
value whose data-type is long
• Integers from different number systems (base 8, base 10 and base 16) can also be
represented.
• Example: The number 12 is represented as follows:
In decimal (base 10): 12
In hexadecimal (base 16): 0xC
In octal (base 8): 014
• The following are the different integer data types and their range of values:
Integer Size Integer Data Type Range
8 bits byte -27 to 27 – 1
16 bits short -215 to 215 – 1
32 bits int -231 to 231 – 1
64 bits long -263 to 263 – 1
• To declare integer variables:
byte < identifier or variable-name > short < identifier or variable-name > int < identifier
or variable-name > long < identifier or variable-name >
Example:
int noOfDaysInAMonth
Floating Point
Are fractional numbers with a decimal point. They may be positive or negative.
Example: 3.1416, -20.5, 3.14F, 6.02e23
There are 2 types of floating point literals or variables: float and double
float literals are followed by an “f” or “F”, while double literals have a “d” or “D” at the
end. If neither is present, the default data type is double.
Floating point literals can also be expressed in standard format or in scientific notation.
Example:
Standard = 583.45
Scientific = 5.8345e2
Floating point data types have the following ranges:
Float Size Float Data Type Range
32 bits float -231 to 231 – 1
64 bits double -263 to 263 – 1
To declare floating point variables:
float < identifier or variable-name > double < identifier or variable-name >
Example:
float area;
double salary;
The String Class
Strings
A cluster of characters.
String literals are enclosed in double quotes (“ “).
Example of String literals: “blue”
“ABC 123”
“40-D C.P. Garcia Ave., Quezon City”
If the double quote is included in the string, add the escape character \ before the double
quote
To declare variables of type String:
String < identifier or variable-name >
Example:
String message
String is not a primitive data type. String is defined as a class in the java.lang package.
String literals and variables are objects or instances of the String class.
Assigning Values to Variables
The assignment operator “=“is used to assign values to variables.
The following can be assigned or “moved to” a variable:
- Literals
- Another variable
Both sides of the assignment variable should be of the same data type
The declaration
String name = “Ana”;
can be broken down into 2 statements:
String name; name = “Ana”; // declaration statement
//assignment statement
Notes:
1. It is always good to initialize variables as you declare them.
2. Use descriptive names for your variables.
Example: a variable that contains a student’s grade may be named studentGrade
and not just random letter and numbers.
3. In object-oriented programming, variable names begin with a lowercase letter. The
starting letter of succeeding words are capitalized.