Java Number & Math
Java Number
byteintlongdouble
int a = 5000;
float b = 13.65f;
byte c = 0x4a;
Java
IntegerLongByteDoubleFloatShort Number
| Byte | byte | |
| Short | short | |
| Integer | int | |
| Long | long | |
| Float | float | |
| Double | double | |
| BigInteger | - | |
| BigDecimal | - |
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();
}
}
//
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
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
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"));
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"
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()
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
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;
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
Math.IEEEremainder(10, 3); // IEEE 1.0
4.
Math.PI; // 3.141592653589793
Math.E; // e 2.718281828459045
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
x
502***[email protected]
/** * @author Dale * java * */ public class Number { public static void main(String[] args) { /** Integer i1 = 128; // Integer.valueOf(128); int t = i1; // i1.intValue() System.out.println(t); */ /** –128127127, ,new */ Integer i1 = 200; Integer i2 = 200; /** == equals == equals */ if(i1==i2) { System.out.println("true"); } else { System.out.println("false"); } } }x
502***[email protected]
estivalwind
fan***[email protected]
1Java -128 ~ 127 -128 ~ 127
2 Integer -128 ~ 127 ,
estivalwind
fan***[email protected]
flaming
248***[email protected]
Java int Integer
1. int int Integer Integer
2.
new
3.
int Integer
Integer Integer int
4. new Integer new Integer() false
/** * newIntegernewInteger */ public class Test { public static void main(String[] args) { Integer i= new Integer(200); Integer j = 200; System.out.print(i == j); //false } }new Integer java new Integer() false
5. new Integer [-128,127] true false
/** * newInteger */ public class Test { public static void main(String[] args) { Integer i1 = 127; Integer ji = 127; System.out.println(i1 == ji);//true Integer i2 = 128; Integer j2 = 128; System.out.println(i2 == j2);//false } }java Integer i1 = 127 Integer i1 = Integer.valueOf(127)
6. Integer ( new ) int true
/** * Integerint */ public class Test { public static void main(String[] args) { Integer i1 = 200; Integer i2 = new Integer(200); int j = 200; System.out.println(i1 == j);//true System.out.println(i2 == j);//true } }Integer int Integer int int true
flaming
248***[email protected]
yuki
897***[email protected]
Math.floor
Math.ceil
Math.round
yuki
897***[email protected]
JavaNewer
529***[email protected]
byteintlongdouble java
IntegerLongByteDoubleFloatShort Number
JavaNewer
529***[email protected]
inytialf
iny***[email protected]
floor()ceil()
1. floor
2. ceilceiling
floor()ceil()double
round()y
.5
.5
inytialf
iny***[email protected]