← Home

Variables

A variable is a piece of information or a value that can be changed. A variable always has a name known as a variable identifier, which acts a reference to the piece of information it holds. The variable identifier must not contain any spaces. For instance my_variable is a valid variable name, whereas my variable is not. A variable must also have a data type, that it is defined as. Once a variable is defined, it can be assigned a value. Data types include integers, strings, booleans, etc.

Data types

Strings

A string is a series of characters. To define a string called name do:

1String name;

Once name is defined, you can assign it to a value:

1name = "George";

If you want, you can define it and initialise it in a single line:

1String name = "George";

And to print it out:

1System.out.println(name);

If you want to display two lines worth of text, for example in a JPanel (which you will discover in a long time), or even in the console, you can use this:

1System.out.println(name + "\n" + name);

Integers

An integer is a whole number. Integers are stored as the data type int. To define an Integer with variable identifier age do:

1int age = 15;

You can increment and decrement variables by one, by using unary operators, that is to say operators that take one value:

1age++; // Age now equals 16
2age--; // Age is back to 15, as java works sequentially, that is to say, line-by-line.

Binary operators like +, -, / and * take two int values.

You will learn how to convert between variables at a later point.

Booleans

A boolean is a variable that has two possible values - true or false. That's it:

1boolean myBoolean = true;

Chars

A char is defined as a single letter of a String. So, "Hello" is a String but the char at index 0 is H. Chars are declared as follows:

1char myChar = 'h';

Bear in mind that when declaring chars, you must use quotation marks, not speech marks.

Ex 1

Write a program in which you initialise an int, a char, a boolean and a String to suitable values of you choice, then get the program to print out the values of each variable on a new line. So if the int is equal to 4, the first line to be printed would be: The value of the int is 4.

Primitives versus Classes

You may have picked up on the fact that String is capitalised, while int, char and boolean are not. This is because int, char and boolean are all primitive data types, while String is a class data type. These may not mean anything to you yet, but in essence there are 8 built-in primitives within Java. Primitives cannot be added or removed by the programmer; they are the DNA of the programming language. Primitives represent one scalar value. Classes are far more powerful than primitives. They shift the power to the programmer, allowing the programmer to create classes, which are composed from other data types. Java comes with a bunch of classes built in, namely String.

But what about Integer and Boolean? These are indeed classes, that wrap around their respective primitives. So the following would be perfectly valid:

1Integer age = 13;
2Boolean alive = false;

However using these instead of primitives has an unnecessary performance cost, and should not be used. There are valid reasons for using them, that will come up in Chapter II, but as for now they are a burden.

Selection (Comparing values)

You can compare and select values by using an if/else statement:

1int x = 5;
2int y = 4;
3
4if(x < y) {
5  System.out.println("x is smaller than y");
6} else {
7  System.out.println("x isn't smaller than y");
8}

The following operators apply:

Operator Meaning
< Smaller than
> Greater than
<= Smaller than or equal to
>= Greater than or equal to
== Is equal to
!= Is not equal to

These are known as relational operators.

Bear this in mind: because String isn't a primitive data type, the == operator doesn't work.

N.B. Don't mix up == and =. One is for comparison, the other is for assignment.

On a separate point, the following code would work:

1int x = 5;
2int y = 4;
3
4if(x < y); {
5  System.out.println("x is smaller than y");
6}

Now, if you look at that closely you will see that there is a semi-colon before the opening brace of the if statement. By all means, that could be easily mistaken as an error, but is not. If you do not have an else branch with this if statement, the code would work. However, there is no need to ever use this as the statement will work perfectly well without the semi-colon. This is just a passing statement, and there is no problem if this little thing is forgotten.

There are also things called logical operators, and there are another 4 of those. These are them, amd they mostly work with boolean values:

Operator Meaning
! NOT OPERATOR - returns true if the value is false and vice versa
&& AND operator - returns true if both values next to it are true
^ XOR OPERATOR - returns true if one value is true and one is false

I mentioned 4 operators, but the table only lists 3. This is because this file is being made in a format called MarkDown, and the table system in MarkDown is hell on earth. Suffice to say, it does not allow me to make the final operator, so I'm going to have to tell you in a not-so-pretty format. The final one is the OR operator, signified by this : ||. This will return true if one or two of the values next to it are true, and doesn't require both to be.

Else if

If you want you can do can have multiple comparisions;

1int x = 5;
2int y = 4;
3
4if(x < y) {
5  System.out.println("x is smaller than y");
6} else if(x > y) {
7  System.out.println("x is bigger than y");
8} else {
9  System.out.println("x is the same as y");
10}

You don't necessarily need the else branch:

1boolean alive = true;
2
3if(alive == true) {
4  System.out.println("You're alive, good for you");
5}

Time to do some maths →