-
Notifications
You must be signed in to change notification settings - Fork 0
Coding With Java
Java is an object-oriented programming language featuring classes, platform independence, and many similarities to C++. It's easy to pick up, and is a language that many applications use due to its flexibility.
Good places to get started with learning are Codecademy and Sololearn.
Variables are just as they sound- things that vary. They're used to store information that can be called again later when needed.
Variables come in a handful of types:
- int - whole numbers
- float - numbers with decimal points
- double - like floats, but can hold more data
- char - single letters
- bool - true or false
- string - strings of letters
- void - technically "nothing," only used for function return types
Along with variable types like these, there are certain keywords that come with declaring variables:
- public - the object will be accessible not just in that class/package/scope
- private - the variable will only be accessible in that class/package/scope
- static - if the object is part of a class, any changes that are made to it will be reflected in all instances of the class
- final - like
constin other languages, it creates variables that can't be initialized more than once, classes that can't be extended, and functions that can't be defined again.
Declaring variables looks something like this:
public static int exampleInt = 10;
private final exampleString = "Hello, world!";The syntax is [private/public] [static] [final] vartype varname; or [private/public] [static] [final] vartype varname = vardata;
Functions are containers for statements that may be repeated over and over. They all have:
- A
returnType, which is one of the above variables - A
funcName, which is the name of the function - A set of
parameters, which may or may not actually be present
Functions can be declared using the same keywords as above, thought static doesn't do the same thing it does for variables- instead, it allows access to static variables, and only static variables.
Classes are a type of organization used to hold sets of functions and variables that need to be replicated multiple times in the code.
Robot objects are parts of the robot that you can interact with through programming.
Some examples include:
- Solenoids
- Motor controllers (Victors, Talons)
- Cameras/vision elements
- Limit switches
- Encoders
- Gyros
- Other Sensors (ultrasonic, infrared)
How you declare robot objects will depend on where you're using them, but it'll look something like this:
public static TalonSRX exampleTalon = new TalonSRX(0); // Declares new TalonSRX connected to the device number 1
public static Solenoid exampleSolenoid = new Solenoid(1) // Declares a new Solenoid connnected to channel 2
// NOTE: arrays like devices are 0 indexed, meaning that the first object is 0, second is 1, etcTalonSRX and Solenoid are two object types.
Next are the variable names, keywords used to access them later.
The new keyword is a way of creating a new object/class instance, with the class' constructor following it. Both of these constructors take an int as a parameter, representing the device or channel they're interacting with.
If all else fails, check the manual (code found here)
Git CLI docs | VSCode site | Screensteps for Java | CTRE Phoenix Downloads
Credit: greysdawn Example Repo