System类
Java中的System类主要用于输入输出,与获取一些系统信息与进行一些系统操作,下面给出API中对system类的解释:
系统类提供的工具包括标准输入、标准输出和错误输出流;访问外部定义的属性和环境变量;加载文件和库的方法;以及快速复制数组一部分的实用方法
System类的构造方法为private,且所有办法都为static,所以在使用的时候不可操作System的实例,直接用System进行操作即可。
System类的常用办法:
1.public static long currentTimeMillis()---------用于获取系统当前毫秒值
该办法会获取当前系统时间与1970年01月01日00:00点之前的毫秒差值,通过在程序起始与结束两次使用该办法,可计算出程序运行时间,代码如下:
public class SystemTest{
public static void main(String args[]){
long start = System.currentTimeMillis();
for(int i = 1; i<100; i++)
long end = System.currentTimeMillis();
System.out.println("程序运行时间为[%d]毫秒",(end-start));
}
}
2.public staitc void exit(int status)--------结束正在运行的Java程序
一般情况下参数传入0,异常情况下传入其他数字,下面给出示例代码:
public class SystemTest{
public static void main(String args[]){
for(int i = 1; i<100; i++){
System.out.println(i);
if(i==4)System.exit(0);
}
}
/*
输出结果为:
1
2
3
4
*/
3.public static notive void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)--------拷贝数组
src为被拷贝数组,srcPos为被拷贝数组起始下标,dest为拷贝数组,destPos为拷贝数组下标,length为拷贝长度,下面给出示例代码:
public class SystemTest{
public static void main(String args[]){
int[] src={1,2,3,4,5};
int[] dest={0,0,0,0};
System.arraycopy(src,1,dest,2,2);
System.out.println(Array.toString(dest));
}
}
/*
输出结果为:
[0,0,2,3]
*/
Integer类
Java中针对几个基本数据类型,都有与之对应的包装类,通过将基本数据类型包装,可以对其进行更加方便的操作,其中Integer就是与int对应的包装类。
Integer类的构造办法为:Integer(int src)与Integer(String src),可通过intValue()的操作返回int值,如:
Integer a = new Integer(100);
String src = "100";
Integer b = new Integer(src);
int c = a.intValue();
Integer类还具有自动拆箱与自动装箱的功能,例如:
integer a = 100;//自动装箱
int b = 3;
int c = a + b; //自动拆箱
下面介绍其常用办法。
1.ParseInt(String s,int radix)------将数字字符串转换为int
radix规定的是转换后的数字进制,若不传入radix,则默认为十进制,下面给出示例代码。
public class IntegerTest{
public static void main(String args[]){
String a = "10";
String b="C';
String c="123";
int a1 = Integer.ParseInt(a,2);
int b1 = Integer.ParseInt(b,16);
int c1 = Integer.ParseInt(c);
System.out.println(a1);
System.out.println(b1);
System.out.println(c1);
}
}
/*
输出结果为:
2
13
123
*/
2.toString(int a,int b)------将int转换为字符串
a为被转换int,b为转化后的进制。
若不传入b,则默认按十进制转换。
按十进制转化的另一种办法就是+"",下面给出示例代码。
public class IntegerTest{
public static void main(String args[]){
int i = 9;
//转化为二进制
String i1 = Integer.toString(i,2);
//按十进制转化的第一种办法
String a = Integer.toString(i);
//第二种办法
String b = i+"";
System.out.println(i1);
System.out.println(a);
System.out.println(b);
}
}
/*
输出结果为:
1001
9
9
*/
3.toBinaryString(src)------将十进制数转化为二进制
还有toOctalString(src)和toHexString(src)对应地将十进制数转化为八进制与十六进制,返回的数据类型为字符串,下面给出示例代码。
public class IntegerTest{
public static void main(String args[]){
int i = 9;
//转化为二进制
String b = Integer.toBinaryString(i);
//转化为八进制
String o = Integer.toOctalString(i);
//转化为十六进制
String h = Integer.toHexString(i);
System.out.println(b);
System.out.println(o);
System.out.println(h);
}
}
/*
输出结果为:
1001
11
9
*/
Math类
Math类提供了编写程序所需要的数学工具,其常用的办法有:
public class MathTest{
public static void main(String args[]){
/*
Math.sqrt()//计算平方根
Math.cbrt()//计算立方根
Math.pow(a, b)//计算a的b次方
Math.max(a, b);//计算最大值
Math.min(a, b);//计算最小值
Math.abs();//取绝对值
Math.ceil();//舍小数,取偏大的值
Math.floor();//舍小数,取偏小值
Math.round();//四舍五入
Math.random();//取得一个大于或等于0但小于1的随机double数
*/
//sqrt()&cbrt()
System.out.println(Math.sqrt(16)); //4.0返回double值
System.out.println(Math.cbrt(8)); //2.0返回double值
//pow(a,b)
System.out.println(Math.pow(2,2)); //4.0返回double值
//max(a, b)&min(a, b)
System.out.println(Math.max(2,1.0));//2.0有一个为浮点数就返回浮点数
System.out.println(Math.min(2,1));//2
//abs求绝对值
System.out.println(Math.abs(-1)); //1
System.out.println(Math.abs(10.1)); //10.1
//ceil取偏大值
System.out.println(Math.ceil(-10.1)); //-10.0
System.out.println(Math.ceil(10.7)); //11.0
System.out.println(Math.ceil(-0.7)); //-0.0
System.out.println(Math.ceil(0.0)); //0.0
System.out.println(Math.ceil(-0.0)); //-0.0
//floor取偏小值
System.out.println(Math.floor(-10.1)); //-11.0
System.out.println(Math.floor(10.7)); //10.0
System.out.println(Math.floor(-0.7)); //-1.0
System.out.println(Math.floor(0.0)); //0.0
System.out.println(Math.floor(-0.0)); //-0.0
//round四舍五入
System.out.println(Math.round(10.1)); //10
System.out.println(Math.round(10.7)); //11
System.out.println(Math.round(10.5)); //11
System.out.println(Math.round(-10.5)); //-10 负数情况下.5会被舍去
System.out.println(Math.round(-10.51)); //-11
System.out.println(Math.round(-10.6)); //-11
System.out.println(Math.round(-10.2)); //-10
//部分代码转载自:https://www.cnblogs.com/whiteme/p/7234243.html
}
}