## Values

A value can be assigned directly to a variable, using an explicit value. Such an explicit value is called a literal. A character literal, in this case the character literal b, can be assigned to a variable with the name a using the assignment

        char a = 'b';


As can be seen, the assigment is done by placing the character b, enclosed in single quotes, at the right hand side of an equals sign. In this assignment, the word char is placed in front of the variable a. This means that the variable is being declared, to be of the type char. A variable of type char can hold values which are characters, such as the letters a to z, but also special signs, like semicolon (;), comma (,), and exclamation mark (!).

A variable can also be assigned a numerical value. There are two kinds of numerical values. One kind is referred to as floating point values, and the other kind is referred to as integer values. A floating point variable represents decimal numbers and an integer variable represents whole numbers. As an example, the decimal value for $$\pi$$, rounded by the programmer to 7 decimals, can be assigned to a variable named pi, using the assignment

        pi = 3.1415927;


In the above assignment, the variable pi is assumed to be declared, before it is being assigned a value. The declararation is done, using the word double to indicate a floating-point variable with double precision, as

    static double pi;


In this declaration, the word static is also used, in front of the the word double. The reason for this is that the class to which the variable pi belongs will never be instantiated in the program where it is being used. The concept of class instantiation can be thought of as creating a variable, referred to as an object, then being an instance of the class from which is created. The so created object contains variables for the data belonging to the class, and it also contains the methods defined as belonging to the class.

As an example of an assignment of an integer variable, a variable named i can be assigned the value 5, and at the same time being declared as a variable of type int, using also the word static as above in the declaration of the variable pi, as

    static int i = 5;


Variables of type int can hold values which are whole numbers. Positive numbers as well as negative numbers are allowed. A complete program with variables, and with literal values being assigned to these variables, is shown in Figure 3.

class NamesAndValues
{
static int i = 5;
static double pi;

public static void main(String [] args)
{
char a = 'b';
pi = 3.1415927;
System.out.println("i is " + i +
" and pi is " + pi +
" and a is " + a);
}
}


Figure 3. A program with variables, and with assignments of values to these variables.

This the Java view - other views are C - Python

The program in Figure 3 contains the three variables described above, named i, pi, and a. The program also contains an instruction for printing the values of the variables. The printing is done by a call to a method named println. This method belongs to a class called out, which in turn belongs to a class called System. Therefore, the names System and out are used in the method call, as

        System.out.println("i is " + i +
" and pi is " + pi +
" and a is " + a);


In this call to the method println, strings of text are combined with the variables. The combination is done using the plus-sign, which connects the different parts into a new string. The so connected string appears as the argument to a call to println, and it is therefore printed when the program runs.

The program in Figure 3 can be run, as described in Section How to make it run. The output of the program is then shown, as

i is 5 and pi is 3.1415927 and a is b