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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments