Xah Lee, 2005-01
It's interesting that java doesn't provide the power operator. (for example, 3^4). You have to use “java.lang.Math.pow(3,4)”. That method returns type “double”.
import java.lang.Math; class t2 { public double square (int n) { return java.lang.Math.pow(n,2); } } class t1 { public static void main(String[] arg) { t2 x1 = new t2(); double m =x1.square(3); System.out.println(m); } }
In the above example, we defined a class t2 and t1.
“t1” is the main class for this file. The file name thus should be “t1.java”. The t2 is a auxiliary class, that is, a helper class for what we need do in this package.
The t2 class defines one method, the “square”. It takes a “integer” and returns a decimal number of type “double”. (“double” is basically a decimal number normal length the computer can store.)
In the main class t1, the line:
t2 x1 = new t2();
creates a new instance of the class t2, and named it x1.
The line:
double m =x1.square(3);
calls the “square” method of x1, and assign the result to “m”.
In java, All numbers have a type. All method definition must declare a type for each of their parameter, and declare a type for the thing the method returns.