【8-22】java学习笔记04

时间:2022-04-18 09:58:36

java基础类库


Scanner类(java.util.scanner)


Scanner对象.hasNextXxx(),hasNext()默认方法为字符串;//Returns true if this scanner has another token in its input.

Scanner对象.next(); //Finds and returns the next complete token from this scanner.
Scanner sc = new Scanner(System.in);
sc.useDelimiter(正则表达式);//sc.useDelimiter("\n");以换行符区分每一个输入
while(sc.hasNext()) { System.out.println(sc.next()); }

hash值计算(Static method in class java.util.Objects extend java.lang.Objects )


public class haha {

    public static void main(String[] args)
{
String a=new String("haha");
String b=new String("haha");
System.out.println("a的hash值:"+a.hashCode());
System.out.println("b的hash值:"+b.hashCode());
System.out.print("a的identityHashCode值:");
System.out.println(System.identityHashCode(a));
System.out.print("b的identityHashCode值:");
System.out.println(System.identityHashCode(b));
}
}

runtime类(java.lang.runtime)


import java.io.IOException;
public class hah {
public static void main(String[] args) throws Exception
{
Runtime rt=Runtime.getRuntime();
System.out.println(rt.availableProcessors());
System.out.println(rt.freeMemory());
System.out.println(rt.totalMemory());
System.out.println(rt.maxMemory());
rt.exec("notepad.exe");//开启新进程执行系统命令
}
}

object类


  • boolean equals(Object obj);//根据地址计算
  • int hashCode();//根据地址计算一般需重写
  • String toString();//类的tostring字符串为“类名@hashcode值”
  • protected Object clone() throws CloneNotSupportedException
  • ......

String类(java.lang.String java.lang.StringBuffer java.lang.StringBuilder)


  • String对象一旦创建就成为临时变量,StringBuffer和StringBuilder类为字符串对象提供了insert append replace...等方法,可以改变字符串对象(StringBuffer为线程安全的,效果会差一点);

Math工具类(java.lang.Math)


  • 其构造器被定义为private,因此无法创建Math对象,其多有方法都是类方法,可直接调用;
  • 提供PI和E两个类变量;

Random工具类(java.util.Random和java.util.concurrent.ThreadLocalRandom)


  • Random类产生伪随机数,种子相同产生的随机数序列相同;
  • 使用48位的种子;
  • 使用默认种子构造Random对象时,属于同一个种子;
  • 常用时间作为种子:
    import java.util.Random;
    public class hah {
    public static void main(String[] args) throws Exception
    {
    Random rand=new Random(System.currentTimeMillis()); int i=20;
    while(i>0)
    {
    int r=rand.nextInt();
    System.out.println("r:"+r);
    --i;
    }
    }
    }

BigDecimal类(java.math.BigDecimal)


  • 使用BigDecimal(String val)构造器,保持数值准确性

    BigDecimal(String val)
    Translates the string representation of a BigDecimal into a BigDecimal.

  • 或者使用valueOf(double val)等构造

    valueOf(double val)
    Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.

时间工具类(java.util.Date和java.util.Calendar)


  • Calendar类为抽象类,用getinstance()静态方法获取对象;
  • 可以将Date对象传个setTime()函数,设定时间;

    setTime(Date date)
    Sets this Calendar's time with the given Date.

  • 引入import static java.util.Calendar.*;包
  • set()方法有延时特性,在调用时才修改;

时间日期类(java.time.Clock和java.time)


正则表达式(java.util.regex.Pattern java.util.regex.Matcher)