Basic Java Variables
By Stephen Bucaro
A computer program is a set of instructions that tell the computer what to do. each instruction
is called a statement. In Java, as in other programming languages, information that can
be changed as the program runs is stored in variables. A variable is created with a
statement that has the type and the name of the variable.
int a = 43;
A variable statement can also include the value of the information being stored. In the Java code
shown above, a variable of type int named a is being initialized to the value 43.
There are four main types of variables in Java; integers, floating point numbers, characters, and Strings.
Integer
int smlAmt = 24;
The integer type, designated by the keyword int is used to store numbers that
do not have a decimal point. It can be used to hold any integer from -2,147,483,648 to 2,147,483,647.
Floating Point
float bigAmt = 487.34;
The floating point type, designated by the keyword float, is used to store numbers
that contain a decimal point. It can used to hold any number from 4E-45 to 3.4028235E38.
Character
char bigC = 'C';
The character type, designated by the keyword char, is used to store a single character.
A character may be a single letter, a punctuation mark, or a symbol. To designate char type data,
it must be contained within single-quotes as shown above.
String
String slogan = "Now is the time";
The String type, designated by the keyword String, is used to store a sequence or
"string" of characters. Note that whereas the keywords for the other variable types start with a lower-case
letter, the String keyword starts with an upper-case S. To designate String type data, it must be contained
within double-quotes as shown above.
When you declare a String type variable, you are actually creating a String object which provides
many properties and methods that can be use to operate on the string. For example the statement
below would use the String object property length to place the length (in characters) of the String
named slogan in the variable len.
var len = slogan.length();
The statement below would use the String object method toLowerCase to place the String "now is the time"
in the variable newSlogan.
String newSlogan = slogan.toLowerCase();
Java Strings are "immutable", meaning they can't be changed. So any operation on a String that changes
a String actually creates a new String.
Boolean
The boolean type can store only the keyword true or the keyword false. Booleans are
used to store the result of test statements.
int a = 4;
int b = 5;
boolean result;
result = a < b; // true
result = a > b; // false
Naming Varaibles
In Java variable names can begin with a letter, underscore, or dollar sign. The rest of the name can be any
letters or numbers except punctuation marks or spaces. Also, remember that Java is case-sensitive, so when you
use upper-case or lower-case characters in a variable name, you must use that same arrangement of upper-case
and lower-case characters wherever you refer to that variable in your code.
Reserved words are words that are used by the language for special purposes. Java has 53 reserved words,
including: break, catch, continue, final, native, package, private, super, throw, transient, and many more.
Reserved words cannot be used for variable names.
|