How To Declare Variables And Write An Assignment Statement In Java

How To Declare Variables And Write An Assignment Statement In Java
Page content

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 PrintSum.java.

When run, this program will print

14 + 25 = 39

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.

References

Java Programming – A Beginner’s Course

Related programming references

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions