JavaScript Variables
JavaScript Variables
As with algebra, JavaScript variables are used to hold values or expressions.A variable can have a
short name, like x, or a more descriptive name, like carname.
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character
Example
A variable's value can change during the execution of a script. You can refer to a variable by its name
to display or change its value.
var x;
var carname;
After the declaration shown above, the variables are empty (they have no values yet).
However, you can also assign values to the variables when you declare them:
var x=5;
var carname="Volvo";
After the execution of the statements above, the variable x will hold the value 5, and carname will
hold the value Volvo.
Note: When you assign a text value to a variable, use quotes around the value.
Variables
Variables are places in memory to store values. There are different kinds of variables, and every language
offers slightly different characteristics.
Name.
Data Type specifies the kinds of data a variable an store. Java has two general kinds of data types.
8 basic or primitive types (byte, short, int, long, float, double, char, boolean).
An unlimited number of object types (String, Color, JButton, ...). Java object variables hold a
reference (pointer) to the the object, not the object, which is always stored on the heap.
Scope of a variable is who can see it. The scope of a variable is related program structure: eg, block,
method, class, package, child class.
Lifetime is the interval between the creation and destruction of a variable. The following is basically how
things work in Java. Local variables and parameters are created when a method is entered and destroyed
when the method returns. Instance variables are created by new and destroyed when there are no more
references to them. Class (static) variables are created when the class is loaded and destroyed when the
program terminates.
Initial Value. What value does a variable have when it is created? There are several possibilites.
1. No initial value. Java local variables have no initial value, however Java compilers perform a simple
flow analysis to ensure that every local variable is assigned a value before it is used. These error
messages are usually correct, but the analysis is simple-minded, so sometimes you will have to
assign an initial value even tho you know that it isn't necessary.
2. User specified initial value. Java allows an assignment of intitial values in the declaration of a
variable.
3. Instance and static variables are given default initial values: zero for numbers, null for objects,
and false for booleans.
Declarations are required. Java, like many languages, requires you to declare variables -- tell the
compiler the data type, etc. Declarations are good because they help the programmer build more reliable
and efficient programs.
Declarations allow the compiler to find places where variables are misused, eg, parameters of the
wrong type. What is especially good is that these errors are detected at compile time. Bugs that
make it past the compiler are harder to find, and may not be discovered until the program has been
released to customers. This fits the fail early, fail often philosophy.
A declaration is also the perfect place to write comments describing the variable and how it is used.
Because declarations give the compiler more information, it can generate better code.
Java Variable Types
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is
shown here:
The type is one of Java's datatypes. The identifier is the name of the variable. To declare more than one
variable of the specified type, use a comma-separated list.
Here are several examples of variable declarations of various types. Note that some include an initialization.
This chapter will explain varioys variable types available in Java Language. There are three kinds of
variables in Java:
1. Local variables
2. Instance variables
3. Class/static variables
Local variables :
Local variables are created when the method, constructor or block is entered and the variable will
be destroyed once it exits the method, constructor or block.
Local variables are visible only within the declared method, constructor or block.
There is no default value for local variables so local variables should be declared and an initial value
should be assigned before the first use.
Example:
Here age is a local variable. This is defined inside pupAge() method and its scope is limited to this method
only.
public class Test{
public void pupAge(){
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age)
}
Example:
Following example uses age without initializing it, so it would give an error at the time of compilation.
Instance variables :
Instance variables are declared in a class, but outside a method, constructor or any block.
When a space is allocated for an object in the heap a slot for each instance variable value is
created.
Instance variables are created when an object is created with the use of the key word 'new' and
destroyed when the object is destroyed.
Instance variables hold values that must be referenced by more than one method, constructor or
block, or essential parts of an object.s state that must be present through out the class.
The instance variables are visible for all methods, constructors and block in the class. Normally it is
recommended to make these variables private (access level).However visibility for subclasses can
be given for these variables with the use of access modifiers.
Instance variables have default values. For numbers the default value is 0, for Booleans it is false
and for object references it is null. Values can be assigned during the declaration or within the
constructor.
Instance variables can be accessed directly by calling the variable name inside the class. However
within static methods and different class ( when instance variables are given accessibility) the
should be called using the fully qualified name . ObjectReference.VariableName.
Example:
import java.io.*;
class Employee{
// this instance variable is visible for any child class.
public String name;
Class/static variables :
Class variables also known as static variables are declared with the static keyword in a class, but
outside a method, constructor or a block.
There would only be one copy of each class variable per class, regardless of how many objects are
created from it.
Static variables are rarely used other than being declared as constants. Constants are variables that
are declared as public/private, final and static. Constant variables never change from their initial
value.
Static variables are stored in static memory. It is rare to use static variables other than declared
final and used as either public or private constants.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since
they must be available for users of the class.
Default values are same as instance variables. For numbers the default value is 0, for Booleans it is
false and for object references it is null. Values can be assigned during the declaration or within the
constructor. Additionally values can be assigned in special static initializer blocks.
Static variables can be accessed by calling with the class name . ClassName.VariableName.
When declaring class variables as public static final, then variables names (constants) are all in
upper case. If the static variables are not public and final the naming syntax is the same as instance
and local variables.
Example:
import java.io.*;
class Employee{
// salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development";
In this section, you will learn about Java variables. A variable refers to the memory location
that holds values like: numbers, texts etc. in the computer memory. A variable is a name of
location where the data is stored when a program executes.
The Java contains the following types of variables:
JAVASCRIPT
INTRODUCTION TO JAVASCRIPT 51-57
PROGRAMME TO CREATE A SERIES 1-10 58
PROGRAMME TO CREATE A TABLE OF 7 59
PROGRAMME TO CREATE A BUTTON 60-62
PROGRAMME TO CREATE A STAR 63
MY WEBSITE 64-78
QUIZ PROGRAMME 79-83