Variables and basic types
1.2.1 — Variables and basic types
A variable stores a value under a name. This lesson explains how to create variables, use them in calculations, and change their values.
System.out.println(...) prints a value. When + has a String operand, it joins text and values. For example, "Count: " + 6 produces "Count: 6".
Run the code cells in order: some use variables declared earlier in this notebook.
Declare and initialize a variable
int count = 6;intis the type: this variable holds whole numbers.countis the name.6is the initial value.
The line declares the variable and initializes it. = stores the value on the right in the variable on the left. The semicolon ends the statement.
Use the name to read the stored value, as in the next cell.
Variable names
Names are case-sensitive: count and Count are different. Names cannot contain spaces or start with a digit.
Use descriptive names. Java code commonly uses camelCase, such as firstNumber: start lowercase, then capitalize each new word.
Include units where useful, such as distanceKm or amountCents.
Calculate using variables
Run the next two cells in order. The first declares two numbers; the second reads them and stores their sum in total.
The calculation uses the stored values, just as 18 + 12 uses the numbers directly.
/ divides numbers. Here, total / count divides 30 by 6, giving 5.
Reassign a variable
After declaring count, you can replace its value without writing the type again:
count = 8;This is assignment. It stores 8; it does not compare the two values. Initialization supplies the first value, and reassignment replaces it.
A stored result does not update itself
int total = firstNumber + secondNumber; calculates the sum once and stores it. Changing firstNumber later does not change total.
Run the next two cells. The first changes an input and prints the old total. The second calculates and stores a new total.
Trace one statement at a time
To trace a program, write down the value after each step. You do not need to hold the whole program in your head.
int places = 10;
places = places - 2;
places = places + 1;| Step | Expression evaluated on the right | places afterward |
|---|---|---|
| Declare | 10 | 10 |
| First assignment | 10 - 2 | 8 |
| Second assignment | 8 + 1 | 9 |
Java reads the old value before replacing it. places = places - 2 is useful assignment even though it would be a strange equation in mathematics.
Four basic types
A primitive type is a built-in Java value type.
| Type | Values | Example |
|---|---|---|
| int | Whole numbers from -2,147,483,648 to 2,147,483,647 | 6 |
| long | Whole numbers with a much larger range | 3000000000L |
| double | Approximate numbers that can have fractional parts | 7.5 |
| boolean | true or false | true |
A value such as 6 written directly in code is called a literal.
L marks a whole-number literal as long. Use uppercase L so it is easy to distinguish from 1. Three billion exceeds the int range.
A decimal literal such as 7.5 has type double. Use a decimal point in Java code. Many decimal fractions are stored approximately.
true is a boolean value. "true" is text, with type String. Quotes change the type of the value.
Assign values of a compatible type
An int variable cannot hold a String:
int number = 3;
number = "three"; // compilation errorTry these lines in a new code cell, then fix the assignment to number = 4;.
Some numeric conversions are allowed. For example, double value = 3; converts the integer 3 to 3.0.
Variables across notebook cells
This notebook keeps variables after a cell runs. The next cell reads count and total from this lesson.
If a name cannot be found, run its declaration cell first. After restarting the Java runtime, you need to run the declarations again.
Practice: change a variable
The cell below creates practiceCount with the value 10. Replace the TODO comment with an assignment that subtracts 2 from practiceCount.
Run the cell. It should print 8.
Hint
Use practiceCount = practiceCount - 2; where the TODO comment is. Keep the print statement below it.
Worked solution
int practiceCount = 10;
practiceCount = practiceCount - 2;
System.out.println(practiceCount);The assignment replaces 10 with 8. The print statement reads that updated value.
Check your understanding
Change the starting value from 10 to 12 and run the cell again. It should now print 10.
Explain which line declares the variable and which line changes it.
Enable JavaScript to run the Java notebook.