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: 512 The 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).
Data Types
The following is a list of Java’s primitive data types: Data Type
Description
int
Integer – 32bit ranging from -2,147,483,648 to 2,147,483,648
byte
8-bit integer ranging from -128 to 127
short
16-bit integer ranging from -32,768 to 32,768
long
64-bit integer from -9,223,372,036,854,775,808 to -9,223,372,036,854,775,808
float
Single-precision floating point, 32-bit
double
Double-precision floating point, 64-bit
char
Character , 16-bit unsigned ranging from 0 to 65,536 (Unicode)
boolean
Can be true or false only
Commentaires