Java常用类库初识

String类

直接为String赋值

/*String
直接为String赋值
*/
class  StringDemo01
{
    public static void main(String[] args) 
    {       
        String name = "YuYu";
        System.out.println("姓名:"+name); 
    }
}

第二种String实例化方式

//第二种String实例化方式
class  StringDemo02
{
    public static void main(String[] args) 
    {
        String name = new String("YuYu");
        System.out.println("姓名:"+name);
    }
}

String的内容比较

public class  StringDemo03
{
    public static void main(String[] args) 
    {
        int x = 30;
        int y = 30;
        System.out.println("两个数字的比较结果是:"+(x==y));
        //true
    }
}

使用”==”比较字符串的内容

//使用"=="比较字符串的内容
public class StringDemo04
{
    public static void main(String[] args) 
    {
        String str1 = "hello";
        String str2 = new String("hello");
        String str3 = str2;
        System.out.println("str1==str2 --->" + (str1==str2));//false
        System.out.println("str1==str3--->"+(str1==str3));//false
        System.out.println("str2==str3--->"+(str2==str3));//true
        //==比较的是地址
    }
}

使用equals方法对String的内容进行比较

public class  StringDemo05
{
    public static void main(String[] args) 
    {
        String str1 = "hello";
        String str2 = new String("hello");
        String str3 = str2; //传递引用
        System.out.println("str1==str2 --->" + (str1.equals(str2)));//true
        System.out.println("str1==str3--->"+(str1.equals(str3)));//true
        System.out.println("str2==str3--->"+(str2.equals(str3)));//true
        //equals() 比较的是内容
    }
}

验证一个字符串就是String的匿名对象

public class StringDemo06 
{
    public static void main(String[] args) 
    {
        System.out.println("\"hello\"equals\"hello\"--->"+("hello".equals("hello")));//true
    }
}

采用直接赋值的方式声明多个String对象,并且内容相同,观察地址比较

class  StringDemo07
{
    public static void main(String[] args) 
    {
        String str1="hello";
        String str2="hello";
        String str3="hello";
        System.out.println("str1 == str2--->"+(str1 == str2));//-->true
        System.out.println("str1 == str3--->"+(str1 == str3));//--->true
        System.out.println("str2 == str3--->"+(str2 == str3));//--->true
        //说明这3个字符串都指向堆内存的同一个,这也就是String使用赋值的方式之后,只要是以后声明的字符串内容相同,则不会再开辟新的内存空间
        //在Java中会提供一个字符串池来保存全部内容
    }
}

字符串的内容不可改变

//修改字符串的内容
class  StringDemo09
{
    public static void main(String[] args) 
    {
        String str = "hello";
        str = str +"world";
        System.out.println("str="+str);
    }
}

一个String对象内容的改变实际上是通过内存地址的”断开-连接”变化完成的,而字符串中的内容并没有任何的变化。

StringBuffer类

字符串连接操作

//StringBuffer 
//字符串连接操作
class  StringBufferDemo01
{
    public static void main(String[] args) 
    {
        StringBuffer buf =new StringBuffer();
        buf.append("Hello");
        buf.append("World").append("!!!!");
        buf.append("\n");
        buf.append("数字 = ").append(1).append("\n");//添加数字
        buf.append("字符 = ").append('C').append("\n");//添加字符
        buf.append("布尔 = ").append(true);//添加布尔类型
        System.out.println(buf);

    }
}

验证StringBuffer的内容是可以修改的

//验证StringBuffer的内容是可以修改的
class  StringBufferDemo02
{
    public static void main(String[] args) 
    {
        StringBuffer buf = new StringBuffer();
        buf.append("Hello");
        fun(buf);
        System.out.println(buf);
    }
    public static void fun(StringBuffer s){
        s.append("MLDN").append("XXXX");
    }

}

在任意位置处为StringBuffer添加内容 insert()方法在指定位置添加内容

class   StringBufferDemo03
{
    public static void main(String[] args) 
    {
        StringBuffer buf = new StringBuffer();
        buf.append("World");
        buf.insert(0,"Hello");
        System.out.println(buf);
        buf.insert(buf.length(),"MLDN~");
        System.out.println(buf);
    }
}

字符串反转操作 reverse()反转变成String类型

//字符串反转操作 reverse()反转变成String类型
class  StringBufferDemo04
{
    public static void main(String[] args) 
    {
        StringBuffer buf = new StringBuffer();
        buf.append("WORLD!!");
        buf.insert(0,"Hello");
        String str = buf.reverse().toString();
        System.out.println(str);
    }
}

替换指定范围的内容 replace()

//替换指定范围的内容 replace()
class  StringBufferDemo05
{
    public static void main(String[] args) 
    {
        StringBuffer buf = new StringBuffer();
        buf.append("Hello").append("world");
        buf.replace(6,11,"xxxx");
        System.out.println("内容替换之后"+buf);
    }
}

删除指定范围的字符串

class  StringBufferDemo07
{
    public static void main(String[] args) 
    {
        StringBuffer buf = new StringBuffer();
        buf.append("hello").append("world");
        buf.replace(7,11,"xxxx");
        String str = buf.delete(6,8).toString();
        System.out.println(str);
    }
}

查找指定的内容是否存在 indexOf()

//查找指定的内容是否存在 indexOf()
class  StringBufferDemo08
{
    public static void main(String[] args) 
    {
        StringBuffer buf =new StringBuffer();
        buf.append("Hello").append("World");
        if (buf.indexOf("Hello")==-1)           //没有查找到则返回-1
        {
            System.out.println("没有查找到指定内容");
        }else{
            System.out.println("可以查找到指定的内容");
        }
    }
}

Runtime类

在Java中Runtime类表示运行时操作类,是一个封装了JVM进程的类,每一个JVM都会对应着一个Runtime类的实例,此实例有JVM运行时为其实例化。所以在JDK文档中读者不会发生任何有关Runtime类中构造方法的定义,这是因为Runtime类本身的构造方法时私有化的(单例设计),如果想取得一个Runtime实例,则只能通过以下的方式

Runtime run = Runtime.getRuntime();

取得JVM中的信息,观察JVM的内存空间

class  RuntimeDemo01
{
    public static void main(String[] args) 
    {
        Runtime run =Runtime.getRuntime(); //通过Runtime类的静态方法为其进行实例化操作
        System.out.println("JVM最大的内存量:"+run.maxMemory());
        System.out.println("JVM空闲内存量:"+run.freeMemory());
        String str = "Hello"+"World"+"!!!"+"\t"+"Welcome"+"To"+"HD"+"~";
        System.out.println(str);
        for (int x= 0;x<1000 ;x++ )
        {
            str +=x;
        }
        System.out.println("z操作String之后,内存空间:"+run.freeMemory());
        run.gc();
        System.out.println("垃圾回收后。JVM内存空间:"+run.freeMemory());

    }
}

调用本机中执行程序

//调用本机可执行程序
class  RuntimeDemo02
{
    public static void main(String[] args) 
    {
        Runtime run = Runtime.getRuntime();
        try{
            run.exec("editplus.exe");
        }catch(Exception e){
            e.printStackTrace();}
    }
}

System类

System类是一些与系统相关属性和方法的集合,而且在System类中所以的属性都是静态的,可以直接使用System.方法()调用。

System类的应用:
计算花费的时间 System.currentTimeMillis()

//System类  计算花费的时间 System.currentTimeMillis()
class  SystemDemo01
{
    public static void main(String[] args) 
    {
        long startTime = System.currentTimeMillis();
        int sum = 0;
        for (int i = 0;i<3000000 ;i++ )
        {
            sum +=i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("计算所花费的时间:"+(endTime-startTime)+"毫秒");
    }
}

取得本机的全部环境属性

//取得本机的全部环境属性
class  SystemDemo02
{
    public static void main(String[] args) 
    {
        System.getProperties().list(System.out);
    }
}

对象的生命周期
一个类加载后进行初始化,然后就可以进行对象的实例化,对象的实例化会调用构造方法完成,当一个对象不再被使用时就要等待被垃圾收集,然后对象终结,最终被程序卸载。

日期操作类

Date类

得到当前系统日期

//Date类 得到当前系统日期
import java.util.Date;
class  DateDemo01
{
    public static void main(String[] args) 
    {
        Date date = new Date();
        System.out.println("当前日期为"+date);
    }
}

从结果可以看出,已经得到了系统的当前时间,但是并不是我们平常看到的格式。

Calendar类

Calendar类本身是一个抽象类,必须依靠对象的多态性,通过子类进行父类的实例化操作,Calendar类的子类是GregorianCalendar类。

//Calender类  抽象类,必须通过子类来实现实例化
import java.util.Calendar;
import java.util.GregorianCalendar;
class  DateDemo2
{
    public static void main(String[] args) 
    {
        Calendar calendar = null;
        calendar = new GregorianCalendar();//通过子类为其实例化
        System.out.println("年:"+calendar.get(Calendar.YEAR));//年
        System.out.println("月:"+calendar.get(Calendar.MONTH)+1);//月
        System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));//日
        System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY));//时     System.out.println("分:"+calendar.get(Calendar.MINUTE));//分
        System.out.println("秒:"+calendar.get(Calendar.SECOND));//秒
System.out.println("毫秒:"+calendar.get(Calendar.MILLISECOND);//毫秒
    }
}

DateFormat类

//DateFormat 实现Date格式化

//DateFormat 实现Date格式化
import java.text.DateFormat;
import java.util.Date;
class  DateDemo03
{
    public static void main(String[] args) 
    {
        DateFormat df1=null;
        DateFormat df2=null;
        df1=DateFormat.getDateInstance();
        df2=DateFormat.getDateTimeInstance();
        System.out.println("DATE:"+df1.format(new Date()));
        System.out.println("DATETIME:"+df2.format(new Date()));
    }
}

指定风格

//指定显示的风格
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
class  DateDemo04
{
    public static void main(String[] args) 
    {
        DateFormat df1=null;
        DateFormat df2=null;
        df1=DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));
        //取得日期时间,设置日期的显示格式、时间的显示格式
        df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_YIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
        System.out.println("DATE:"+df1.format(new Date()));
        System.out.println("DATETIME:"+df2.format(new Date()));
    }
}

格式是显示其默认的时间显示格式。如果想要得到自己需要的日期显示格式,必须通过SimpeDateFormat类来实现。

SimpleDateFormat类

要使用SimpleDateFormat类来实现自定义格式,则必须先定义一个完整的日期转换模板,在模板中通过的特定日期标记可以将一个日期格式中的日期数字提取出来。

//指定显示的风格
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
class  DateDemo04
{
    public static void main(String[] args) 
    {
        DateFormat df1=null;
        DateFormat df2=null;
        df1=DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN"));
        //取得日期时间,设置日期的显示格式、时间的显示格式
        df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_YIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
        System.out.println("DATE:"+df1.format(new Date()));
        System.out.println("DATETIME:"+df2.format(new Date()));
    }
}

首先使用第1个模板将字符串中表示的日期数字取出,然后再使用第2个模板将这些日期数字重新成新的格式表示。

基于SimpleDateFormat类——取得完整的日期

//实现二:基于SimpleDateFormat类
import java.text.SimpleDateFormat;
import java.util.Date;
class DateTime
{
    //声明日期格式化操作对象,直接对new Date()进行实例化
    private SimpleDateFormat sdf = null;
    //得到完整的日期,格式为:yyyy-MM-dd  HH:mm:ss.SSS
    public String getDate(){
        this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return this.sdf.format(new Date());
    }
    public String getDateComplete(){
        this.sdf = new SimpleDateFormat("yyyy年MM年dd日 HH时mm分ss秒SSS毫秒");
        return this.sdf.format(new Date());
    }
    public String getTimeStamp(){
        this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return this.sdf.format(new Date());
    }
}

class  DateDemo07
{
    public static void main(String[] args) 
    {
        DateTime dt =new DateTime();
        System.out.println("系统日期:"+dt.getDate());
        System.out.println("中文日期::"+dt.getDateComplete());
        System.out.println("时间戳:"+dt.getTimeStamp());
    }
}

Math类

Math类是数学操作类,提供了一系列的数学方法。

Math类的基本操作

//Math类的基本操作
class  MathDemo01
{
    public static void main(String[] args) 
    {
        System.out.println("求平方根:"+Math.sqrt(9.0));
        System.out.println("求两数的最大值:"+Math.max(10,30));
        System.out.println("求两数的最小值:" +Math.min(10,30));
        System.out.println("2的3次方:"+Math.pow(2,3));
        System.out.println("四舍五入:" +Math.round(33.6));
    }
}

Random类

Random类是随机数产生类,可以指定一个随机数的范围,然后任意产生在此范围中的数字。

//Random类 生成10个随机数字,且数字不大于100
import java.util.Random;
public class RandomDemo01
{
    public static void main(String[] args){
        Random r = new Random();
        for (int i=0;i<10 ;i++ )
        {
            System.out.print(r.nextInt(100)+"\t");
        }
    }
}

NumberFormat类

NumberFormat类表示数字的格式化类,即可以按照本地的风格习惯进行数字的显示。
使用当前语言环境格式数字

import java.text.NumberFormat;
class  NumberFormatDemo01
{
    public static void main(String[] args) 
    {
        NumberFormat nf =null;
        nf = NumberFormat.getInstance();
        System.out.println("格式化后的数字是:"+nf.format(10000000));
        System.out.println("格式化后的数字是:"+nf.format(1000.345));

    }
}

BigInteger类

当一个数非常大时,则肯定无法使用基本类型接收,BigInteger类表示的是大整数类。

//BigInteger类
import java.math.BigInteger;
class  BigIntergerDemo01
{
    public static void main(String[] args) 
    {
        BigInteger bi1 = new BigInteger("123456789");
        BigInteger bi2 = new BigInteger("987654321");
        System.out.println("加法操作:"+bi2.add(bi1));
        System.out.println("减法操作:"+bi2.subtract(bi1));
        System.out.println("成法操作:"+bi2.multiply(bi1));
        System.out.println("除法操作:"+bi2.divide(bi1));
        System.out.println("最大数:"+bi2.max(bi1));
        System.out.println("最小数:"+bi2.min(bi1));
    }
}

BigDecimal类

进行四舍五入的四则运算

//BigDecimal类 
//进行四舍五入的四则运算
import java.math.BigDecimal;
class MyMath
{
    public static double add (double d1,double d2){
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2= new BigDecimal(d2);
        return b1.add(b2).doubleValue();
    }


    public static double sub (double d1,double d2){
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2= new BigDecimal(d2);
        return b1.subtract(b2).doubleValue();
    }

        public static double mul (double d1,double d2){
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2= new BigDecimal(d2);
        return b1.multiply(b2).doubleValue();
    }

        public static double div (double d1,double d2,int len){
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2= new BigDecimal(d2);
        return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

        public static double round(double d,int len){
        BigDecimal b1 = new BigDecimal(d);
        BigDecimal b2= new BigDecimal(1);
        //任何一个数字除以1都是原数字
        //ROUND_HALF_UP是BigDecimal的一个常量,表示进行四舍五入的操作
        return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
    }


}
class  BigDecimalDemo01
{
    public static void main(String[] args) 
    {
        System.out.println("加法运算"+MyMath.round(MyMath.add(10.345,3.333),1));
        System.out.println("乘法运算"+MyMath.round(MyMath.mul(10.345,3.333),3));
        System.out.println("除法运算"+MyMath.div(10.345,3.333,3));
        System.out.println("加法运算"+MyMath.round(MyMath.sub(10.345,3.333),3));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值