How to modify and manipulate variables in Java and C++
The vast majority of applications, regardless of platform, store
data. Variables are one such method of storing data, and they
are helpful because an application can constantly manipulate and
reference them. One common example is a score in a video game. The
score is stored in a variable, and whenever you defeat an enemy or
pass a level, the variable’s value increases.
In Java and C++, a variable is created with three components:
Data type - data type tells your computer how to store the data.
The important fundamental data types are mentioned at the end of
this article, but for now, we will only be working with the
integer data type, denoted with the word
int
.
Name - you use the name to reference the variable later on
Value - this is the data that is being stored
Note: Specifying data type is only necessary the first time a
variable is created.
Let’s first store a variable of data type integer, named
number
, and value
5
. Then, let’s use the print function we learned in the “Hello
World!” article to check the variable’s value. Here is what it would
look like:
Code:
number = 5;
print(number);
Output:
> 5
Code:
int number = 5;
System.out.println(number);
Output:
> 5
Code:
int number = 5;
cout << number;
Output:
> 5
Notice that unlike in the “Hello World!” article, we don’t have
quotation marks around
number
in the print statement. This is because quotation marks indicate the
computer should print the words between them. We don’t want to print
the actual word “number,” we want the computer to print the value of
the variable named
number
. Here’s what would happen if we surrounded number with quotation
marks:
Code:
number = 5;
print("number");
Output:
> number
Code:
int number = 5;
System.out.println("number");
Output:
> number
Code:
int number = 5;
cout << "number";
Output:
> number
Math using variables
Now that we know how to create variables, we can manipulate and
modify them. For example, here is how we manipulate them using the
four basic arithmetic operations:
Code:
a = 5
b = 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output:
> 10
> 0
> 25
> 1.0
Code:
int a = 5;
int b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
Output:
> 10
> 0
> 25
> 1
Code:
int a = 5;
int b = 5;
cout << a + b << "\n"; // the "\n" just makes a new line in the output
cout << a - b << "\n";
cout << a * b << "\n";
cout << a / b << "\n";
Output:
> 10
> 0
> 25
> 1
Common Mistake: Since the integer data type only stores integers,
when numbers that aren’t integers are stored in a variable of data
type integer, they are automatically rounded down (e.g., 99/100 → 0,
5.43 → 5, 4.65 → 4).
We can also modify the value of an already-created variable with a
line of code in the form “name of variable = new value of variable.”
If we wanted to change
number
from the previous example to have the value
10
, this is what it would look like:
Code:
number = 5
number = 10
print(number)
Output:
> 10
Code:
int number = 5;
number = 10;
System.out.println(number);
Output:
> 10
Code:
int number = 5;
number = 10;
cout << number;
Output:
> 10
You can also use arithmetic when modifying a variable …
Code:
number = 5
number = 10 + 5
print(number)
Output:
> 15
Code:
int number = 5;
number = 10 + 5;
System.out.println(number);
Output:
> 15
Code:
int number = 5;
number = 10 + 5;
cout << number;
Output:
> 15
… and even use the variable itself when modifying it.
Code:
number = 5
number = number + 2
print(number)
Output:
> 7
Code:
int number = 5;
number = number + 2;
System.out.println(number);
Output:
> 7
Code:
int number = 5;
number = number + 2;
cout << number;
Output:
> 7
In programming, the equals sign does not have the same purpose as in
math. Instead, equal signs are used to set the variable on the LEFT
equal to the value on the RIGHT. The value on the right is first
calculated, then the variable on the left is assigned that value.
If you plan to use the variable itself when modifying it, you can
use the shortcut below. This code does the same thing as the
previous example but takes less space. You can also use this
shortcut with any arithmetic operation by replacing
+
in the expression with the desired operation (
-
,
*
,
/
).
Code:
number = 5
number += 2
print(number)
Output:
> 7
Code:
int number = 5;
number += 2;
System.out.println(number);
Output:
> 7
Code:
int number = 5;
number += 2;
cout << number;
Output:
> 7
Variables are an extremely powerful tool that every programmer
utilizes daily.
This article only went over variables with data type integer, but
there are many other data types in Java and C++ that store different
kinds of data, such as:
string - stores text
float - stores numbers with decimals
long - stores really big numbers
char - stores single characters
boolean - stores either a 1 or a 0 (denoting True and False);
this is covered in a later article.
You can play with all the code we've used in this article on Replit: