栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022.05.29--java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022.05.29--java

包装类基本用法
1.包装类
2.字符串相关类(StringStringBuilderStringBuffer)
3.时间相关类
4.其他类(Math、RandomFile枚举)
5.递归打印目录树结构

1.包装类基本知识
基本数据类型        包装类
byte            Byte
boolean            Boolean
short            Short
char            Character    
int            Integer//最常用
long            Long
float            Float
double            Double

用法几乎一样,只是换了一个名字

Number类是抽象类,因此他的抽象方法,所有子类都需要提供实现,Number提供了抽象方法:
intValue()、longValue()floatValue()doubleValue()意味着所有的数字型包装类都可以互相转型

    //基本数据类型转为对象
        Integer i=new Integer(300);
        Integer i2=Integer.valueOf(20);
        //包装类对象转为基本数据类型
        double d=i2.doubleValue();
        //字符串数字转成Integer对象
        Integer i3=Integer.valueOf("234");
        Integer i4=Integer.parseInt("334");
        //Integer对象转成字符串
        String str=i3.toString();
        //一些常用的常量
        System.out.println(Integer.MAX_VALUE);
        //结果:包装类可以把包装类对象,基本数据类型,字符串相互转

自动装箱和拆箱

自动装箱:
    基本类型的数据处于需要对象的环境中时,会自动转为"对象"
    编译器自动帮做的

自动拆箱:

 

        //包装类的缓存问题
        Integer d1=4000;
        Integer d2=4000;
        //当数字在-128~127时,返回缓存数组中的某种元素
        Integer d3=123;//实际是从缓存中取的123
        Integer d4=123;//第二次取的也是同一个对象
        System.out.println(d3==d4);//true
        System.out.println(d1==d2);//false  两个不同的对象
        System.out.println(d1.equals(d2));//true  //比较的是对象的值

自定义一个包装类

public class MyInteger {
    private int value;
    private  static  MyInteger[] cache=new MyInteger[256];//数组需要声明容量

    public static  final  int Low=-128;
    public  static  final int High=127;
    static{
        for(int i=Low;i<=High;i++){
            cache[i+128]=new MyInteger(i);
        }
    }
    public static MyInteger valueof(int i){
        if(i>=-Low&&i<=High){
            return cache[i+128];
        }
        return new MyInteger(i);
    }

    private MyInteger(int i){
        this.value =i;
    }

    public static void main(String[] args) {
        MyInteger m=MyInteger.valueof(200);

        
        System.out.println(m);//MyInteger@1b6d3586
       //1.基本类型转MyInteger;
        //2.MyInteger转基本类型
        //缓存,提高效率;-128-127

    }
}


String类
String类        不可变字符序列  常量只能被初始化一次
StringBuilder类        可变字符序列    线程不安全,效率高
StringBuffer类        可变字符序列    线程安全,效率低

    String str1="hello"+" java";
        String str2="hello java";
        String str3="hello";
        String str4=" java";
        String str5=str3+str4;
        System.out.println(str1==str2);//true
        System.out.println(str2==str5);//false

StringBuilder类

StringBuffer类

       
        StringBuffer s3=new StringBuffer("jin");
        s3.insert(3,"yu").insert(5,"sheng");//jinyusheng--第三个后插入
        System.out.println(s3);//jinyusheng
        s3.delete(0,3);//yusheng:0开始删三个
        System.out.println(s3);
        s3.deleteCharAt(1).deleteCharAt(0).deleteCharAt(0);//ysheng--sheng--heng
        System.out.println(s3.charAt(3));//g
        System.out.println(s3.reverse());//字符串逆序,gneh

不可变和可变字符序列使用陷阱
    public class Test1001 {
        public static void main(String[] args) {
        String str="";
        long num1=Runtime.getRuntime().freeMemory();
        long time1=System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            str=str+i;//产生了5000个对象
        }
        long num2=Runtime.getRuntime().freeMemory();
        long time2=System.currentTimeMillis();
        System.out.println("String占用内存"+(num2-num1));
        System.out.println("String耗时"+(time2-time1));
        StringBuilder str2=new StringBuilder("");
        long num3=Runtime.getRuntime().freeMemory();
        long time3=System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            str2.append(i);//只有一个对象
        }
        long num4=Runtime.getRuntime().freeMemory();
        long time4=System.currentTimeMillis();
        System.out.println("StringBuilder占用内存"+(num4-num3));
        System.out.println("StringBuilder耗时"+(time4-time3));
        }
    }
    
//大量修改的时候一定要用StringBuilder,不然会造成空间和内存浪费
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号