Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Variables
-
All variables have type.
-
Declare by placing the type first, then the variable name.
-
You can initialize a variable at the same time:
-
Or simply declare it:
-
Either way, though, you must end the statement with a semicolon, as you
should with any Java statement.
-
To name a variable, follow these rules:
-
Start with a letter.
-
The following characters can be letters, numbers, or the underscore.
-
No other special characters are permitted. Nor is whitespace.
-
It is common to start with a lowercase letter, though this is not required.
-
Java defines certain reserved rwords that cannot be used as variable names.
-
You can declare more than one variable at once:
-
boolean hasGrades, paidInFull;
-
This delcares hasGrades and paidInFull as boolean type variables.
Note the placement of the comma and the semicolon.
-
When declaring variables for objects, it is common to have a variable name
that is similar to the object name. Again, this is not required.
-
Car car;
-
Car myCar;
-
Car userCar;
Assignments and Initializations
Created by: Brandan
Jones January 4, 2002