- A Simple Java Program
- Comments
- Data Types
- Variables
- Assignments and Initializations
- Operators
- Strings
- Control Flow
- Big Numbers
- Arrays
Assignments and Initializations
After you declare a variable, you must explicitly initialize it by means of an assignment statement—you can never use the values of uninitialized variables.
You assign to a previously declared variable using the variable name on the left, an equal sign (=), and then some Java expression that has an appropriate value on the right.
int vacationDays; // this is a declaration vacationDays = 12; // this is an assignment
Here's an example of an assignment to a character variable:
char yesChar; yesChar = 'Y';
One nice feature of Java is the ability to both declare and initialize a variable on the same line. For example:
int vacationDays = 12; // this is an initialization
Finally, in Java you can put declarations anywhere in your code. For example, the following is valid code in Java:
double salary = 65000.0; System.out.println(salary); int vacationDays = 12; // ok to declare variable here
Of course, you cannot declare two variables with the same name in the same scope.
C and C++ distinguish between the declaration and definition of variables. For example,
int i = 10;
is a definition, whereas
extern int i;
is a declaration. In Java, there are no declarations that are separate from definitions.
C and C++ distinguish between the declaration and definition of variables. For example,
int i = 10;
is a definition, whereas
extern int i;
is a declaration. In Java, there are no declarations that are separate from definitions.
Constants
In Java, you use the keyword final to denote a constant. For example,
public class Constants { public static void main(String[] args) { final double CM_PER_INCH = 2.54; double paperWidth = 8.5; double paperHeight = 11; System.out.println("Paper size in centimeter: " + paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH); } }
The keyword final indicates that you can assign to the variable once, then its value is set once and for all. It is customary to name constants in all upper case.
It is probably more common in Java to want a constant that is available to multiple methods inside a single class. These are usually called class constants. You set up a class constant with the keywords static final. Here is an example of using a class constant:
public class Constants2 { public static final double CM_PER_INCH = 2.54;; public static void main(String[] args) { double paperWidth = 8.5; double paperHeight = 11; System.out.println("Paper size in centimeter: " + paperWidth * CM_PER_INCH + " by " + paperHeight * CM_PER_INCH); } }
Note that the definition of the class constant appears outside the main method. Thus, the constant can also be used in other methods of the same class. Furthermore, if (as in our example) the constant is declared public, methods of other classes can also use the constant—in our example, as Constants2.CM_PER_INCH.
const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.
const is a reserved Java keyword, but it is not currently used for anything. You must use final for a constant.