Variable Types
| Posted in Java , Variable Types | Posted on 10:50 PM
A variable is a data container that well, varies. You have to define it and then indicate what type of data it will contain. int, double, and char are the first examples in the book.
int x; //declare integer variable
/* Occupies 32 bits or 4 bytes, which is: -231 to 231-1 or -2,147,483,648 to 2,147,483,647. */
double x; //declare double variable
/* Occupies 64 bits or 8 bytes, with 14 or 15 significant digits. */
char a; //character variable
/* Occupies 16 bits or 2 bytes, stores a - z, A - Z, 0 - 9, as well as +, - , etc. */
Several variables can be declared in a single statement.
int var1, var2, var3; // three variables are declared in one statement
Variables can also be given a value as they are declared.
int var1=3; //variable declared and given value of 3
int var2=var1+3; //variable declared and given value of an expression
Division of two integers yields an integer. The book gives the example of 5/9. The integer value of this would be 0. 5.0/9 forces a float, double, or something a little bit more accurate.
It's 11:11 pm. Goodnight for now!
Comments Posted (0)
Post a Comment