#5 JAVA variables
Variables and Data Types
Variables
A variable is a place where the program stores data temporarily. As the name implies the value stored in such a location can be changed while a program is executing (compare with constant).
class Example2
{
public static void main(String args[])
{
int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
System.out.println("var1 contains " + var1);
var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2);
}
}
Predicted Output:
var2 contains var1 / 2: 512The above program uses two variables, var1 and var2. var1 is assigned a value directly while var2 is filled up with the result of dividing var1 by 2, i.e. var2 = var1/2. The words int refer to a particular data type, i.e. integer (whole numbers).
Comments
Post a Comment