Tuesday, February 25, 2014

Java Variables

Java Variables

Variables

The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a scope, which defines their visibility, and a lifetime.

Declaring a Variable In Java 

All variables must be declared before they can be used. The basic form of a variable declaration is shown here:

Syntax

type identifier [ = value];


Sample Code 1

class Example1
{
public static void main(String args[])
{
int a = 10;
System.out.println("Value of variable is: " + a);
}
}

Sample Code 2

class Example2
{
public static void main(String args[])
{
int a, b, sum;
a = 7;
b = 5;
sum = a + b;
System.out.println("Addition of variable is: " + sum);
}
}

No comments:

Post a Comment