> For the complete documentation index, see [llms.txt](https://velocity-raptors-9450.gitbook.io/intro-to-frc-programming-romi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://velocity-raptors-9450.gitbook.io/intro-to-frc-programming-romi/intro-to-java/fundamentals-of-java/variables.md).

# Variables

Variables are elements in code that can store data.&#x20;

## Data Types

Data in Java have data types to help differentiate objects from each other.\
There are two general categories of data types— Primitives and Composites. We'll go over commonly used data types here.&#x20;

### Primitives

Primitives are simple data types that require minimal storage and are generally straightforward. Here are some examples.

| Data Type          | Description                                                                                                | Example(s)                                                                                                         |
| ------------------ | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| Integer (int)      | Whole numbers, no decimals. NOTE: Integers have a specific range- they don't encompass every whole number. | int num = 1;                                                                                                       |
| Doubles            | numbers with decimals. Also known as `floats`. Like ints, they have a max/mininum size.                    | double num = 0.24;                                                                                                 |
| Characters (chars) | Digits, icons, or letters. Note that a digit's value is different from what it looks like.                 | <p>char c = 'A';<br>char fourAsDigit = '4'; (not the same as the number 4)</p>                                     |
| Booleans           | Conditions, used for logic                                                                                 | <p>boolean incomingTest = true;<br>boolean studying = false;<br>boolean correctMath =</p><p> (2 == 4); // true</p> |

### Composites

Composites can store much more complex data. Due to their complexity,  they are treated differently by the computer (except for strings). Here are some noticeable ones:

| Data Type               | Description                                                                                                         | Example(s)                                                                  |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| Strings                 | Store text (multiple chars)                                                                                         | string intro = "Hello World!"                                               |
| Arrays                  | Store multiple values of the same type                                                                              | <p>int\[] array = {5, 4, 3, 2, 1}<br>char\[] msg = {'H', 'E', 'L', 'P'}</p> |
| User-Created Data Types | Don't worry about this— we'll cover it later. Its fields and purposes could change depending on what it's used for. | <p>Object o = new Object()<br>Animal cat = new Animal("Cat");</p>           |

You might notice something about composites and primitives— composites are made up of many different primitives! Due to their nature, composites are much more data-intensive than primitives. &#x20;

## Creating & Modifying Variables

All you need to do is define a variable name and set its data type. It's that simple.

```java
[data type] variableName;
/* example */
int number;
```

Variables are kinda useless if you can't assign them values. To do this, just set the variable's name to some value, like so:

```java
int number = 3;
/* you could also do this */
int number;
number = 3;
```

Note that you can only assign variable values of the same type as the variable, or your code will break. For example, you can only set variables that have a data type of "String", strings. You cannot assign them stuff like booleans, or your code will be real happy at you.

{% hint style="info" %}
However, there are some exceptions to this— doubles and integers. If you assign an integer variable a double, it would just round down that value and set that whole number to an integer. This is a Quality of Life feature.
{% endhint %}
