Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java - Math Class
Java Math Class
The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Math Class Declaration
Following is the declaration for java.lang.Math class −
public final class Math extends Object
Java Math Class Fields
Following are the fields for java.lang.Math class −
static double E − This is the double value that is closer than any other to e, the base of the natural logarithms.
static double PI − This is the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
Java Math Class Methods
Sr.No. | Method & Description |
---|---|
1 | static double abs(double a)
This method returns the absolute value of a double value. |
2 | static float abs(float a)
This method returns the absolute value of a float value. |
3 | static int abs(int a)
This method returns the absolute value of an int value. |
4 | static long abs(long a)
This method returns the absolute value of a long value. |
5 | static double acos(double a)
This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. |
6 | static double asin(double a)
This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2. |
7 | static double atan(double a)
This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. |
8 | static double atan2(double y, double x)
This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). |
9 | static double cbrt(double a)
This method returns the cube root of a double value. |
10 | static double ceil(double a)
This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. |
11 | static double copySign(double magnitude, double sign)
This method returns the first floating-point argument with the sign of the second floating-point argument. |
12 | static float copySign(float magnitude, float sign)
This method returns the first floating-point argument with the sign of the second floating-point argument. |
13 | static double cos(double a)
This method returns the trigonometric cosine of an angle. |
14 | static double cosh(double x)
This method returns the hyperbolic cosine of a double value. |
15 | static double exp(double a)
This method returns Euler's number e raised to the power of a double value. |
16 | static double expm1(double x)
This method returns ex -1. |
17 | static double floor(double a)
This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. |
18 | static int getExponent(double d)
This method returns the unbiased exponent used in the representation of a double. |
19 | static int getExponent(float f)
This method returns the unbiased exponent used in the representation of a float. |
20 | static double hypot(double x, double y)
This method returns sqrt(x2 +y2) without intermediate overflow or underflow. |
21 | static double IEEEremainder(double f1, double f2)
This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. |
22 | static double log(double a)
This method returns the natural logarithm (base e) of a double value. |
23 | static double log10(double a)
This method returns the base 10 logarithm of a double value. |
24 | static double log1p(double x)
This method returns the natural logarithm of the sum of the argument and 1. |
25 | static double max(double a, double b)
This method returns the greater of two double values. |
26 | static float max(float a, float b)
This method returns the greater of two float values. |
27 | static int max(int a, int b)
This method returns the greater of two int values. |
28 | static long max(long a, long b)
This method returns the greater of two long values. |
29 | static double min(double a, double b)
This method returns the smaller of two double values. |
30 | static float min(float a, float b)
This method returns the smaller of two float values. |
31 | static int min(int a, int b)
This method returns the smaller of two int values. |
32 | static long min(long a, long b)
This method returns the smaller of two long values. |
33 | static double nextAfter(double start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument. |
34 | static float nextAfter(float start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument. |
35 | static double nextUp(double d)
This method returns the floating-point value adjacent to d in the direction of positive infinity. |
36 | static float nextUp(float f)
This method returns the floating-point value adjacent to f in the direction of positive infinity. |
37 | static double pow(double a, double b)
This method returns the value of the first argument raised to the power of the second argument. |
38 | static double random()
This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. |
39 | static double rint(double a)
This method returns the double value that is closest in value to the argument and is equal to a mathematical integer. |
40 | static long round(double a)
This method returns the closest long to the argument. |
41 | static int round(float a)
This method returns the closest int to the argument. |
42 | static double scalb(double d, int scaleFactor)
This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. |
43 | static float scalb(float f, int scaleFactor)
This method return f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. |
44 | static double signum(double d)
This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. |
45 | static float signum(float f)
This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero. |
46 | static double sin(double a)
This method returns the hyperbolic sine of a double value. |
47 | static double sinh(double x)
This method Returns the hyperbolic sine of a double value. |
48 | static double sqrt(double a)
This method returns the correctly rounded positive square root of a double value. |
49 | static double tan(double a)
This method returns the trigonometric tangent of an angle.r |
50 | static double tanh(double x)
This method returns the hyperbolic tangent of a double value. |
51 | static double toDegrees(double angrad)
This method converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
52 | static double toRadians(double angdeg)
This method converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
53 | static double ulp(double d)
This method returns the size of an ulp of the argument. |
54 | static double ulp(float f)
This method returns the size of an ulp of the argument. |
Methods Inherited
This class inherits methods from the following classes −
- java.lang.Object
Java Math Class Example
The following example shows the usage of some important methods provided by Math class.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two double numbers double x = 60984.1; double y = -497.99; // get the natural logarithm for x System.out.println("Math.log(" + x + ")=" + Math.log(x)); // get the natural logarithm for y System.out.println("Math.log(" + y + ")=" + Math.log(y)); // get the max value System.out.println("Math.max(" + x + ", y" + ")=" + Math.max(x,y)); // get the min value System.out.println("Math.min(" + x + ", y" + ")=" + Math.min(x,y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.log(60984.1)=11.018368453441132 Math.log(-497.99)=NaN Math.max(60984.1, y)=60984.1 Math.min(60984.1, y)=-497.99