-
Math MethodsJava 2022. 2. 8. 21:24
자바에서 주로 쓰이는 숫자 메소드들에 대해 정리하였습니다.
< Introduction >
The Java Math class is part of the java.lang package which is available by default; it contains a variety of methods that can be used to perform numerical calculations in our programs.
Every method in the Math class is static. This means that we can call and use these methods without creating an object of the class.
< Calling Static Methods >
There are two main options for calling a static method.
1. Our first option is to append the dot operator to the class name followed by the method we want to execute.
: Math.method( )
class Numbers { public static void main(String[] args) { // Call method using the Class name, the dot operator, the method name, and arguments int smallerNumber = Math.min(3, 10); System.out.println(smallerNumber); // Prints: 3 } }
2. Our second option for calling a static method from the Math class is to import the class by adding import static java.lang.Math.*;
import static java.lang.Math.*; // import Math class class Numbers { public static void main(String[] args) { // Call method by using method name and arguments int smallerNumber = min(3, 10); System.out.println(smallerNumber); // Prints: 3 } }
< int abs(int x) >
Returns the absolute value of an int value
Example)
System.out.println(Math.abs(5)); // Prints: 5 System.out.println(Math.abs(-5)); // Prints: 5
< double abs(double x) >
Returns the absolute value of a double value
Example)
System.out.println(Math.abs(5.0)); // Prints: 5.0 System.out.println(Math.abs(-5.0)); // Prints: 5.0
< double pow(double base, double exponent) >
Returns the value of the first parameter raised to the power of the second parameter.
Example)
double x = Math.pow(5, 3); System.out.println(x); // Prints: 125.0
< double sqrt(double x) >
Returns the positive square root of a double value
Example)
double x = Math.sqrt(49); System.out.println(x); // Prints: 7.0 double y = Math.sqrt(52); System.out.println(y); // Prints: 7.211102550927978
< double random() >
Returns a double value greater than or equal to 0.0 and less than 1.0
Example)
System.out.println(Math.random()); // 0.8592007008856128 System.out.println(Math.random()); // 0.6120058754881421 System.out.println(Math.random()); // 0.48259656765819403
+. if we wanted an int value in the range of minValue up to and including maxValue
: (Math.random() * (maxValue - minValue + 1)) + minValue
// Random int value between 10 and 20 int d = (int)(Math.random() * 11 ) + 10;
: (int) means type conversion!
Reference: https://kephilab.tistory.com/27
3. Java 자바 - 자동 타입 변환, 강제 타입 변환
타입 변환 자바에는 두 종류의 타입 변환이 있다. - 자동 타입 변환 (묵시적) - 강제 타입 변환 (명시적) 1. 자동 타입 변환 프로그램 실행 도중에 자동으로 타입 변환이 일어난다. 작은 크기 가지
kephilab.tistory.com
To see all the methods offered by the Math class, check out the official documentation for the Math class.
Math (Java Platform SE 8 )
Returns the value of the first argument raised to the power of the second argument. Special cases: If the second argument is positive or negative zero, then the result is 1.0. If the second argument is 1.0, then the result is the same as the first argument
docs.oracle.com
'Java' 카테고리의 다른 글
Inheritance and Polymorphism (0) 2022.02.09 Static Variables and Methods (0) 2022.02.09 Access, Encapsulation, and Scope (0) 2022.02.08 String Methods (0) 2022.02.07 Loops (0) 2022.02.07