+ 7
Sorry, who can explain me what means math pow in Java?
I'm a freshman
5 Respuestas
+ 9
Thanks very much !!!
+ 6
pow(x, y) is x multiplied by itself y times.
pow(2,3) is 2 * 2 * 2 = 8
pow(2,4) is 2 * 2 * 2 = 16
etc.
или просто: возведение в степень
+ 6
Other notation you may be familiar with, using @abe's example:
2^3 == 2³ == 2*2*2 = 8
In words: Two to the third power.
+ 3
Power of, it's a math class.
Java doesn't provide the power operator. (For example, “^” in 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 2 classes, T1, T2.
“T1” is the main class for this file. Save the file as T1.java.
The “T2” class defines one method, the “square”. It takes a “integer” and returns a decimal number of type “double”. (“double” basically means a large decimal number.)
In the main class “T1”, the line:
T2 x1 = new T2();
It means: x1 is a variable, its type is T2. The value of x1 is a new instance of T2.
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.
+ 3
a method which returns the value of the first argument raised to the power of the second argument.
Math.pow( 2, 3) == 2*2*2 = 8.0
it returns a double