This lesson is still being designed and assembled (Pre-Alpha version)

Variables and Types

Overview

Teaching: 0 min
Exercises: 0 min
Questions
  • What is a variable?

  • What is a type?

Objectives
  • Use variables to store values and do calculations

In computer programming a variable is a symbolic name for a specific area in the computer’s memory where a value is stored. In Java we have to define a variable before we can use it and when we define it, we have to specify the type of information we will be storing in the variable.

In Java we have the following types available which we can use to define what information will be stored in a variable:

In addition to these types, Java provides special support for strings with the String type. Note the capitalisation of the String type. We’ll just mention the reason for the capitalisation but we won’t discuss it in more detail. String is a class whereas the other types we mentioned are known as primitives.

Each of these types use a different amount of memory.

Create a new repl and call it 03.Variables. Enter the following code and compile and run it:

1  // Define the class
2  class Main {
3   // Define the main method
4   public static void main(String[] args) {
5     // Define variables and initialise them with values;
6     String stringVariable = "Print ten plus ten: ";
7     int integerValue = 10;
8     // You can also define a variable without initialising it;
9     int total;
10    System.out.println(stringVariable);
11    System.out.println(stringVariable + integerValue + 10);
12    System.out.println(stringVariable + integerValue + integerValue);
13    System.out.println(stringVariable + (integerValue + integerValue));
14    // Do some multiplication and store the value in the variable total
15    // Storing a value in a variable is called assingment
16    total = integerValue * integerValue * 10;
17    System.out.println("Print the value of total: " + total);
18  }
19}

Print ten plus ten: 
Print ten plus ten: 1010
Print ten plus ten: 1010
Print ten plus ten: 20
Print the value of total: 1000

Let’s look at the program line by line:

  1. The first line is just a comment.
  2. Define the beginning of a class named Main.
  3. Another comment.
  4. Declaring the main method (the front door to the program).
  5. Yet another comment.
  6. String specifies the type of value that will be stored in the variable called stringVariable. The = is an assignment operator. The variable stringVariable will be assigned the value specified to the right of the =, which in this case is Print ten plus ten:.
  7. int specifies the type of value that will be stored in integerValue. The variable integerValue will be assigned the value specified to the right of the =, which in this case is 10.
  8. Another comment.
  9. On this line we declare yet another integer, called total, using the int keyword but we don’t assign a value to the variable.
  10. Print the content of the variable stringVariable to the console.
  11. Convert the number 10 and the integer value stored in integerValue to strings and then concatenate them to the string value stored in stringValue. Print the final string to the console.
  12. Convert the integer value stored in integerValue to a string and then concatenate that twice to the string value stored in stringValue. Print the final string to the console.
  13. Add the integer value stored in integerValue to itself, convert the result to a string and then concatenate that to the string stored in stringAviable. Print the final string to the console.
  14. Another comment
  15. … and yet another comment.
  16. The variable total is assigned the value of the calculation integerValue * integerValue * 10
  17. Convert the integer value stored in total to a string and concatenate it to the string Print the value of total:. Print the final string to the console.
  18. Curly brace to close the main method.
  19. Curly brace to close the class called Main.

Challenge

Before leaving you to experiment with what you have learnt so far we are going to mention a couple more things to make it more interesting.

  • Operators: Operators are characters that are used to perform actions on values.
  • Operators have precedence. That means that some operators have higher precedence than others. Operators with higher precedence are evaluated before operators with lower precedance. Operators on the same level of precedence are evaluated from left to right. Below is a table of operators. The subheadings in the table groups the operators with the same precedence together. The groups are listed according to the precedence order. The closer to the top of the table a group appears, the higher its precedence.

Take the next few minutes and see if you can write some code to do the suggested calculations below. Separate your code with white space and comments. In the next lessons we will then organise our code in a much more efficient way. Don’t just “hard code” values, but use variables and assignments.

  1. Converting fahrenheit to celsius. The formula used for this is: ((fahrenheit - 32) * (5/9))
  2. Converting celsius to fahrenheit. The formula used for this is: ((celsius * 9/5) + 32)
  3. Converting celsius to kelvin: The formula used for this is: (celsius + 273.15)
  4. Converting fahrenheit to kelvin: Can you work this one out yourself by using what you have already done?
Operator Description Example
Postfix    
++ (Increment) Increases the value of operand by 1. B++ gives 21
– (Decrement) Decreases the value of operand by 1. B– gives 19
Multiplicative    
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0
Additive    
+ (Addition) Adds values on either side of the operator. A + B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
Assignment    
= (Simple assignment operator) Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C

Key Points