Java Syntax Basics

Note About This Guide

If you are new to Java programming, it is expected that you may not understand everything at first. You will still be able to read and understand the next guide on FTC Java Basics, and you can use this guide as a reference whenever you need.

Java Syntax Basics

Java is a line based language, meaning that the computer will interpret your program as typed lines of code. As a result, the way in which you write your code must follow a strict structure, known as the syntax. Here is the most common syntax you will use in Java.

Comments

Sometimes you will want to leave a note in your code (a reminder, an explanation, etc) for yourself. This can be done through comments. Any line that begins with the characters // will be treated as a comment, meaning that your program will simply ignore it.

// This is an example comment. Use comments to describe what your code does or to remind yourself of something.

You can also make multi-line comments by beginning with /* and ending with */.

/*
This is part of the comment
and this is also part of the comment
*/

Classes

In Java, a class is a program that contains variables (values that the program stores) and methods (functions that define the behavior of the program). A simple class looks like this:

public class MyClass extends ParentClass {
    // Code for this class' variables and methods will go here
}

The syntax of a basic class is access-level class ClassName extends ParentClassName {}.

The access-level simply defines where this class can be used. For classes, we usually use public, which means that this class can be used by anything. If the concept of access levels is confusing, that’s okay; for FTC programming, they are not too important to understand.

The extends ParentClassName part of the syntax is optional. If included, this will cause the class to inherit the variables and methods of another “parent” class.

The brackets ({}) is the class’s block, which will contain all of the code that we will write in the class.

Data Types

Before discussing methods and variables, it is important to discuss data types. All variables and values returned by methods have a type that defines what type of data it is. These are a few of the most basic (primitive) data types in Java:

TypeDescriptionExample Values
intA whole number (integer)4, -7
doubleA decimal number1, 7.2, -0.3
booleanA “on or off”, “yes or no”, “true or false” value.true, false (these are the only possible values)
stringA “string” of characters (basically any text)."Hello world!", "left_drive"
(string values must always be surrounded by quotes to differentiate them from regular code)

Notice that all of the primitive data types start with a lower case letter (like int). There are also object data types, which are based off of classes and start with an upper case letter. For instance, in FTC, there is a DcMotor object data type based on the DcMotor class; as the name implies, variables with this data type contains the variables and methods for a motor on the robot.

Methods

Methods define the various functions of a class. Within the class’s block (brackets), we can define a method like so:

public class MyClass extends ParentClass {
    public void myMethod() {
        // Method code will go here
    }
}

The syntax of a basic method is access-level return-type methodName() {}.

The access-level works the same as with classes: it defines what can use the method. Here we just use public.

The return-type is the data type (int, DcMotor, etc) of the value that this method returns as output when ran. If the method does not return any value, the return type is simply void.

The () contains any method parameters (inputs). We will talk about those more later in the guide.

Statements

We have discussed the syntax structures in Java that will contain our code (classes and methods), but how do we actually write code to put into those structures? In Java, statements are lines of code that can perform various actions when ran, such as defining variables, assigning (setting) variables, and invoking (running) class methods.

Statements in Java must always end with a semicolon (;) character; otherwise, the computer will not know where the statement ends. Here are some common types of statements:

Variable Definition

We can use statements to define (create) new variables to store values within our code. Variables can be defined either in the class (called “instance variables“) or in a method (called “local variables“). Here are examples of both in use:

public class MyClass extends ParentClass {
    // Instance variable definition
    private DcMotor myMotor;

    public void myMethod() {
        // Local variable definition
        double myDouble = 1;
    }
}

The instance variable statement is inside of the class, but not the method. The syntax is access-level type variableName;.

We usually make our class variables private (however, if you make the variables public or don’t even include the access level, it will still work as intended for FTC programming purposes).

The local variable syntax is similar to the instance variable syntax, except there is no access-level included since local variables can only be used within the method that they are defined in.

You can also set the initial value of a variable by including = value at the end of the definition (but before the semicolon, of course).

Variable Assignment

We can write statements within our methods to set the values of defined variables. For instance:

public class MyClass extends ParentClass {
    int exampleInt;

    public void myMethod() {
        // Variable assignment
        exampleInt = 7;
    }
}

Since the variable is already defined, you do not include the variable’s data type in its assignment statement.

Method Invocation

Statements can also be used to invoke (run) a method. You can either invoke another method in your class or invoke a method contained in an object variable. To invoke a method in an object variable, you have to use a . operator to access it. For instance, to invoke a method named myMethod contained in a variable named myObject, you would write:

myObject.myMethod();

If a method requires parameters (inputs), you must list your input values (or variables and expressions) inside of the method’s parenthesis, separated by commas. For instance:

// Let's say myMethod is defined like this in our class:
public void myMethod(String caption, int value) {
    // Code using the caption and value input variables
}
// The method takes two inputs: a String and an int.
// Thus, when invoking the method, we have to pass those inputs like so:
myMethod("Example Name", 7);

Expressions

In our examples for the different types of statements, we simply used raw values (like 7) for our variable definitions/assignments and method invocations. However, you can also use more complicated expressions in your statements, comprised of values, variables, math, conditionals, and other expressions.

Arithmetic Expressions

An arithmetic expression use mathematics to return a calculated value. Here is an example of defining a variable named z using an arithmetic expression.

int x = 3;
int y = 2;
int z = (x + y) * 4;

As expected, the value of z will end up being \( 20 \), the result of \( (3 + 2) \times 4 \).

Boolean Expressions

A boolean expressions returns a boolean value (true or false). Boolean expressions can contain comparison operators that compare two values. Here is an example of a comparison:

int x = 3;
int y = 2;
boolean greater = x > y;

The > comparison determines if one value is greater than another, and returns true if it is greater or false otherwise. In the above example, the value of greater will be true since the value of x (3) is greater than y (2).

Here is list of all comparisons:

OperatorNameExamples that Return true
==Equal2 == 2, "word" == "word"
!=Not equal0 != 1, "word" != "other_word"
>Greater than3 > 2
<Less than2 < 3
>=Greater than or equal to2 >= 2, 3 >= 2
<=Less than or equal to2 <= 2, 2 <= 3

Boolean expressions can also contain logical operators which compare two boolean values. A common operator is and (&&), which returns true if both values are true and false otherwise. For instance:

int x = 3;
int y = 2;
boolean andResult = (x > y) && (x == 2);

The boolean expressions on the left and the right of the && operator are both true, so the value of andResult will be set to true. Here is an example where andResult is false.

int x = 7;
int y = 2;
boolean andResult = (x > y) && (x == 2);

In this case the first expression (x > y) will still be true, but the second expression (x == 2) will now be false. Thus, andResult will be false.

Further Learning

If you want to learn more about the Java programming language (not specific to FTC programming, only Java in general) beyond the information in this concise guide, Codecademy’s free Java course is a good place to start; lessons 1, 2, 3, 4, and 6 of the course are most relevant to the Java skills used in FTC programming. Note that in depth Java knowledge is not required in order to understand the next guides, but extra knowledge can definitely be beneficial.