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.
Variables & Data Types
- Variables are containers for data. Always assign a default value.
- Use
snake_case
for names. - Common data types:
Datatype Description Example int
A non-decimal number int team_number = 4143;
double
A decimal number double pi = 3.1415;
boolean
A value that is either true or false boolean robot_is_on = true;
String
A list of characters (a.k.a. text) *note the capitalization in String
String 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
,String
1 + 1 = 2
1.5 + 1.0 = 2.5
"MARS" + "WARS" = "MARSWARS"-
Subtraction int
,double
2 - 1 = 0
2.5 - 0.5 = 2.0*
Multiplication int
,double
9 * 3 = 27
15 * 0.5 = 7.5/
Division int
,double
9 / 3 = 3
8.0 / 3.0 = 2.6666%
Modulo int
,double
10 % 3 = 1
8.5 % 3.0 = 2.5 - Assignment:
Operator Description Example Equivalent Expression =
Assignment int x = 0;
// Assigns a value (right side) to a variable (left side) </td>int 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
int
and adouble
. You cannot store decimal precision data in anint
datatype. 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
switch
for multiple conditions on the same variable.- Use
break
to exit a switch case. - Use
default
for a fallback case in a switch statement.
- Use
Loops
- Use
for
andwhile
loops to repeat code.- For Loop:
for (int i = 0; i < 10; i++) { ... }
- While Loop:
while (condition) { ... }
- For Loop:
- Avoid
while
loops in robot code to prevent infinite loops. - Increment Operators:
Operator Description Compatible Datatypes Example ++
Increment int
,double
int x = 0;
x++; // x = 1--
Decrement int
,double
int x = 0;
x--; // x = -1 - break and continue statements:
break
exits the loop immediately.continue
skips the current iteration and continues with the next one.
Methods
- Methods are reusable blocks of code.
- Use
camelCase
for 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
return
to send a value back from a method.void
methods 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
for
loops to iterate through arrays and ArrayLists. - Use
for-each
loops for cleaner iteration:for (int num : numbers) { ... }
Classes & Objects
- Java is class-based. Classes are blueprints; objects are instances.
- Use
PascalCase
for class names. - Use constructors to initialize objects.
- Create an object:
ClassName obj = new ClassName();
- Access fields and methods with dot notation:
obj.field
orobj.method()
. - Prefer getter/setter methods for encapsulation.
- getters return field values:
getFieldName()
- setters modify field values:
setFieldName(value)
- Inheritance: Use
extends
to create a subclass. Subclasses inherit fields and methods from the superclass.