您的位置:首页 > 编程语言 > Java开发

Java类库1(16.7.12)

2016-07-17 21:41 369 查看
目录
总结

Java基础类库
System系统类

Runtime运行时类

String字符串类

StringBuffer类

StringBuilder类

Random类

BigDecimal类

目录

总结

上上次课讲到了Java的基础类库,内容不算多Runtime运行时类和String字符串类,由于这几天没网络所以课后总结这么久才写。还有上次课由于有事情所以没来上课,不过讲的内容不多也就是几个类而已所以课后查阅知识点顺便总结出来。

Java基础类库

System系统类

System类可以获取系统属性,获取系统的环境变量,获取系统的时间等,不能创建System类的对象。标准输入输出就不讲了。


public static void exit(int status)  //结束当前运行程序
public static Properties getProperties()//获得系统全部属性
public static long currentTimeMillis()//获得当前日期和时间
public static String getProperty(String key)//获得指定系统属性


Runtime运行时类

Runtime类代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例。


Runtime runtime=Runtime.getRuntime();//获得与当前应用程序相联系的运行时环境
runtime.freeMemory());//获得系统内存剩余空间的大小
runtime.totalMemory());////获得系统内存空间总量
runtime.maxMemory());//获得系统最大内存数


String字符串类

//字符串截取
String t="ssaf,sdf,dsf,sadf33";
String cutStr=t.substring(t.indexOf(",")+1,
t.lastIndexOf(","));
System.out.println(cutStr);
//字符串替换replace()
String originalStr="abcwwwxcfd";
String newString=originalStr.replace("abc", "fdsf");
System.out.println(newString);
//去掉起始和结尾的空格
String hhString="  dsafd  ";
System.out.println(hhString.trim());


StringBuffer类

字符序列可变的字符串,线程安全。StringBuffer类中的内容本身是允许改变的,StringBuffer主要用于频繁修改字符串内容的地方。


public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 100; i++) {
buf.append(i);
}
System.out.println(buf);
}


在StringBuffer类中也定义了许多String类没有的操作:
在指定位置插入内容:insert()
替换指定范围的内容:public StringBuffer replace(int start,int end,String str)
删除指定范围的内容:public StringBuffer delete(int start,int end)


StringBuilder类

线程不安全,效率高
StringBuilder.Append 将信息追加到当前 StringBuilder 的结尾。   StringBuilder.AppendFormat 用带格式文本替换字符串中传递的格式说明符。
StringBuilder.Insert将字符串或对象插入到当前StringBuilder对象的指定索引处。
StringBuilder.Remove 从当前 StringBuilder 对象中移除指定数量的字符。  StringBuilder.Replace 替换指定索引处的指定字符。




Random类

用于得到随机数的类


//得到一个2位的随机数
public static void main(String[] args) {
Random random = new Random();
int rand = random.nextInt(90)+10;
System.out.println(rand);
}


BigDecimal类

用于精确计算,防止精确丢失。通过BigDecimal类有一个最大的好处是可以进行指定位数的四舍五入操作。


import java.math.BigDecimal;

class NewMath {
public static double round(double value, int scale) {
BigDecimal bigdecimal = new BigDecimal(value);
return bigdecimal .divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}

public class BigDecimalTest{
public static void main(String[] args) {
System.out.println(MyMath.round(75.456, 2));
System.out.println(MyMath.round(90.45567, 3));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: