+ 7
Sorry, who can explain me what means math pow in Java?
I'm a freshman
5 Answers
+ 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