Explore comparing variable values and making decisions based on the results.

Understand what comparison is, what the Java comparsion operators are and how to use them.


A simple program, like Hello World, just flows from start to finish, one statement after another until done. Solving real problems always involves evaluating data or user input and making decisions about what to do next, when to do it and what to do when things go wrong and much more. Handling these possibilities is called Flow Control.

Flow control is based on comparison. This means taking two variables and comparing them resulting in a boolean (true/false) value. We then use that boolean result to decide what to do next in a flow control statement.

Comparison is taking two values separated by a comparison operator and replacing them (internally) with the boolean result of the comparison. For instance, if  X = 5 and Y = 5, then the comparison  X == Y  results in true. Remember that = by itself is not comparison, it is assignment. Compare equal is ==.  Using X from above,  X == 7  results in false. The Not equal comparison operator is !=, so  X != 7  results in true.

Java supports an extensive list of comparison operators. Here is a detailed discussion of comparison operators. Note that comparison operators are divided into several types. Relational operators are used when you are comparing two non-boolean values. When comparing boolean values, the operators are called conditional. There are also bitwise operators but we will leave that for you to investigate in the resources noted earlier in this paragraph.

Relational operators are pretty easy to understand, the two values (also called operands) are compared and are either equal or not equal, with not equal split into greater or less than. Conditional operators are different. When we have two boolean values, we can AND (&& operator) them, meaning if both values are true, the result is true. If either value is false, the result is false. We can also OR (II operator) the values, meaning if either value is true, the result is true.

Watch these videos (video1, video2, video3, video4) for more information.

The ! operator prefixed to a boolean inverts the value of the boolean. Thus (from above) if  X == Y is true, !( X == Y) is false. If we have a boolean variable, B equal to true, then B by itself evaluates to true, but !B evaluates to false.

Comparison can be combined with parenthesis:  ( X == 5 ) && ( Y > 3 )  results in true, since both terms are true and then they are and'ed together in a third comparison. By the same token, ( X == 5 ) && ( Y != X ) results in false, since one term is true and one is false

Click Next for a quiz on comparsions.



Explore basic Java concepts such as program structure, naming rules, variables, data types and statements.
Explore the concept of Statements in a programming language and how they are used in Java.

Understand the concept of statements and in particular how statements are used in Java. Understand using variables and statements to perform basic math operations.


Statements are the work horse construct of any text based programming language. Statements are where action happens and where you encode your algorithm into instructions for your robot. Statements fall into two categories, actions with assignment of a resulting value to a variable and actions that do not assign a result:

Note there is always only one item, a variable, on the left side of the assignment operator. You cannot have operations on the left side of the = sign: think ‘assignment’ not equals.

Operators are used to manipulate variables or constants (called operands) in a statement. Operators have an order of evaluation called precdence (more), at the same parenthetic level. You can control the order of operations by enclosing them within parenthesis which can be nested. In the above example, in the first statment, 2 * 3 is done first (due to multiplication having a higher precedence than addition), resulting in 5 + 2 + 6. These are then added up and the result is 13. In the second statement, 2 + 2 is done first due to the parenthesis resulting in 4. Now you have 5 + 4 * 3. Multiplication is done before (precedes) addition so 4 is multiplied by 3 resulting in 12. Now you have 5 + 12. Finally the 5 and 12 are added to give the result 17. Watch this video up to comparison operators (discussed later). The various math operators available are discussed here and here (arithmetic and assignment sections).

Statements are also used to make decisions about what your program should do next (Flow Control) and to call Methods. More on both of these topics later. Statements can span more than one line and continue until terminated with a semicolon. Statements, except for variable declarations, only appear inside Methods. Variable declaration statements can appear inside or outside of Methods, but always inside of a Class. We will discuss all of this a bit later.

Click the next arrow for a quiz on variable assignment statements.



Explore the concept of Data Types and in particular the data types in Java.

Understand Data Types in general and the specific data types available in Java.


As discussed in the previous lesson, variables in Java are typed, that is, we specify what kind of data can be contained in the variable. Java has two categories of data type, primitive types and reference types. Java has eight primitive types. Primitive types are also called value types. Reference types allow you define your own data types (objects). The primitive data types are:

boolean stores only the idea of 'true' or 'false'
int stores a signed integer value 32 bits in size
short stores a signed integer value 16 bits in size
stores a signed integer value 64 bits in size
double stores a high precision decimal number
float stores a lower precision decimal number
byte stores a signed integer value 8 bits in size
char stores a single text symbol 16 bits in size

Primitive data types contain the simple numeric value of the appropriate type. Selecting a data type depends on the type of data you wish to store in a variable and also its numeric range or size. Primitive types default to a value of zero (false for boolean) if not initialized when created. Besides defining the type of data a variable can hold, the type also determines the memory space allocated for the variable. Typically, we only use int, double, float and boolean in robotics. Primitive types can be used in math operatons since the primitives are basicall all numbers.

As you can see, there is some repetition among the types of data different primitives can be used for. In general, int will work for most tasks we have working with whole numbers, and double will work well for any decimal operations. It is possible to do nearly all of your programming without using the different sizes of data. Recall that when creating a variable, Java uses the type to decide how big the memory storage location should be. As should make sense, if you try to take a piece of data stored in a long type and put it into a variable with type int, there will be an error. The same goes for any mismatch of the size limitations of the different primitives.

The Java compiler and JVM will watch for type mismatches, that is, trying to put an incompatible value into a variable, like trying to set an int to a double value like this:  int x = 3.2; The compiler will flag this as an error and at run time this would result in an exception (run time error).

Watch this video for more about variables and data types. Go here, here or here for detailed discussions of each data type.

Reference data types contain references or pointers to more complex data like objects. We will learn more about objects and reference data types a bit later. Reference variables default to a special Java value called null if they are not initialized. For reference variables, which are supposed to point to a more complex data object, null means the variable is not pointing to anything. Normally you cannot use a reference variable in a math expression as objects typically are not single numbers.

Strings of characters (text) are stored in a special object, acting like both a primitive and reference data type. A String contains a list of characters and can be treated somewhat like a primitive data type in that Java supports adding two strings together, which concatenates them into a new single string. Strings also support methods that can be called to perform operations on the string of characters contained in the string. Go here and here for detailed discussions of Strings. Note that a String variable may not be pointing to anything, meaning it is un-initialized or null. A String variable may also point to an actual string object, which happens to contain no characters, which is called an empty or zero length string. Null and empty do not mean the same thing.


Explore Variables in Java.

Understand what Variables are and why they exist. Understand how variables are defined and used in Java. Understand variable scoping.


A Variable is a memory location, or 'slot', reserved by your program into which you will store data. It is called a variable because we can change it as needed as the program executes. When we define variables we assign them meaningful names instead of having to work with actual memory addresses. Here are some examples:

Notice the Java keyword int. Java is a strongly typed language which means that when a variable is defined, we assign a specific data type to that variable. Different kinds of data will require different amounts of memory space to store. So, when we first create the slot in memory for the variable to use, we must tell the compiler what type of data we will be putting in that slot. This allows the compiler and the JVM to check that we only put the correct data into the variable and to some extent, that we use it correctly. In the example, the variables are all assigned the int (or integer) data type. (We will discuss data types in depth in the next lesson). When data of the wrong type is stored in a variable, you will encounter an error.

When we define a variable, we can initialize it (put a value in it right away) like we did in the case of myVar2, or we can separate the definition from the initialization, doing that later in the program in a statement as we did for myVar1. The first line of this code simply told the computer to create a slot in memory called myVar1 that will hold data of type “int”. The last line then says, “put the value 7 into the slot I labeled myVar1”. Before the last line, myVar1 is zero (the default), and when we put something into a variable for the first time, it is called ‘initialization’. Sometimes it is more convenient to initialize our variable as soon as we define it, like we did in the case of myVar2. That line says, “create a slot called myVar2 that can hold an int and put 3 in it”.

Notice that the fourth line of code does not include ‘int’ in front of myVar1. This is because myVar1 has already been defined in the code we have written. It is only necessary to define the type of a variable the first time we create it. Once it has been created, the compiler will have set aside the appropriate space to store an integer in that location in memory, so we do not have to restate that myVar1 is an integer. It is important not to restate the data type, because doing so will influence the scope of that variable depending on where it is stated (see below).

Variables are used in statements, which are the actual processing steps the program will execute. Variables change by appearing on the left side of the assignment operator  =. So = does not mean “is equal to”, but means “evaluate what is on the right of this symbol and put the result in the variable on the left”. We will discuss statements and operators in more detail a bit later.

Variables can be used below where they are defined but not above and must be used in a class or method. Variables can only be used inside the code block (class/method) in which they are defined. This is called the scope of the variable:

There is an exception to general variable scoping. Variables defined in a class at the class level can be accessed outside the class. We will discuss this further in the lesson on object variables.

Note the keyword final on the definition of myVar4. Final means the value of variable cannot be changed once it is initialized. Such a variable is used to store a constant value. Constants are values that do not change and are used in place of literally stating values in statements. This is very important when you reuse the same number many times in code, such as the diameter of a wheel. If you use a literal value and that value later changes, you will have to manually change each place the literal value is used. Here are two code snippets showing how a constant is used:

If for some reason, we later realize that the conversion was wrong for some reason, we would have to go back and find every 12 and change it. As code becomes more complex, it is easy to miss an instance of using that number. If you use a constant, changes to the definition of the constant will be carried through the rest of your code, making modifications much simpler and less error-prone. Here is the same code as above, written with a constant for the number of inches in a foot.

Watch this video for more about variables.

Watch this video for more about the final keyword and constants.
In the Java world, variables are commonly called "attributes", "members" or "fields".


Explore the basic syntax of Java.

Understand the syntax rules of Java, basic program structure, naming conventions, code blocks and comments.


As we move forward, we will start to acquire new vocabulary at an increasing rate. It is normal for vocabulary to take a while to stick, so don’t give up! Additionally, syntax is something you will learn by doing (and messing up!). Syntax is a challenge that you will always be getting better at, and is something nobody is perfect at.

Java programs consist of one or more classes (templates defining the characteristics and functions of an object). Classes consist of one or more class level variables (or constants) and one or more methods.
Variables are memory locations in which we can store data, like a number or string of characters. Variables can be changed as the program executes. Constants are variables that are set to an initial value and are not changed during execution.
Methods consist of one or more method local variables (or constants) and one or more statements.
Statements are code that change the state of the variables in a program and are how the program performs computations or make decisions about what to do next.
Here is what this looks like:

All of these things (other than statements) have names. Names in Java (also called identifiers) follow these rules:

  • Start with a letter, $ or _ (underscore). After the first character, you may use any combination of characters.
  • Java Keywords cannot be used as names.
  • Names are case sensitive. That means MyName is not the same as myname or myName.

While not requred by Java, most programmers follow this convention:

  • Class names start with an upper case letter. (MyClass rather than myClass)
  • Variable and method names start with a lower case letter. (myVariable rather than MyVariable)
  • When names are made up compound words, the first letter of the second word is upper case: MyClass, myVariable, myMethod.
  • Upper case all words in a constant and separate compound words with an underscore: MY_CONSTANT, WHEEL_DIAMETER, CAMERA_IP.

Keywords are reserved words used by Java. A list of the keywords can be found here. The meanings of the keywords will be discussed later. Note: when something is ‘reserved’ it means you cannot use it as a name of a variable or class. If you try to use a keyword in this way, the program will not compile.

Comments are used to document what your code is doing to aid both yourself and others in reading your code later on. Comments are important and should be used liberally. For example, there may be a complicated set of steps for a robot to lift an arm and open a gripper. In order to understand that is what is happening, someone would have to know what each step of your code does. A comment at the start of that set of steps that says “// lift arm and open gripper” makes things much easier for outsiders to utilize. Here is a discussion of the Java comment formats. Don't worry about JavaDoc (mentioned in the article), that is an advanced topic. 
It is important to note that even when legitimate code is written inside a comment, it will never run. The compiler always ignores the contents of any comment. This is a simple way to remove a portion of code from your program temporarily without having to copy and paste or re-type your code when finished.

Whitespace is also a form of commenting. Whitespace is blank lines, indents, extra spaces used to make your code easier to read. Again, this is both for yourself and others that may have to read your code. Use whitespace to separate operations, groups of statements and dense lines of code.

Note that braces { } are used to identify blocks of code that are treated as a group. All of the code for a class is enclosed in braces just as all code in a method is enclosed in braces. Just like a set of parentheses can have multiple layers like (x - (y-2)), so can braces. It is not uncommon for the end of a program to have many closing braces as each layer is closed off. Making sure each opening brace is paired with a closing brace is essential.

Finally, all statements end in a semicolon.



Explore the classic Hello World! first Java program example.

Understand the most basic Java program and its component parts.


The most common example of a first Java program is the famous Hello World! program. Before we get into the details of Hello World, I am going to introduce some of the supplemental resources we will be using.

TutorialsPoint is a programming instructional website and we will reference some of it's Java topics. In some topics there is a code window where you can execute examples of code discussed in the topic. They also have a Java coding website called CodingGround where you can write, compile and execute example code or your own code. We will use CodingGround to demonstrate topics in our lessons. In those cases, the code from our lesson will be loaded into the CodingGround code window and you can compile and execute that code right in your browser.

We will also link to videos on YouTube that give a different view of topics and show examples. 

We will refer to the official Oracle Java tutorial and documentation often and other Java resource sites when appropriate.

Now let's do some Java! Every programmer’s favorite program is one that will print “Hello World!” on the screen of the device you’re working on. That will be our first task.

In Java, everything is an object, also called a class, and all code must be contained in a class. A class is the description of an object. The terms object and class, while used interchangeably, are not quite the same thing. We will get into the details of objects and classes later. For now, just know that every Java program requires at least one object. This object's name is the same as name of your program, for example: HelloWorld. We define this object by creating the class HelloWorld and storing it in a file called HelloWorld.java. Our source code is always stored in a file with the .java extension and that file must have the same name as the class it contains.

The HelloWorld class must have a method (also called a function) called main. The main method allows the JVM to know where to start your program. We will discuss methods in more detail later. So the simplest Java program looks like this:

In this example the key thing to see is the statement that prints Hello World! on the system console. System.out is a built-in Java API and println( ) is a method in that API that prints text on the display.

Jump over (finish reading this paragraph first) to the Hello World example in CodingGround. You can run the example by clicking the Execute button. The Execute button runs the source code (HelloWorld.java) through the Java compiler javac (as you can see in the console window). Compiling produces the HelloWorld.class file. The .class file contains the bytecode the JVM will execute. The Execute button then runs the JVM and passes the name of the .class file to be executed and the output appears in the console window. Before you go, notice that the example main method is not quite correct. You can edit the code in the code window to correct the problem and create the result we want. After editing you will need to click compile to generate a new .class file with your changes.

A very important concept to keep in mind regardless of what code you are writing is that many steps in the programs that we write will not have visible results. In this example, the only thing that we end up actually seeing on the screen are the words we have put in the quotation marks inside the System.out.println( ) function. Simply looking at the output, we would not have been able to know exactly the steps that went into the original program. This is very important when trying to fix problems with a program that is not behaving as expected. When we get more complex programs, in order to even know which part of our program is executing we need to make sure to have some visual result (printing something to a screen, moving a motor or servo in a particular way) that will help us identify what step in the program is running. Don’t forget: steps in code can run without having any visible result, but that does not mean they did not happen.



Show a simple example of a Java program for Tetrix (FTC) robots. This is a preview of the type program the student will be learning about in subsequent lessons.

Look at the example and know that soon you will understand what this Java code does, where it came from and how to create programs yourself. For now, just a preview.


Here is a very simple Java program for Tetrix (FTC) robots. This is just to give you a preview of what we will be covering in the following lessons. We will show you what all of this means, where it comes from, how it works and examples of how this basic program is the basis of more complex programs that make robots move and respond to their environment. In the FTC environment, a robot program is called an OpMode.


Explore where a student's program fits in the hierarchy of programs that make up a robot's complete set of software.

Understand where in the hierarchy of software layers that make up a robots code the student program sits and the scope what it needs to control.


Software systems are typically made up of several layers of programs, each providing services to the layer above. Robots are no different. In a robot, there will be a layer below your program that provides services to the programs you will write. This layer below (an API) hides complexity or details that have already been programmed for you and you can ignore. There may also be a layer above your program that tells your program when to stop and start and may provide input. In that sense your program is providing a service to that higher layer. The layer at which your program sits and the things it needs to know about and control are its scope.

A way to think about this is to consider the example of the human body. If we think of the body, the body has a brain, nerves, neurons and limbs (arms, fingers). Comparing to a robot, the robots processor is the brain, the nerves are wires, the neurons are motor controllers and the limbs are motors. The brain does not need to know how the neurons work or how the limbs work, it just tells the neuron to move the hand and the neuron takes care of the details of hand movement. In a robot, we don’t worry about how the motor controller chooses the right voltage to make the motor turn, we can just tell the controller to move the motor.

During your learning how to program robots, there will be plenty things going on at lower and higher levels, which we will not discuss in detail but you typically don't need to know how these other levels work. Your program just asks the lower level to perform a function and may respond to a request from a higher level.