CPRO1
INTRODUCTION TO COMPUTER
PROGRAMMING
JAVA VARIABLES
Ms. Image Marie Y. Maiquez
September 2024
Week 7
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
JAVA VARIABLES
Variables are special things for storing data. Any data. All data in Java is
stored using variables. One of the best ways to conceive of a variable is as
a box: a completely ordinary box.
In this way, a name can only be given to a memory location. It can be assigned
values in two ways:
• Variable Initialization
• Assigning value by taking input
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
How to Initialize a variable in Java?
It can be perceived with the help of 3 components that are as follows:
datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable.
type variable (assign)_operator value;
int num1 = 5;
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
JAVA VARIABLES
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
Rules in naming variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three
different variables)
• A variable name cannot be any of the java reserved words.
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
JAVA VARIABLES
Every variable in Java has three important properties: type, name, and
value.
The name is used to distinguish one variable from another. It's like a
label on a box.
The type of a variable determines the type of values/data that can be
stored in it. We store a cake in a cake box, shoes in a shoe box, etc.
The value is some object or the data stored in the variable.
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
To assign a value to a variable, we have the assignment operator. It copies a
value from one variable to another. It does not move the value. It copies. Like a
file on disk. Assignment looks like this:
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
Initialized Variables with values
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
What is the difference between initialization and assignment?
Initialization gives a variable an initial value at the point when it is created.
Assignment gives a variable a value at some point after the variable is created.
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
String name = “Joshua";
System.out.println(name);
int myNum = 15;
System.out.println(myNum);
int myNum;
myNum = 15;
System.out.println(myNum);
CPRO1: INTRODUCTION TO COMPUTER
PROGRAMMING
DISPLAYING VARIABLE DATA
Note that if you assign a new value to an existing variable, it will
overwrite the previous value:
int myNum = 5;
myNum = 15; // myNum is now 15
System.out.println(myNum);