7-18学习笔记

一、字符串

        String  类  引用类型  默认值 null  不是""
        String类被final修饰 没有子类

1、声明字符串

        String str="abc你好";
        str=new String();
        str=new String("你好");
        char[] arr={'a','b','c',97};
        str=new String(arr);
        System.out.println(str);
        str=String.valueOf(12);
        System.out.println(str);

2、字符串的拼接  +

        加号在拼接字符串和数字运算时优先级是一样的
        System.out.println("123"+123+123);//123123123
        System.out.println(123+123+"123");//246123  优先级一样从左到右运算
        字符串和所有类型相加(+)后得到的都是字符串
        str="123"+"abc";//123abc
        str="123"+23;//12323
        str="123"+new Object();//"123"+这个对象的toString的结果
        str="123"+new int[]{1,2,3};//123[I@4554617c即"123"+第一个元素的地址
        System.out.println(str);

 3、字符串的比较

        使用equals方法比较字符串
        boolean bool="123".equals("123");//true
        String strA=new String("123");
        String strB=new String("123");
        bool=strA.equals(strB);//true
        System.out.println(bool);

4、String类型中的常用方法

(1)将常用的参数转成字符串 valueOf(Object o)
        String.valueOf('1');
        String.valueOf(new Object());
        String.valueOf("");
        //String.valueOf(null);

具体调用哪个重载方法  有就近原则  尽量具体

        test(null);//就近原则 尽量具体



    public static void test(String str){
        System.out.println("String");
    }
    public static void test(Object obj){
        System.out.println("Object");
    }

(2) 查找子串出现的位置(index) indexOf(String str)
        找不到返回-1
        int index="123456".indexOf("3457");
        System.out.println(index);//-1
        index="123123123".indexOf("1");//0
        index="123123123".lastIndexOf("1");//6
 (3)获取指定位置的字符  charAt(int index)
        char item="123456".charAt(4);//'5'
        System.out.println(item);
        //item="123456".charAt(8);//报错StringIndexOutOfBoundsException
 (4)截取字符串  substring(int begin)  substring(int begin,int end)
         str="123456".substring(1);//"23456"
        System.out.println(str);
        //包含开始下标,不包含结束下标
        str="123455".substring(1,5);//"2345" 有头无尾
(5)替换  replace(String target,String replacement)、replaceAll(String regex,String replacement)
str="12345634".replace("34","AAA");//"12AAA56AAA"
System.out.println(str);
str="12.31.23".replaceAll(".","A");//"AAAAAAAA" 此处’.‘代表任意字符 根据正则表达式进行替换
System.out.println(str);
(6)分割字符串  split(String regex)
        String[] strArr="123123123".split("2");//1,31,31,3
        System.out.println(Arrays.toString(strArr));
        strArr="123123123".split("1");//, 23, 23, 23
        System.out.println(Arrays.toString(strArr));
        strArr="1231231231".split("1");//, 23, 23, 23
        System.out.println(Arrays.toString(strArr));
(7)字符串长度 length方法
        int l="123123".length();
        for (int i = 0; i < str.length(); i++) {}
(8)去除前后空白位 trim()方法 空格 \n \t \r
        String strC="\n\r  \t  123  \n\r";
        System.out.println(strC);
        System.out.println("-----------------");
        System.out.println(strC.trim());

 (9)大写 小写 (针对字母) toUpperCase()  toLowerCase()
        str="123abc".toUpperCase();//转成大写
        System.out.println(str);
        str="123ABCabc".toLowerCase();//转成小写
        System.out.println(str);
(10)判断是否是空串  isEmpty()
        bool="123".isEmpty();//是空串 返回true
        System.out.println(bool);
        if(!str.isEmpty()){
            //str中有内容就要执行此代码
        }
(11)是否以“”开始 是否以“”结束  startsWith(String str)  endsWith(String str)
        "123456".startsWith("123");//true
        "123456".endsWith("65");//false

5、字符串常量池    池  容器  重用

        String  对象  定义后就不可改变  常量  private final修饰value数组
        字符串怎样加入到常量池中--- 使用量的方式声明的字符串就会加入到常量池中
        int a=12;//入常量池
        String str="abc";//入常量池
        Integer.valueOf("23");//入常量池
        char[] arr={'a','b','c'};//入常量池
        str=new String(arr);//不入常量池
        程序中第一次使用量的形式定义“123”,会将这个字符串对象存入<字符串常量池中>
        之后再使用量的形式使用该对象 就执行使用常量池中的对象
        String strA="123";
        String strB="123";
        System.out.println(strA==strB);//true

        String strC=new String("123");
        String strD=new String("123");
        System.out.println(strA==strC);//false
        System.out.println(strC==strD);//false

        //常量优化
        String strE="12"+"3";//解析时已化为"123"
        String strF="1"+"2"+"3";//"123"

        String item="12";
        String strG=item+"3";
        String strGG=item+3;
        System.out.println((strG==strGG)+"---------------");//false
        //str()+"3"
        String strH="12"+3;//"123"
        System.out.println(strA==strE);//true
        System.out.println(strA==strF);//true
        System.out.println(strE==strF);//true
        System.out.println(strA==strG);//false
        System.out.println(strA==strH);//true
        System.out.println(strG==strC);//false

        final String aa="12";
        String strI=aa+"3";
        System.out.println(strA==strI);//true

        String bb=new String("12");//用了构造方法 只有在运行后才能知道bb是什么
        String strJ=bb+"3";
        System.out.println(strA==strJ);//false
        String strK=aa+"3";
        System.out.println(strA==strK);//true

6、intern方法

        str.intern();
        返回 str对象在字符串常量池中的副本对象
        过程:检查str是否在字符串常量池中存在副本,如果不存在就复制一份存入到常量池中
            然后返回常量池中的副本对象
            如果已经存在副本对象,直接返回副本对象
        若有  两个字符串  equals  为true
        那么 两个字符串的intern方法==
        strA=new String("123123");
        strB=new String("123123");
        System.out.println(strA.equals(strB));//true
        System.out.println(strA==strB);//false
        System.out.println(strA.intern()==strB.intern());//true
        new String("abc"+”12“) 创建了几个对象  1个或2个

二、StringBuilder StringBuffer

        String  字符串定义后不可改变  存在常量池中
        String str="";
        for (int i = 0; i < 10; i++) {
            str=str+i;
        }
        //0 01 012 0123 01234  ......  0123456789
        为了在拼接字符串的时候不要产生中间串 减少字符串常量池的使用 可使用StringBuilder StringBuffer两个类
        调用append  往StringBuilder数组中追加字符
        其中没有产生字符串对象
        StringBuilder strB=new StringBuilder();
        strB.append("123");
        strB.append("abc");
        strB.append("456");
        
        System.out.println(strB);
StringBuilder默认容量是 16
StringBuilder 扩容
追加字符时容量不够就需要扩容(value)  默认  原来容量*2+2  若不够则要多少扩到多少
StringBuilder线程不安全
StringBuffer  线程安全的 其余与StringBuilder一样
 数组复制  System.arraycopy(原数组,复制开始位置下标,目标数组,粘贴开始位置下标,复制字符长度);

 Objects类

        Objects.isNull(null);//true  判断对象是否为null
        Objects.equals(strB,null);// 判断这两个对象是否相等 内容是否相同

 三、时间类型 Date

        用long来存储时间  1970-1-1 00:00:00 000开始累加 每1ms加1

1、获取当前时间  执行时的时间

        Date date=new Date();
        System.out.println(date);
        long time=date.getTime();//时间戳
        System.out.println(time);

        date.getMonth();//0-11之间

 2、时间格式化

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");//hh为12小时制
  String sdfStr=sdf.format(date);
  System.out.println(sdfStr);

 3、 2个日期类

        //LocalDate 本地日期  LocalDateTime 本地时间  不计时区
        LocalDateTime ldt=LocalDateTime.now();
        //记时区
        ZonedDateTime zdt=ZonedDateTime.now();
        Object obj=zdt.getZone();
        System.out.println(obj);

 四、随机数

1、Math.random()   真随机

        double ran=Math.random();//[0,1)随机数
        // ran*50  [8,90)即 (0-82)+8
        double ranNum=ran*82+8;

2、Random类   伪随机

        根据随机种子生成随机数,默认执行时的时间戳为随机种子  种子一样 随机数生成顺序一样
        Random ranObj=new Random(12);
        Random ranObjN=new Random(12);
        int a=ranObj.nextInt();
        int b=ranObjN.nextInt();
        System.out.println(a+"==============="+b);
        b=ranObjN.nextInt(200);
        a=ranObj.nextInt(200);
        System.out.println(a+"==============="+b);

五、取整

        ranNum=12.5;
        //四舍五入
        long num=Math.round(ranNum);//13
        int intNum=Math.round(12.33f);
        //向上取整
        double ceilNum=Math.ceil(ranNum);//13.0
        //向下取整
        double floorNum=Math.floor(ranNum);//12.0
       四舍五入时 遇小数为0.5的往上取 
        num=Math.round(0.5);// 1
        num=Math.round(-0.5);// 0  
        num=Math.round(-0.4);// 0
        num=Math.round(-0.6);// -1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值