Galatia Academy

THIS IS CURRENTLY A DRAFT - IT IS STILL IN EDITING.

Variables, Types and Operators

Variables and Types are the building block of how to interact with data in Programming. They are one of the most important components to understand.

Variables

In programming, you want a way to store the data that your program needs and creates. That is what variables are for. Variables by themself are a relatively simple concept to grasp. They are something you can store data in and retrieve data from.

The process of creating a variable is called a declaration statement . The declaration has two components, first the type, secondly it's name. What exactly a type is comes up in the next section, for now let us decide on int for its type. The name can be whatever you want, but there are a few rules. Variables cant have spaces, hyphens or use be named after an existing java keyword. There are also conventions to follow. Generaly people write variable names in camel case (i.e myVariable). The name of the variable should also be indicative of what it stores (i.e userResponse).

//"int" is the type, "myVariable" is the name. Both together declare a variable.
int myVariable;

This just creates the variable though, you also want to actually assign something to it. You do this with the = operator.

//We assign the value 5 to myVariable.
int myVariable = 5;

Now that we assigned something to myVariable, it is stored in there. To make use of the value in this variable, we can actually re-use the = operator, just on the other side of the operation.

int myVariable = 5;
int myOtherVariable = myVariable; //myOtherVariable now holds the value 5.

Another way to make use of the value of a variable is by passing it as an argument to a method. We have not covered methods yet, so just keep it in the back of your mind for now. The println in the example below will take the value of our variable and print out 5 to the terminal.

Types

What are types?

Types are what define what the data that we store looks and behaves like. In the previous section, we have used the int type for our variable. Int is short for Integer, and represents whole numbers. They cannot have a decimal part. An int are also what is called a primitive type. Primitives are the building blocks of all other types in java.

Below is an overview of the different primitive types in java, and how to assign their values.

//the ones you'll probably use
boolean truthValue = false; //booleans, representing "true" or "false"
int number = 1; //integers, representing every whole number between -2,147,483,648 and 2,147,483,647.
float decimalNumber = 1.0f; //a number representing any number with stuff after the decimal place.
char character = 'a' //A single character of ASCII text

//the ones you might use
double aDouble = 1.0d; //a number representing any number with stuff after the decimal place, but much more precisely.
long aLong = 1L; //64-bit signed integers, representing every whole number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807, inclusive.

//the ones you probably won't use
byte aByte = 0b10101010; //a single byte of memory, its value ranges from -128 to 127.
short smallValue = 1; //a smaller whole number type, that can only store values between -32,768 to 32,767.

Casting

Sometimes you have a primitive type in one format, but require it in another. For this you can cast it. Certain types of casts are automatic, those are called implicit casts. If we for example try to assign an integer to a float variable, that will work fine. This works because floats can store larger values than integers can. If we invert that assignment though, we get an error.

float myFloat = 1; //totally fine
int myInt = 1.2f; //throws an error

This is because converting a double to an integer can mess with the data, due to floats being able to store larger values. To make developers aware of this risk, java requires you to explicitly cast the value. An explicit cast is done by placing an opening (, the name of the type to cast to, and a closing ).

double myFloat = 1; //totally fine
int myInt = (int) 1.2f; //converts fine now

Even so though, you have to keep in mind that an integer can not store the same data as a float. When you convert a float to an integer, the decimal part will be lost. Java does not round the float to an integer, It essentialy just cuts off the decimal portion.

Operators

Java has many available operators. As the name implies, they perform some kind of operation. Those can be broadly fit in to the following groups:

Arithmetic Operators

Arithmetic Operators are for your math essentials. You can use them to calculate different values with ints, longs, floats and doubles. They work in a similar way as they would on your calculator. Those operators are +, -, *, / and %.

int MyVariable1 = 2 + 2; // = 4
int MyVariable2 = 2 - 2; // = 0
int MyVariable3 = 3 * 3; // = 9
int MyVariable4 = 3 / 3; // = 1

int myVariable5 = 4 % 2; // = 0          The modulo operator, returns the remainder value from a division.

int MyVariable6 = 2 + 4 * 2;  // = 10    You can combine multiple terms.

Assignment Operators

We have already learned about the = operator, also called the assignment operator, earlier on this page. It's role is to assign values to a variable. Java has multiple variations of this operator that each provide some commonly needed utility. Those are the compound assignment operators +=, -=, *=, /= and the incremental/decremental operators ++ and --.

int myVariable = 0; //Declares and assigns "myVariable" with the value 0.
myVariable = 5; //Sets myVariables value to 5;

//Compound assignment operators, combines an assignment with an arithmetic operator.
myVariable += 2; //Increases "myVariable" by 2, equivalent to "myVariable = myVariable + 2"
myVariable -= 2; //Subtracts "2" from myVariable, equivalent to "myVariable = myVariable - 2"
myVariable *= 2; //Multiplies myVariable by 2, equivalent to "myVariable = myVariable * 2"
myVariable /= 2; //Divides myVariable by 2, equivalent to "myVariable = myVariable / 2"

//Incremental & decremental operators, shortcuts for increasing or decreasing a number by 1.
myVariable++; //Increments myVariable by 1, equivalent to "myVariable = myVariable + 1";
myVariable--; //Decrements myVariable by 1, equivalent to "myVariable = myVariable - 1";

Relational Operators

In programming, you often need to check if something is the case or not. For that purpose, you have relational operators. As you have seen in the Types section, java has a type called boolean. A boolean is a type where the value can only ever be true or false. The result of a relational operator is always a boolean, so either true or false. The value of this type will become more appearent on the control flow page after this one.

The basic relational operator is the Equal to operator ==. It will compare both its left and right operands, and return true if they are equal and false if they are not equal. Its direct neighbour is the Not Equal to operator !=. This operator reverses the idea, and returns true if something is not equal.

boolean myVariable1 = 2 == 2; // = true
boolean myVariable2 = 3 == 3; // = true
boolean myVariable3 = 2 == 3; // = false

boolean myVariable4 = 2 != 2; // = false
boolean myVariable5 = 3 != 3; // = false
boolean myVariable6 = 2 != 3; // = true

boolean myVariable7 = 'a' == 'a'; // = true, can also compare non-numerical values!
boolean myVariable8 = 'a' == 'A'; // = false, case matters!

After those two, there are four more relational operators. Those check if a value is either greater or smaller than another one. Those are >, <, >= and <=.

boolean myVariable1 = 3 > 2; // like saying "3 is greater than 2", which is true
boolean myVariable2 = 3 < 2; // like saying "3 is smaller than 2", which is false.

boolean myVariable3 = 2 > 2; // like saying "2 is greater than 2", which is false.
boolean myVariable4 = 2 >= 2; // like saying "2 is greater than or equals to 2", which is true.
boolean myVariable5 = 2 <= 2; // like saying "2 is smaller than or equals to 2", which is true.

Logical Operators

Logical operators operate on boolean values and return a new boolean value as a result. They become required once you want to check for more complex cases. The logical operators are &&, || and !.

The AND & OR Operators

The AND operator && evaluates the operants to its left and its right, and returns true if both of them are true. Meanwhile the OR operator || checks both of its operants and will return true if either of them is true.

 // AND operator
boolean myVariable1 = true && true; // = true;
boolean myVariable2 = true && false; // = false;
boolean myVariable3 = false && true; // = false;
boolean myVariable4 = false && false; // = false;

// OR operator
boolean myVariable5 = true || true; // = true;
boolean myVariable6 = true || false; // = true;
boolean myVariable7 = false || true; // = true;
boolean myVariable8 = false || false; // = false;

// More realistic examples
boolean isLightSwitch1Active = true;
boolean isLightSwitch2Active = false;
boolean enableLights = isLightSwitch1Active || isLightSwitch2Active; // true

boolean isCorrectUsername = true;
boolean isCorrectPassword = false;
boolean allowLogin = isCorrectUsername && isCorrectPassword; // false

The NOT Operator

The NOT operator ! acts like an inversion to a boolean value. when applied to a boolean, it will return the opposite value. a true becomes false, and a false becomes true.

boolean isLoggedIn = false;
boolean requireLogin = !isLoggedIn; //true

Operator Precedence

As you may remember from school, mathematical operators arent just evaluated from left to right. Same applies for operators with programming languages. Specific operators will always evaluate their operands before others. The common example is that multiplications and divisions are evaluated before additions and subtractions, and you can see the same behaviour with many of the operators.

In the table below, you can see the order that they are evaluated by. This table is slightly simplified to remove operators that this guide has left out. You will notice that the operators with the lowest precedence are the assignment operators. If you check back on our examples so far, that makes sense. Every operator on the right side of the = sign was evaluated before the = operator itself did its job.

Priority Operators
1 ()
2 i++ i-- !
3 * / %
4 + - (also String concatenation)
5 < > <= >=
6 == !=
7 &&
8 ||
9 = += -= *= /=

At the top you can see the special rule for parenthesis. Parenthesis always have their content evaluated before the outer content.

int result1 = 2 + 2 * 4; // 10
int result2 = (2 + 2) * 4; // 16

//Also works for logical operators
boolean result3 = true || false && false; //true
boolean result4 = (true || false) && false; //false

Reference Types

So far, we have worked with primitive types. Java has another kind of type, called reference types. Every type in java that is not a primitive is a reference type.

Primitive Type Reference Type
boolean Boolean
int Integer
long Long
float Float
double Double
char Character
String
Array
ArrayList
HashMap
Every other type, including your own custom types

Reference types can be a bit more complicated to understand and explain. Where as variables with primitive types store the value in the variable itself, for reference types, variables instead store a reference to the object in their variable. In Java, every value that is not a primitive is an object. What this means concretely will become more appearent at the classes chapter.

Arrays

The Array type is a reference type. It is used to store a collection of multiple values. We will go in to more details on the array type on the Arrays, Lists and Maps page, but we will be using it here since it visualises some differences between primitive and reference types quite well. Note that arrays have special syntax not found in other types. Instead of declaring a variable like Array myVariable, you instead declare it by adding [] after the type that the collection should hold.

int[] values1 = {1, 2, 3}; //A collection of multiple integers
float[] values2 = {1.0f, 2.0f, 3.0f}; //A collection of multiple floats
char[] values3 = {'a', 'b', 'c'}; //A collection of multiple characters

You can modify an entry in an array with the following syntax. It works by placing [] after the variable name and specificying the position in the collection that you want to modify. Note that in most programming languages, the index starts at 0.

int[] values = {1, 2, 3};
values[0] = 10; // At index 0, replace the first value with 10 (was "1" before)
values[1] = 20; // At index 1, replace the second value with "20" (was "2" before)
values[2] = 30; // At index 2, replace the third value with "30" (was "3" before)

Primitive VS Reference Types

With arrays established, lets compare some of the differences that you will encounter with primitive and reference types.
As mentioned, primitive variables hold their value, and reference variables hold a reference to their object. One key distinction is that when you assign a primitive variable to another primitive variable, it will copy the value. If you assign a reference variable to another reference variable, it will copy the reference.

This manifests in a way that should become more clear in the following code example, try to run it!

When you look at the terminal output after running it, something should stick out. The value of values2 is changed, despite our code only running values1[0] = 1000, which should have only changed values1. In fact, values2 seems to have the exact same value as values1 does.

This gets back to reference types copying their reference, not their value when assigned to a variable. What this means concretely is that values1 and values2 point to the same object in memory, not two seperate objects. Any change to either variable will effect the other.

.equals vs ==

Wrapper Types

Strings

Null

Relational Operators on Reference Types

Test Project

Last updated 5/25/2026, 12:24:17 PM by maver · revision 19