constants and variables

By: aathishankaran Emailed: 1697 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

 

v     Constants

 

v     Variables

 

Constants

 

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

 

  1. Alphanumeric
  2. Numeric

 

Alphanumeric constants

 

Comprise 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:

 

q       Integer: Whole numbers. Example: 45,99

q       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:

 

  1. Alphanumeric
  2. 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:

 

q       Char Name;

This statement indicates declaration of an alphanumeric variable called Name.

 

q       Integer Quantity;

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

 

q       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

 

Assinging 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.

 

 


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
FileReader and FileWriter example program in Java
How to use ArrayList in Java
XML and Java - Parsing XML using Java Tutorial
InetAddress Example program in Java
Recursion in java
Connect to a password protected URL from Java program
The Basic Structure of a Simple Java program
right shift operator, >>, in Java
Read from a COM port using Java program
Vector example in Java
append() in Java
concat(), replace(), and trim() Strings in Java
Student marks calculation program in Java
Stack example in Java - push(), pop(), empty(), search()
Most Emailed Articles (in Java)
How to use ArrayList in Java
Read from a COM port using Java program
Data Types in Java
Write to a COM port using Java program
How to access instance from an inner class and accessing outer class variables in java
Method Overloading (function overloading) in Java
Sample program to demonstrate the use of ActionListener
Tomcat and httpd configured in port 8080 and 80
Save/Write/Read image file from/to a database using Java program
Java program for changeable wrapper class
History of Java
Report Sections in JasperReports
Using jar utility in Java - A jar tutorial
FilenameFilter - sample program in Java
Observable class and the sample program in Java