3: Data Types & Flow
We will learn to solve simple problems with Java programming. We will create all sorts of variables, test their limitations and exercise basic control flow.

Learning Targets

    I can declare variables of common Java data types and assign values.
    I can control the flow of my app with loops, conditionals and method calls.
    I can write efficient conditionals and return comparisons in my methods.
    I can describe the difference between logic, run time and compile time errors.

Declaring Variables

Primitives are objects that live entirely in stack memory (they're lowercase like int and not like String) . We'll go over the difference between stack and heap memory later. For now, just think of stack memory as the lightweight stuff that's really fast-access. Take a look at the types of Java primitives.

Naming Conventions

CompoundCapitalize name of classes
camelCase variables and methods
ALLCAPS variables marked as final, meaning they can't change
snake_case is typically used only in Python and I miss it every time I work in Java or C#

Operators

Arithmetic

Operator
Description
Example (a=10, b=20)
+ (Addition)
Adds values on either side of the operator.
a + b will give 30
- (Subtraction)
Subtracts right-hand operand from left-hand operand.
a - b will give -10
* (Multiplication)
Multiplies values on either side of the operator.
a * b will give 200
/ (Division)
Divides left-hand operand by right-hand operand.
b / a will give 2
% (Modulus)
Divides left-hand operand by right-hand operand and returns the remainder.
b % a will give 0
++ (Increment)
Increases the value of operand by 1.
b++ gives 21
-- (Decrement)
Decreases the value of operand by 1.
b-- gives 19

Comparison

Operator
Description
Example (a=10, b=15)
Result
==
Equality operator
a==b
false
!=
Not Equal to operator
a!=b
true
>
Greater than
a>b
false
<
Less than
a<b
true
>=
Greater than or equal to
a>=b
false
<=
Less than or equal to
a<=b
true

Conditions

Don't be like this. Go straight to the condition.

Loops

Loops are how we repeat commands or loop through items in a collection.

for loops

standard for loop

Most for loops look something like this:
1
for(int x = 0; x < 20; x++) {
2
// code goes in here
3
}
Copied!
The for statement has a declaration, condition and increment. But you can skip the declaration if you'd like. Let's say you want the counter to have a wider scope (so it can be accessed outside of the loop) like this:
1
int x = 0; // declared outside the loop so it can remain afterwards
2
for(; x < 20; x++){
3
// code goes here for the loop
4
}
5
System.out.println(x); // will print 20
Copied!
We start at zero and stop without including our final number

foreach loop

Let's say I have an array or collection of numbers like this: int[] numberArray = [5, 10, 15, 20, 25];
Each number in that group has an address or index (that starts at 0). If I wanted to print the number 10 for example, I could write System.out.println(numberArray[1]);
So if I wanted to loop through (called iterating or traversing) all the items in this array, I could use a normal for loop like this:
1
for (int x = 0; x < numberArray.length; x++){
2
System.out.println(numberArray[x]);
3
}
Copied!
But there's an easier form of the for loop that's meant to easily traverse through a collection. We'll go over some of the pros and cons of this method later.
1
for (int x : numberArray){
2
// x isn't looping through the addresses this time
3
// rather, it's pretending to be each number in the collection
4
System.out.println(x);
5
}
Copied!

while loops

Infinite loop

1
while(true){
2
// will never stop looping unless I use the break command
3
}
Copied!

do-while loop

The do-while is rarely used but it's handy if you want to make sure the steps run the first time before the loop condition is tested. Read more.
1
do {
2
// code will run at least one
3
} while(x > 10);
Copied!

Method Calls

This is a part of control flow. The thread, the computer's train of thought, is being sent over to work through a set of commands called a method or function (either name works in this class).
The main function is calling max(), passing it a few numbers and saving the result
The variable declared in the header is scoped to that function
Last modified 1mo ago