0
LEARNING GUIDE
Week No.: __2__
TOPIC/S
Programming Basics : Variables/Constants, Data Types, and Expressions
EXPECTED COMPETENCIES
Upon completing this Learning Module, you will be able to:
1. Understand Java Variables/Constants , Data Types and Expressions
2. Apply Variables, Data Types and Expressions in a Java program
CONTENT/TECHNICAL INFORMATION
Programming Basics
A. Variables
Generally a variable is a place in memory where information or data is stored. This
information can be changed anytime as the program runs or at runtime.
In Java, a variable is required to be declared first before it can be used.
A variable can be identified through its given name called identifier.
A variable is assigned with a Data Type.
Rules in Naming Java Variables
1. Cannot contain white spaces
2. Can begin with special characters such as $ and _
3. Variable name should begin with a lower case letter as per Java coding
standard. For lengthy variables names, start the second word with capital
letter. Example int smallNumber , int myAddress.
4. In Java variable names are case sensitive. SUM<> sum
Valid Variable Name Invalid Variable Name
total %sum
_myName big number
$average *product
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
1
How to Declare a variable in Java
Syntax:
1. data_type variable; // without value assignment
Example: int num;
2. data_type variable_name = value; // with value assignment
Example: int num1 = 100;
Types of Variables in terms of Variable Scope
There are three types of variables in Java
1. Local variable
2. Instance variable
3. Static or (Class) variable
1) Local Variable
A variable declared inside the body of the method is called local variable. Local
variable is only valid within that method and the other methods in the class cannot use
it.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called
instance variable.
It is called instance variable because its value is specific and is not shared among
instances.
3) Static variable instance
A variable which is declared as static using the static keywords is called static
variable. It cannot be local. You can create a single copy of static variable and share
among all the instances of the class. Memory allocation for static variable happens
only once when the class is loaded in the memory.
Example to understand the types of variables in java
class A{
int num=50; //instance variable
static int x=100; //static variable
void method(){
int n=70; //local variable
}
} //end of class
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
2
Java Variable Example: Add Two Numbers
class adn{
public static void main(String[] args){
int x=50;
int y=10;
int z=x+y;
System.out.println(c);
}}
Output: 60
B. Constants
A constant is a place in memory(variable) whose value cannot change once it has
been assigned. Java doesn't have built-in support for constants.
A constant can make java program more easily read and understood by others. Also a
constant is cached by the JVM as well as our application, thus using a constant can
improve performance.
To define a variable as a constant, just add the keyword “final” in front of the variable
declaration.
Syntax: final data_type variable_name = value;
Example: final float pi = 3.1416f;
The above statement declares the float variable “pi” as a constant with a value of
3.1416f. The value of pi cannot change at any point in time in the program. Changing
the value of pi like “pi=5.25f”, Java will display errors at compile time.
C. Data Types
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable.
1. Primitive data types: Include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: Include Classes, Interfaces, and Arrays.
Java Primitive Data Types
Java primitive data types are the building blocks of data manipulation. Considered as
the most basic data types available in Java Language.
Note: Java is a statically-typed program language. It means, all variables must be
declared before it can be used.
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
3
8 types of primitive data types:
1. boolean 5. int
2. byte 6. long
3. char 7. float
4. short 8. Double
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
Default Value and Size of Primitive Data Types
Data Type Default Value Default Size
Boolean false I bit
Char ‘\u0000’ 2 byte
Byte 0 1 byte
Short 0 2 byte
Int 0 4 byte
Long 0L 8 byte
float 0.0f 4 byte
Double 0.0d 8 byte
Boolean Data Type
The Boolean data type is used to store only two values: true and false. This data type
is used to test conditions in a program.
Example: boolean try = true
Byte Data Type
The byte data type is an 8-bit signed two's complement integer. Its value-range lies
between -128 to 127. Its default value is 0.
Example: byte a = 10, byte b = -20
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
4
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range lies
between -32,768 to 32,767. Its default value is 0.
Example: short sum = 10000, short total = -5000
Int Data Type
The int data type is a 32-bit signed two's complement integer. Its value-range lies
between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1). Its default value is 0.
Example: int x = 50000, int y = -100000
Long Data Type
The long data type is a 64-bit two's complement integer. Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). Its default
value is 0.
Example: long x = 200000L, long y = -200000L
Float Data Type
The float data type is a single-precision 32-bit floating point. Its value range is
unlimited. Its default value is 0.0F.
Example: float ans = 5340.1f
Double Data Type
The double data type is a double-precision 64-bit floating point. Its value range is
unlimited. Its default value is 0.0d.
Example: double = 32.5d
Char Data Type
The char data type is a single 16-bit Unicode character. Its value-range lies between '\
u0000' (or 0) to '\uffff'. The char data type is used to store characters.
Example: char firstLetter = 'A'
D. Expressions
Expression in programming is a combination of one or more operators, constants,
keywords, functions and variables that the programming language interprets to
produce a value.
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
5
Example of expressions:
1. (5 + 4 – 2)* 12
2. a +b * c
3. (x < y)
4. sin(x+y) / z
5. if (x > y)
constants --- 5 , 4 , 2 , 12
variables or operands --- a , b , c , x , y , z
operators --- +, - , * , / , > , <
keywords --- if
functions --- sin()
Keywords
In Java, a keyword also known as reserved word is one that has a predefined meaning
in Java programming language syntax. Keywords are words that only Java language
can understand.
Note: Java keywords may not be used as identifiers for naming variables, classes,
methods or other entities.
List of Java Keywords
A list of Java keywords or reserved words:
JAVA KEYWORDS/ RESERVED WORDS
abstract continue finally interface public this
boolea default float long return throw
n
break do for native short throws
byte double if new static transient
case else implements null strictfp try
catch enum import package super void
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
6
car extends instanceof private switch volatile
class final int protected synchronized while
Operators
Operators are symbols or character that has a special meaning in Java.
Types of Java Basic Operators
1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Ternary Operator (miscellaneous operator)
Definition of Terms Compilation
Variable - a place in memory where information or data is stored and its value can be
changed as the program runs.
Runtime – time when Java program is running or being executed.
Identifier – any name given to a variable, constants, user defined functions … etc.
Variable Scope – it determines what part of the program or project a variable is valid
or visible.
Data type - specifies what kind of data including the different sizes and values that
can be stored in the variable.
Expression - is a combination of one or more operators, constants, functions and
variables that the programming language interprets to produce a value.
Keyword - is a word that has a predefined meaning in Java programming language
syntax or it is a word that only Java language can understand.
Syntax – is a rule in making or writing java language statement or commands.
Operators - are symbols that has a special meaning or a character that represents an
action.
Activity:
1. Hands On Activity . Writing Simple programs.
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
7
Problem:
1. Write a program that computes the sum of two numbers (variables)
2. Write a program that computes the average of 5 numbers (variables)
This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.