Math主要包括一些常用数学函数和常量
public static final double E:自然常数
public static final double PI:圆周率
public static int abs(int a) :绝对值
public static double ceil(double a) :向上取整
public static double floor(double a):向下取整
public static int max(int a,int b) min自学:取最大值,去最小值
public static double pow(double a,double b):a的b次幂
public static double random():随机数[0.0,1.0]
public static int round(float a) 参数为double的自学: 四舍五入
public static double sqrt(double a):取a的正平方根
package com.company;
public class Main {
public static void main(String[] args)
{
System.out.println("e = "+Math.E);
System.out.println("PI = "+Math.PI);
System.out.println("abs(-1) = "+Math.abs(-1));
System.out.println("abs(1) = "+Math.abs(1));
System.out.println("ceil(3.2) = " + Math.ceil(3.2));
System.out.println("floor(3.9) = "+ Math.floor(3.9));
System.out.println("round(3.5) = " + Math.round(3.5));
System.out.println("round(3.4) = "+Math.round(3.4));
System.out.println("max(2,5) = "+Math.max(2,5));
System.out.println("pow(2,3) = "+Math.pow(2,3));
System.out.println("sqrt(4) = "+Math.sqrt(4));
}
}
e = 2.718281828459045
PI = 3.141592653589793
abs(-1) = 1
abs(1) = 1
ceil(3.2) = 4.0
floor(3.9) = 3.0
round(3.5) = 4
round(3.4) = 3
max(2,5) = 5
pow(2,3) = 8.0
sqrt(4) = 2.0
public static void exit(int status)
用于终止程序,用法System.exit(0),函数的参数表示终止状态,非0表示异常
public static void arraycopy(Object src , int srcPos , Object dest , int destPos , int length)
将src数组从srcPos开始长度为lenth的数据复制到dest从destPos开始长度为lenth的内存里
public static void main(String[] args)
{
int arr [] = new int [10];
for(int i=0;i<10;i++) arr[i]=i;
for(int x:arr) System.out.print(x+" ");
System.out.println(' ');
int b [] =new int [10];
for(int x:arr) System.out.print(x+" ");
System.out.println(' ');
*ystem.arraycopy(arr,0,b,0,8);
for(int x:b) System.out.print(x+" ");
System.out.println(' ');
}
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 0 0
public static long currentTimeMillis()
返回当前时间,单位为毫秒,可以通过这个函数计算程序执行时间
public static void main(String[] args)
{
long t0=System.currentTimeMillis();
long j=100000000;
while (j-->0);
long t1=System.currentTimeMillis();
System.out.println(t1-t0);
}
42
public static long nanoTime()
返回当前时间与纳秒为单位
Integer类在对象中包装基本类型int的值。 Integer类型的对象包含一个类型为int的字段。此外,这个类提供了几种方法,用于将int转换为String,将String转换为int,以及在处理int时有用的其他常量和方法。
MIN_VALUE :int的最大值
MAX_VALUE :int的最小值
TYPE :类型(int)
SIZE : 位数
BYTES :字节数
public Integer(int value) : 构造方法
public Integer(String s) : 构造方法
public boolean equals(Object obj) :比较两个对象是否相等
public int compareTo(Integer anotherInteger) :比较两个数的大小关系
public static int compare(int x, int y) :比较大小
public static int compareUnsigned(int x, int y) : 无符号比较
==integer类与int类型的数是等价的,及integer类可以当作int的使用 ==
public static void main(String[] args)
{
Integer a = new Integer("233");
Integer b = new Integer(232);
Integer c = new Integer(233);
System.out.println(b);
System.out.println(a);
System.out.println("BYTES : "+a.BYTES);
System.out.println("MAX_VALUE : "+a.MAX_VALUE);
System.out.println("MIN_VALUE : "+a.MIN_VALUE);
System.out.println("SIZE : "+a.SIZE);
System.out.println("TYPE : "+a.TYPE);
System.out.println(a.compareTo(b));
System.out.println(a.compareTo(232));
System.out.println(a.compareTo(234));
System.out.println(a.compare(-2,1));
System.out.println(a.compareUnsigned(-2,1));
System.out.println(a.divideUnsigned(4,2));
System.out.println(a.remainderUnsigned(5,2));
System.out.println(a.sum(2,3));
System.out.println(a.max(2,3));
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
232
233
BYTES : 4
MAX_VALUE : 2147483647
MIN_VALUE : -2147483648
SIZE : 32
TYPE : int
1
1
-1
-1
1
2
1
5
3
false
true