Java类库1(16.7.12)

目录

总结

上上次课讲到了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));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值