java.lang中的一些类的用法,System,String,Runtime,Math,包装类 2020.12.17

今日感受

今天是上课以来最难顶的一天,教的几个类方法没记得多少,老师还给了三道题,烦的很,就做出来第一道,第二道还有点bug,第三道根本没动什么笔,现在快23:00了,才开始写心得,我的思想都被这三道题搅得很累很乱,学了啥都记不太全了,不过还是能写多少写多少吧。

第一个,System类,大部分方法对于现在的我似乎很难用的上,比较重要的输入输出流,老师说后面才讲。

sy.getProperties与.getenv,一是获取当前系统的属性,种类很多;二是返回一个不能修改的当前系统环境的字符串映射图(这里是从网上文档上看到的)返回的是map类型,老师说只有做完整的应用程序时才会用到,其获取的方法我看不懂,老师写的for获取的。。。。

.currentTimeMillis()与nanoTime(),分别是从1970.1.1号开始到现在时间的毫秒数与纳秒数,是用来计算一个程序运行耗时的。

.exit(int x)用于终止正在运行的java虚拟机,参数x不为0时表示异常终止,可以使用参数用来区分异常终止出现在哪个部分,由编程员而定。

.gc()垃圾回收,回收堆中的未用对象,不过老师说作用并不大。

第二个,Runtime类,每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时,我理解为运行时的一个类。

.exec();里面填写一条字符串系统命令,该指令可在cmd下使用,并且效果一致,挺好玩的可以直接打开某个软件等;

 第三个,Math类,与数学计算相关,方法都围绕数学上的计算而来,这里列举几个我自己认为比较实用的:

Math.abs(x):返回x绝对值;max(a,b)返回a与b中较大的一个;min(a,b)返回较小的一个;round(double a):返回最接近参数的long,round(float a):返回最接近参数的int;sqrt(double a):返回正确舍入的double值的正平方根;random():返回带正号的double值,该值大于等于0.0且小于1.0,可用作计算随机数。

第四个,包装类,Integer,Long,Short等,即是基本数据类型包装而来,Integer变为int为拆箱,从“https://www.cnblogs.com/bigdata-stone/p/10560759.html”上看到的区别:

1.Integer是int的包装类,int则是java的一种基本的数据类型;

2.Integer变量必须实例化之后才能使用,而int变量不需要实例化;

3.Integer实际是对象的引用,当new一个Integer时,实际上生成一个指针指向对象,而int则直接存储数值

4.Integer的默认值是null,而int的默认值是0。

这个网址的博客上还讲了int与Integer的比较,还有Integer与Integer的比较,有几种情况,介绍的还是比较详细。

缓存池(-128到127)IntegerCache固定的
Integer i1=123;//Integer.valueOf(123);==>IntegerCache.get();new Integer;
返回一个表示指定的 int 值的 Integer 实例。从缓存中get这个值,若是存在则返回这个值,若是没有则new一个Integer;
Integer i2=123;
syso(i1==i2);//true

Integer i1=1230;
Integer i2=1230;
syso(i1==i2);//false:因为超出了缓存限制,所以每次都是new一个Integer,所以地址不同,不是同一个对象。

第五个,String类,本身是一个字节数组,类中定义了一个final,私有的字节数组导致不可变。常量长度不可变,但里面的元素可以变,但是String本身没有提供接口用来改变;

String str=“abc”;
上面等同于下面的代码
char date[]={'a','b',''c};
String str=new String(data);

String s1="abc";
String s2 ="def";
String s3=s1+s2;
有四个对象,上方的代码,实际上做了下方代码的操作
//StringBuidler temp=new StringBuilder(String.valueof(s1));
//temp.append(s2);
//s3=temp.toString();

String s5="a"+"b"+"c"+"d";只有一个对象,虚拟机会进行优化操作
String s5="a"+"b"+s3+"c"+"d";有五个对象,ab是一个对象,s3是一个,c,d都是一个对象,
temp是一个对象。

三个虚拟机存储区域:

constant pool 常量池
Method area 方法区
Run-Time Constant Pool 运行时常量池

下面图片中有虚拟机规范中说明的翻译,还包括了本地方法栈(但现在看不太懂,能稍微理解一点就可以了,知道每个区域存着些什么)

常量池:

与缓存类似,不过没有限制,但一直放如果超出了虚拟机的限制,会有内存溢出错误OutOfMemoryError

String s2="abc";//先从常量池看看有没有abc这个字符串,如果没有就创建一个这样的字符串放在常量池中,然后赋予它s2;
String s1=new String(s2);
syso(s1==s2);//false
String s1="abc";
syso(s1==s2);//true比较的地址,因为他们指向同一个常量,并且放在同一个地方

然后是String的一些方法:

索引x
.charAt(x);对应索引的字符
codePointAt(x);索引对应字符对应的编码
concat("");连接,不能与null连接,不然会报空指针异常,所以一般都用“+”;
.contains("");是否包含里面的字符,返回一个boolean值
.startWith();是否以什么什么开头,返回布尔值
.endWith();以什么什么结尾
.equals();对比两字符串是否一样
.equalsIgnoreCase();忽略大小写比较
.contentEquals();比较内容是否一样,
.indexOf();可以传入字符或者数字,数字则是查询编码对应的字符,对应的索引,若传入的是字符串,则返回的是第一个字符的索引
.isEmpty();是否为空
.length();字符个数
.getBytes();转成字节数组,中文会变成两字节
.replace(x,"");替换,但只是返回替换后的值,并不会改变原来的字符串
.split("");以某字符串进行切割,返回每一个这个字符串后面的一段字符组合的字符数组
.substring(x),从索引处截取,包括该索引的值
.substring(x,y),从x截取,到y索引处,包括x处的值,不包括y索引处的值(jdk6与8实现有很大区别)
.inter();返回该对象在常量池的值


String s1=new String("abc");//new了一个对象所以,s1会通过堆指向,堆也会指向常量池的abc
String s2= s1.intern();s2则是常量池的指向
s1==s2//false
//可变长度参数,可变长度参数只能作为形参列表的最后一个参数


.join(",","a","b");通过,合并
.trim();去两边的空格
valueof(各种数据类型)==>调用他们的toString方法,基本数据类型转换为String,与直接+""效果一样
s1.compareTo(s2);一个字符一个比较,0则相等,负数则s1小于s2;正数则大于,根据对应编码的大小比较

其中substring方法在jdk1.6与1.8中实现的方法不一样,

我还找到了一个关于这个的博客,地址是“https://www.cnblogs.com/zjiacun/p/8278044.html”,介绍了6与7的区别

StringBuilder与StringBuffer,可变的String,两者区别只在于,前者无法保证线程的安全,后者则可以保证,具体在多线程同步时体现。

一个字符串+一个字符串;运算符重载,字符串拼接,java可以,其他语言不一定可以;实际上是使用上面两者的append方法进行实现的;然后是一些方法:

.append();本身可以改变,在字符串后面加入
.insert(索引,值);在指定的地方插入,插入的第一个字符的索引为指定索引
.delete(索引1,索引2);删除从索引1到索引2的字符串,不包括索引2的值,包括索引1的值

然后有三个作业,第一个是实现将一个数字字符串逆序,并且要将原字符串后面的0换到前面来时得去除

这个作业我一开始做完了,但后面在力扣刷题时发现了一些没用注意的点,就是使用int时逆序后不能超出int的边界,不然没有效果,不过如果使用long应该就没有这个问题,我是做了一个判断,当超出是使sum等于0;

public class text2 {
    public static void main(String[] args) {

        //123450 --->54321
        int n =15400120;//004051-->4*10^3+5*10^1+1*10^0 
        int k=n;
        int num=1;
        int sum=0;
        while(n/10!=0){
            n/=10;
            num++;
        }
        int []h=new int[num];
        for(int i=0;i<num;i++){
            int b=k%10;    
            k/=10;
            sum+=b*Math.pow(10,h.length-i-1);
            h[i]=b;
        }
        if(sum>=(Math.pow(2,31)-1)||sum<=Math.pow(-2,31)){
            sum=0;
        }
        System.out.println(sum);
        }
}

第二个作业是将数字转换为中文的大写,具有钱的性质,即有金额单位,一直到现在还是有bug,,

第三作业是重写一个自己的MyStringBuffer:

public class MyStringBuffer {
    char[]value;
    int count;
    public MyStringBuffer(String str){
        char[]arr=str.toCharArray();
        value=new char[arr.length];
        System.arraycopy(arr, 0, value, 0, arr.length);
        count=arr.length;
    }
    public MyStringBuffer(int capacity){
        if(capacity<=0){
            capacity=16;
        }
        value=new char[capacity];
    }
    public void append(String str){
        append(str.toCharArray());
    }
    private void ensureCapacity(char[]arr){
        if(count+arr.length>=value.length){
            int cap=(count+arr.length)*2;
            char[]temp=new char[cap];
            System.arraycopy(value, 0, temp, 0, count);
            value=temp;
        }
    }
    public void append(char[]str){
        ensureCapacity(str);
        System.arraycopy(str, 0, value, count, str.length);
        count+=str.length;
    }
    public void append(char c){
            append(new char[]{c});
    }
    public void delete(int start,int end){
        if(start<0||start>=count){
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        if (end<0||end>=count) {
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        if(start>end){
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        System.arraycopy(value, end, value, start, count-end);
        count-=(end-start);
            
    }
    public int indexOf(String str){
        if(str.length()>count){
            return -1;
        }
        char[]arr= str.toCharArray();
        for (int i = 0; i < count; i++) {
            boolean flag=true;
            for (int j = 0; j < arr.length; j++) {
                if (value[i+j]!=arr[j]) {
                    flag=false;
                    break;
                }
            }
            if(flag)
                return i;
        }
        return -1;
    }
    public int lastIndexOf(String str){
        if(str.length()>count){
            return -1;
        }
        char[]arr= str.toCharArray();
        for (int i = count; i>0; i--) {
            boolean flag=true;
            for (int j = 0; j < arr.length; j++) {
                if (value[i+j]!=arr[j]) {
                    flag=false;
                    break;
                }
            }
            if(flag)
                return i;
        }
        return -1;
    }
    public void insert(int index,String str){
        if(index<0||index>=count){
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        if (null==str) {
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        ensureCapacity(str.toCharArray());
        System.arraycopy(value, index, value, index+str.length(), count-index);
        System.arraycopy(str.toCharArray(), 0, value, index, str.length());
        count+=str.length();
    }
    public int length(){
        return count;
        
    }
    public String substring(int start,int end){
         if (start < 0)
                throw new StringIndexOutOfBoundsException(start);
            if (end > count)
                throw new StringIndexOutOfBoundsException(end);
            if (start > end)
                throw new StringIndexOutOfBoundsException(end - start);
            return new String(value, start, end - start);
    }
    public void trim(){
        if(count>0) {
            int begin = 0;
            for(int i=0;i<count-1;i++) {
                if(!Character.isSpaceChar(value[i])) {
                    begin=i;
                    break;
                }
            }
            delete(0, begin);
            int end = 0;
            for(int i=count-1;i>0;i--) {
                if(!Character.isSpaceChar(value[i])) {
                    end=i;
                    break;
                }
            }
            delete(end+1, count);
        }
    }
    public String toString(){
        return new String(value,0,count);
    }
    public char charAt(int index){
        if(index<0||index>=index){
            throw new IndexOutOfBoundsException("Are you zhizhang?");
        }
        return value[index];
    }
}
原文地址:https://www.cnblogs.com/zzdbk/p/14151803.html