Pin Me

Java Programming For Beginners - Part 11

written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 10/2/2008

In this article, we begin to delve into the nitty gritty of Java programming. We show how to declare variables and how to use the assignment statement.

  • slide 1 of 3

    Programming with variables

    To reinforce the ideas discussed so far, let us write a program which adds the numbers 14 and 25 and prints the sum.

    We would need storage locations for the two numbers and the sum. The values to be stored in these locations are integer values. To refer to these locations, we make up the names a, b and sum, say. Any other names would do. In Java, as in all programming languages, there are rules to follow for making up variable names. For now, we use the rule that a name must start with a letter, consist of letters and/or digits and cannot contain spaces.

    One possible algorithm for solving this problem might look like this:

    set a to 14

    set b to 25

    set sum to a + b

    print sum

    The algorithm consists of four statements. The following explains the meaning of each statement:

    set a to 14 - store the number 14 in memory location a; this is an example of an assignment statement; we assign a value to a variable.

    set b to 25 - store the number 25 in memory location b.

    set sum to a + b - add the numbers in memory locations a and b and store the sum in memory location sum. The result is that 39 is stored in sum.

    print sum - print (on the screen) the value in sum, i.e. 39.

    The following shows how we can write this algorithm as a Java program.

    public class PrintSum {public static void main(String[] args) {int a, b, sum;a = 14;b = 25;sum = a + b;System.out.printf("%d + %d = %d\n", a, b, sum);}

    We have named the class PrintSum. When we do so, Java requires that we store the program in a file called

    When run, this program will print

    14 + 25 = 39
  • slide 2 of 3

    Declaring variables and assignment statement

    In Java, variables are declared as integer using the required word int. In programming terminology, we say that int is a reserved word. Thus, the statement:

    int a, b, sum;

    ‘declares’ that a, b and sum are integer variables. In Java, all variables must be declared before they are used in a program. Note that the variables are separated by commas, with a semicolon after the last one. If we were declaring just one variable (a, say), we would write:

    int a;

    The statement

    a = 14;

    is Java’s way of writing the assignment statement

    set a to 14

    It is sometimes pronounced “a becomes 14". In Java, an assignment statement consists of a variable (a in the example), followed by an equals sign (=), followed by the value to be assigned to the variable (14 in the example), followed by a semicolon. In general, the value can be a constant (like 14), a variable (like b) or an expression (like a + b). Similarly, “set b to 25" is written as:

    b = 25;

    and “set sum to a + b" is written as:

    sum = a + b;

    One final point: the variable sum is not really necessary. We could, for instance, have omitted sum from the program altogether and used:

    int a, b;
    a = 14;
    b = 25;
    System.out.printf("%d + %d = %d\n", a, b, a + b);

    to give the same result since Java lets us use an expression (e.g. a + b) as an argument to printf. However, if the program were longer and we needed to use the sum in other places, it would be wise to calculate and store the sum once (in sum, say). Whenever the sum is needed, we use sum rather than recalculate a + b each time.

© Copyright 2016