Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Math class in Java programming

Shulou Source: shulou.com Published: 2022-05-31 10:35:51 10月03日 Update

In this article Xiaobian introduces in detail "how to use the Math class in Java programming", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the Math class in Java programming" can help you solve your doubts.

1. Mathclass

1. The class contains basic mathematical operation methods, such as addition, subtraction, multiplication, division, remainder, etc., they can not carry out complex operations, in the Math class to find the absolute value, square root, trigonometric function, etc., all classes in the Math class are static and can be called with its class name.

two。 Static constant

The Math class contains two static constants, PI and E, which represent mathematical pi (pi) and e (natural logarithm), respectively.

3. Static constant example

Public class p61 {public static void main (String [] args) {/ / TODO Auto-generated method stub System.out.println ("PI its constant value:" + Math.PI); System.out.println ("E its constant value:" + Math.E);}}

The result of the operation is:

2. Absolute value of Mathth class

1. The characteristic of an absolute value is a positive number, the absolute value of zero is itself, and the absolute value of a negative number is its opposite.

Method for the absolute value of the 2.Math class:

Public static int abs (int a) method: returns the absolute value of the int type value

Public static float abs (float a) method: returns the absolute value of the float type value

Public static double abs (double a) method: returns the absolute value of the double type value

Public static long abs (long a) method: returns the absolute value of the long type value

Example of the absolute value method of the 3.Math class:

Import java.util.Scanner; public class p67 {public static void main (String [] args) {/ / TODO Auto-generated method stub System.out.println ("Please enter an integer"); Scanner scan=new Scanner (System.in); int a=scan.nextInt (); if (a > 0) {System.out.println ("positive absolute value is itself") System.out.println (Math.abs (a));} else if (a)

Tags: Method absolute value type two maximum minimum maximum constant example result static run programming integer article input content mathematics positive number opposite number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB OPPO Reno Docker Huawei