Quick Reference Guide
This is a quick reference guide for Java programming. It provides an overview of the key concepts throughout this section. It is not meant to be a comprehensive guide, but rather a quick reference for the most common concepts and syntax used in Java programming. WPILib recommends taking the Learn Java course on Codeacademy for a more in-depth understanding of Java programming.
Variables & Data Types
- Variables are containers for data. Always assign a default value.
- Use
snake_casefor names. - Common data types:
Datatype Description Example intA non-decimal number int team_number = 4143;doubleA decimal number double pi = 3.1415;booleanA value that is either true or false boolean robot_is_on = true;StringA list of characters (a.k.a. text) *note the capitalization in StringString message = "I am a string!"; - Enums: Enums are a special type of class that represents a fixed set of constants. They are used to define a variable that can only take one of a predefined set of values.
Operators
- Arithmetic:
Operator Description Compatible Datatypes Example +Addition int,double,String1 + 1 = 2
1.5 + 1.0 = 2.5
"MARS" + "WARS" = "MARSWARS"-Subtraction int,double2 - 1 = 0
2.5 - 0.5 = 2.0*Multiplication int,double9 * 3 = 27
15 * 0.5 = 7.5/Division int,double9 / 3 = 3
8.0 / 3.0 = 2.6666%Modulo int,double10 % 3 = 1
8.5 % 3.0 = 2.5 - Assignment:
Operator Description Example Equivalent Expression =Assignment int x = 0;
// Store the value 0 in variable xint x = 0;+=Addition Assignment int x = 1;
x += 1; // x is now 2x = x + 1;-=Subtraction Assignment double x = 3.14;
x -= 0.14; // x is now 3.0x = x - 0.14;*=Multiplication Assignment int x = 2;
x *= 6; // x is now 12x = x * 6;/=Division Assignment int x = 9;
x /= 3; // x is now 3x = x / 3; - Date Truncation: Be careful when performing math between an
intand adouble. You cannot store decimal precision data in anintdatatype. Any decimal precision will be truncated.
Conditions
- Comparison:
Operator Description Example ==equal (1 == 2) // false
(false == false) // true!=not equal (1 != 2) // true
(false != false) // false<,<=less than, less than or equal to (-10 < 0) // true
(-10 <= -10) // true>,>=greater than, greater than or equal to (10 > 0) // true
(10 >= 10) // true - Logical:
Operator Description Example !logical NOT !(false) // true
!(true) // false&&logical AND (false && false) // false
(false && true) // false
(true && false) // false
(true && true) // true||logical OR (false || false) // false
(false || true) // true
(true || false) // true
(true || true) // true - Use comparison operators inside
()for if-statements. - Only compare compatible types.
- If Statement:
if (x > 0) { ... } - Else If Statement:
else if (x < 0) { ... } - Else Statement:
else { ... } - Switch Statement: Use
switchfor multiple conditions on the same variable.- Use
breakto exit a switch case. - Use
defaultfor a fallback case in a switch statement.
- Use
Loops
- Use
forandwhileloops to repeat code.- For Loop:
for (int i = 0; i < 10; i++) { ... } - While Loop:
while (condition) { ... }
- For Loop:
- Avoid
whileloops in robot code to prevent infinite loops. - Increment Operators:
Operator Description Compatible Datatypes Example ++Increment int,doubleint x = 0;
x++; // x = 1--Decrement int,doubleint x = 0;
x--; // x = -1 - break and continue statements:
breakexits the loop immediately.continueskips the current iteration and continues with the next one.
Methods
- Methods are reusable blocks of code.
- Use
camelCasefor method names. - Method Declaration:
returnType methodName(parameters) { ... } - Call a method:
methodName(arguments); - Parameters: Use parameters to pass data into methods by defining them in the method declaration.
- Return a value: Use
returnto send a value back from a method.voidmethods do not return a value. - Variables inside methods are not accessible outside.
Arrays & ArrayLists
- Arrays store multiple values of the same type.
- Indexing starts at 0.
- Arrays are fixed size and ArrayLists are dynamic arrays.
- Array Specific:
- Declare:
int[] numbers = new int[5]; - Initialize:
int[] numbers = {1, 2, 3, 4, 5}; - Access:
numbers[0]for the first element.
- Declare:
- ArrayList Specific:
- Declare:
ArrayList<Integer> numbers = new ArrayList<>(); - Initialize:
numbers.add(1); - Access:
numbers.get(0)for the first element.
- Declare:
- Use
forloops to iterate through arrays and ArrayLists. - Use
for-eachloops for cleaner iteration:for (int num : numbers) { ... }
Classes & Objects
- Java is class-based. Classes are blueprints; objects are instances.
- Use
PascalCasefor class names. - Use constructors to initialize objects.
- Create an object:
ClassName obj = new ClassName(); - Access fields and methods with dot notation:
obj.fieldorobj.method(). - Prefer getter/setter methods for encapsulation.
- getters return field values:
getFieldName() - setters modify field values:
setFieldName(value) - Inheritance: Use
extendsto create a subclass. Subclasses inherit fields and methods from the superclass.