Java Reference
In-Depth Information
program first to see the overall picture. The following program computes and prints
the BMI for an individual who is 5 feet 10 inches tall and weighs 195 pounds:
1 public class BMICalculator {
2 public static void main(String[] args) {
3 // declare variables
4 double height;
5 double weight;
6 double bmi;
7
8 // compute BMI
9 height = 70;
10 weight = 195;
11 bmi = weight / (height * height) * 703;
12
13 // print results
14 System.out.println("Current BMI:");
15 System.out.println(bmi);
16 }
17 }
Notice that the program includes blank lines to separate the sections and comments
to indicate what the different parts of the program do. It produces the following output:
Current BMI:
27.976530612244897
Let's now examine the details of this program to understand how variables work.
Before variables can be used in a Java program, they must be declared. The line of
code that declares the variable is known as a variable declaration.
Declaration
A request to set aside a new variable with a given name and type.
Each variable is declared just once. If you declare a variable more than once, you will
get an error message from the Java compiler. Simple variable declarations are of the form
<type> <name>;
as in the three declarations at the beginning of our sample program:
double height;
double weight;
double bmi;
 
Search WWH ::




Custom Search