Java Reference
In-Depth Information
table 2-4: Java Keywords
abstract
continue
for
new
switch
assert
default
goto
package
synchronized
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp
volatile
const
float
native
super
while
variables
As stated earlier, every class definition consists of both variables and methods. A variable is a name for a
memory location that stores a specific value. This value may change during program execution, which is
why it's called a “variable.” The BMICalculator class starts by defining the following variables:
// declare variables
double weight;
double height;
double BMI;
The weight , height , and BMI variables are defined using the data type double, which represents a
floating point number. Other data types exist in Java and will be covered in a subsequent section. In
Java, variables must always be defined in a class.
methods
As discussed earlier, a method is a piece of code within a class definition, and it performs a specific
kind of functionality. Just as with a class, every method definition is enclosed within brackets {...} .
In the BMICalculator example, three methods have been deined— BMICalculator , CalculateBMI,
and main . Consider the main method:
public static void main(String[] args) {
BMICalculator calculator = new BMICalculator(60, 1.70);
double bmi = calculator.calculateBMI();
// print BMI to screen
System.out.println("Your BMI is " + bmi + ".");
}
 
Search WWH ::




Custom Search