Java Number & Math

Java Number

byteintlongdouble

int a = 5000; float b = 13.65f; byte c = 0x4a;

Java

IntegerLongByteDoubleFloatShort Number

Bytebyte
Shortshort
Integerint
Longlong
Floatfloat
Doubledouble
BigInteger-
BigDecimal-

Java Number [Java Number]

Number java.lang

Number

public abstract class Number implements Serializable {
    //
    public abstract int intValue();
    public abstract long longValue();
    public abstract float floatValue();
    public abstract double doubleValue();
   
    // Java 8
    public byte byteValue() {
        return (byte)intValue();
    }
    public short shortValue() {
        return (short)intValue();
    }
}

Integer

Test.java

public class Test{ public static void main(String[] args){ Integer x = 5; x = x + 10; System.out.println(x); } }

15

x xxxx

Number num = 1234.56; // Double

System.out.println(num.intValue());    // 1234 ()
System.out.println(num.longValue());   // 1234
System.out.println(num.floatValue());  // 1234.56
System.out.println(num.doubleValue()); // 1234.56

java
Integer x = 10;
Double y = 10.0;

//
System.out.println(x.doubleValue() == y.doubleValue()); // true

BigInteger bigInt = new BigInteger("12345678901234567890");
BigDecimal bigDec = new BigDecimal("1234567890.1234567890");

//
BigInteger sum = bigInt.add(new BigInteger("1"));
BigDecimal product = bigDec.multiply(new BigDecimal("2"));

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);

System.out.println(nf.format(1234.5678)); // "1,234.57"

Java 5+

//
Integer autoBoxed = 42; // Integer.valueOf(42)

//
int autoUnboxed = autoBoxed; // autoBoxed.intValue()

Java Math

Math Java java.lang

Java Math

Math static Math

Test.java

public class Test { public static void main (String []args) { System.out.println("90 " + Math.sin(Math.PI/2)); System.out.println("0" + Math.cos(0)); System.out.println("60" + Math.tan(Math.PI/3)); System.out.println("1 " + Math.atan(1)); System.out.println("/2" + Math.toDegrees(Math.PI/2)); System.out.println(Math.PI); } }

90 1.0
01.0
601.7320508075688767
1 0.7853981633974483
π/290.0
3.141592653589793

1.

Math.exp(1);    // e^1 2.718
Math.log(Math.E); // ln(e) = 1
Math.log10(100); // log10(100) = 2

2.

// [0.0, 1.0)
double random = Math.random();

// [1, 100]
int randomInt = (int)(Math.random() * 100) + 1;

3.

Math.hypot(3, 4); // sqrt(x+y) 5.0
Math.IEEEremainder(10, 3); // IEEE 1.0

4.

Math.PI;    // 3.141592653589793
Math.E;     // e 2.718281828459045

Number & Math

Number & Math

1 xxxValue()
Number xxx
2 compareTo()
number
3 equals()
number
4 valueOf()
Number
5 toString()
6 parseInt()
int
7 abs()
8 ceil()
( >= )
9 floor()
<=
10 rint()
double
11 round()
Math.floor(x+0.5) 0.5 Math.round(11.5) 12Math.round(-11.5) -11
12 min()
13 max()
14 exp()
e
15 log()
16 pow()
17 sqrt()
18 sin()
double
19 cos()
double
20 tan()
double
21 asin()
double
22 acos()
double
23 atan()
double
24 atan2()
25 toDegrees()
26 toRadians()
27 random()

Math floor,round ceil

Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor,round ceil

public class Main { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); } }

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0