Programming Tutorials

constants and variables

By: aathishankaran in Java Tutorials on 2007-02-01  

Main task of a program is to deal with data. Data used in programs are categorized as:

  • Constants
  • Variables

Constants
Constants are data elements whose values do not change during program execution. They are of two types:

  • Alphanumeric
  • Numeric

Alphanumeric constants

Comprise of A-Z, a-z, 0-9, symbols like !@#$%^&*()[]{}.,;:'/\. Each letter, symbol or number used is called a character, for example, the alphabet A. Alphanumeric constants cannot be used for arithmetic calculations. A set of characters is called strings. For example, Robert De Niro. Strings are enclosed within double quotes. Characters are enclosed within single quotes.

Numeric constants

  • are of two types:
    Integer: Whole numbers. Example: 45,99
    Float : Numbers with decimals. Example: 55.3,7.344
  • Can be used for calculations
  • Can be positive or negative

Variables

String and numeric values can be stored in memory for subsequent use. Whenever the memory is used for storing values, it is necessary to assign a unique name to each such area in memory. If the arbitrary name Number is used to refer to the area in memory in which a particular value is stored, then Number can be called a variable. Variable names are used in a program in much the same way as they are in ordinary algebra.

In order to appreciate the significance of variables, consider for a moment the following instruction:

display 20+30;

When this instruction is executed, the sun of 20 and 30 is calculated and displayed. Once the sum is printed out, it is lost from the computer's memory. Should the need arise to evaluate the same expression again, both computer time and human effort would be wasted because the same calculation would have to be worked on again, a better approach is to store the result in the computer's memory, from which it can be recalled as often as needed.

Variables are of two types:

  • Alphanumeric
  • Numeric

Alphanumeric variables store alphanumeric data while numeric variables store numeric data

Declaring Variables

Each variable used in a program must be declared. That is to say, the program must contain a statement specifying precisely what kind of information the variable will contain. This applies to every variable in the program, regardless of the type.

To use the variable number for storing an integer value, the variable number must be declared and it should be of the type integer.

A typical way of variable declaration is:

{data type}{name of variable}

Examples:

Char Name;

This statement indicates declaration of an alphanumeric variable called Name.

Integer Quantity;

This statement declares an integer variable called Quantity, which can hold whole numbers only.

Float Salary;

This statement declares a float variable called Salary, which can hold fractions.

Variable Naming Conventions

The name of a variable needs to be meaningful, short, and should not contain any embedded space or symbols like ? ! @ # $ % ^ & * () [ ] { } . , ; : ` ' / \. However, underscores can be used wherever a space is required; for example, Basic_Salary.

No two variables should have the same name; for example, to accept four numbers, one variable name cannot be used, four different variable names need to be used.

A variable name must begin with an alphabet, which may be followed by a sequence of alphabets or digits (0-9).

Keywords cannot be used as variable names. For example, you cannot declare a variable called display.

Variable naming conventions vary depending on different programming languages

Assigning values to variables

Values can be assigned to variables in two ways:

  • At the time of declaration
  • Anywhere in the program after declaration

Example:

integer Salary = 5000;

Here the numeric constant 5000 is assigned to the integer variable Salary.

integer Number;
Number = 803274;

Here the value 80374 is assigned to the variable Number.

The symbol =, also known as the assignment operator, is used for assigning values to variables.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)