Java基础笔记2

Java基础笔记2

Java常用类

java.lang.String

final类:不可继承

创建方式:

String s1 = "hello"; //会在字符串常量池中创建hello字符串,不可修改

String s2 = new String("hello"); //既在堆中创建,也在常量池中创建

字符串拼接会开辟新的内存空间,尽量避免频繁的拼接操作,可使用StringBuffer或者StringBuilder

例如:

//字面量相加在常量池,只要含有非字面量,都会在堆中分配空间,存储常量池中对应的字符串,相当于new
String s = "aaa";
String s1 = s + "bbb";

总结:

  • 常量与常量的拼接结果在常量池,且常量池不会存在内容相同的常量

  • 只要其中有一个是变量,结果就在堆中

  • 如果拼接的结果调用intern()方法,返回值就在常量池中

字符串处理常用方法:

        String s = "helloWorld";
        
        System.out.println(s.charAt(1));//e
        
        System.out.println(s.contains("ello"));//true
        
        System.out.println(s.contains("allo"));//false
        
        System.out.println(s.endsWith("ld"));//true
        
        System.out.println(s.equalsIgnoreCase("HeLLoWorld"));//true
        
        String s1 = "abc";
        byte[] b = s1.getBytes();
        System.out.println(Arrays.toString(b));//[97,98,99]
        
        System.out.println(s.indexOf("e"));//1
        
        System.out.println(s.indexOf("l"));//2
        
        System.out.println(s.indexOf("o",6));//6
        
        System.out.println(s.lastIndexOf("l"));//8
        
        System.out.println(s.lastIndexOf("l",7));//3
        
        System.out.println(s.length());//10
        
        String s2 = "abcabdabe";
        
        System.out.println(s2.replaceAll("ab","ba"));//bacbadbae

        String s3 = "a,b,c,d";

        String[] a = s3.split(",");
        System.out.println(Arrays.toString(a));//[a, b, c, d]

        System.out.println(s.endsWith("rld"));//true

        System.out.println(s.substring(5));//World

        System.out.println(s.substring(5,7));//Wo

        System.out.println();

        char[] a1 = s.toCharArray();
        System.out.println(Arrays.toString(a1));//[h, e, l, l, o, W, o, r, l, d]

        System.out.println(s.toUpperCase());//HELLOWORLD

        System.out.println(s.toLowerCase());//helloworld

        String s4 = " pxw 123 ";
        System.out.println(s4.trim());//pxw 123

        //将其它类型转换为字符串
        int i = 34;
        System.out.println(String.valueOf(i));//34

正则表达式

数字:\d

非数字:\D

英文字母:\w

非英文字母:\W

邮箱格式:^(\w+((-\w+)|(.\w+)))+\w+((-\w+)|(.\w+))\@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$

手机号码:^13|4|5|7|8\d{4,8}$

java.lang.StringBuffer和java.lang.StringBuilder

String:1.0 不可变的字符序列,底层用char[]存储

StringBuffer: 1.0 可变的字符序列: 线程安全,底层用char[]存储

StringBuilder:jdk5.0 新增 可变的字符序列: 线程不安全,效率高,底层用char[]存储

源码分析:

String str = new String();//char[] value = new char[0];底层创建了一个长度为0的数组
String str1 = new String("abc");//char[] value = new char[]{'a','b','c'}

StringBuffer sb1 = new StringBuffer();//char[] value = new char[16];底层创建了一个长度为16的数组
System.out.println(sb1.length);//0
sb1.append('a');//value[0] = 'a';
sb1.append('b');//value[1] = 'b';

StringBuffer sb2 = new StringBuffer("abc");char[] value = new char["abc".length + 16]
System.out.println(sb2.length);//3
//扩容问题:要添加的数据底层数组存不下了,需要扩容
//默认情况下扩容为原来容量的2倍+2,同时将原来数组中的元素赋值到新数组中
//开发中,建议使用StringBuffer(int capacity)或StringBuilder(int capacity)

StringBuffer常用方法

        StringBuffer s1 = new StringBuffer("abc");
        s1.append(1);
        s1.append('1');
        System.out.println(s1);//abc11
//        s1.delete(2,4);//左闭右开 ab1
//        System.out.println(s1);
//        s1.replace(2,4,"hello");
//        System.out.println(s1);//abhello1
//        s1.insert(2,false);
//        System.out.println(s1);//abfalsec11
//        System.out.println(s1.length());//10
//        System.out.println(s1.reverse());//11cba
//        System.out.println(s1.indexOf("11"));//3
//        System.out.println(s1.substring(2,4));//c1 , s1未改变
//        System.out.println(s1.charAt(0));//a
//        s1.setCharAt(0,'h');
//        System.out.println(s1);//hbc11
//        s1.deleteCharAt(0);
//        System.out.println(s1);//bc11
          s1.replace(2,4,"aa");
          System.out.println(s1);//abaa1   

日期和时间

jdk8.0 之前

        //1.System类
        long time = System.currentTimeMillis();
        //返回当前时间与1970年1月1日0分0秒之间以毫秒为单位的时间差
        //称为时间戳
        System.out.println(time);//1587889160608
        //2.java.util.Date
        Date date1 = new Date();
        System.out.println(date1);//Sun Apr 26 16:18:54 GMT+08:00 2020
        System.out.println(date1.getTime());//1587889160609
        Date date2 = new Date(1587889160609L);
        System.out.println(date2.toString());//Sun Apr 26 16:19:20 GMT+08:00 2020
        //3.java.sql.Date,对应数据库中日期变量
        java.sql.Date date = new java.sql.Date(1587889160609L);
        System.out.println(date);//2020-04-26
        //sql.Date -> util.Date 因为是子类,直接赋值即可
        Date date3 = new Date();
        date3 = date;
        System.out.println(date3);//2020-04-26
        //util.Date -> sql.Date
        Date date4 = new java.sql.Date(1587889160609L);
        java.sql.Date date5 = (java.sql.Date)date4;
        System.out.println(date5);//2020-04-26

        Date date6 = new Date();
        java.sql.Date date7 =  new java.sql.Date(date6.getTime());
        System.out.println(date7);//2020-04-26

        //3.java.text.SimpleDateFormat:对日期Date类的格式化和解析
        //格式化:日期 -> 字符串
        //使用默认构造器
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date date = new Date();
        String format = sdf.format(date);
        System.out.println(format);//2020/4/26 下午4:46

        //解析:字符串 -> 日期
        String str = "2020/4/26 下午4:46";
        Date date1 = sdf.parse(str);
        System.out.println(date1);//Sun Apr 26 16:46:00 GMT+08:00 2020
        
        //使用带参构造器,指定的方式进行格式化
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1 = sdf1.format(date);
        System.out.println(format1);//2020-04-26 04:51:35
        //解析
        Date date2 = sdf1.parse("2020-04-26 04:51:35");
        System.out.println(date2);//Sun Apr 26 04:51:35 GMT+08:00 2020

        //4.Calendar日历类(抽象类)
        //实例化
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());//class java.util.GregorianCalendar
        //常用方法
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//26
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//117
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//1

        calendar.set(Calendar.DAY_OF_MONTH,22);//可变
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//22

        calendar.add(Calendar.DAY_OF_MONTH,5);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//27

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//26
        //日历类-> Date
        Date time = calendar.getTime();
        System.out.println(time);//Sun Apr 26 17:02:30 GMT+08:00 2020
        //Date->日历类
        Date date3 = new Date();
        calendar.setTime(date3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);//26

jdk8.0新增

        //1、java.time.LocalDate、LocalTime、LocalDateTime 类似Calendar
        LocalDate now = LocalDate.now();
        LocalTime now1 = LocalTime.now();
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now);//2020-04-26
        System.out.println(now1);//17:16:04.573103500
        System.out.println(now2);//2020-04-26T17:16:04.573103500
        //指定时间
        LocalDateTime of = LocalDateTime.of(2020, 04, 26, 17, 16, 04);
        System.out.println(of);//2020-04-26T17:16:04
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.getDayOfMonth());//26
        System.out.println(localDateTime.getDayOfWeek());//SUNDAY
        System.out.println(localDateTime.getDayOfYear());//117
        System.out.println(localDateTime.getHour());//17
        System.out.println(localDateTime.getMonth());//APRIL
        System.out.println(localDateTime.getMonthValue());//4
        //体现了不可变性
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime);//2020-04-26T17:21:55.474149300
        System.out.println(localDateTime1);//2020-04-22T17:21:55.474149300

        LocalDateTime localDateTime2 = localDateTime.plusMonths(3);
        System.out.println(localDateTime2);//2020-07-26T17:23:32.677430400

        LocalDateTime localDateTime3 = localDateTime.minusMonths(3);
        System.out.println(localDateTime3);//2020-01-26T17:24:13.805184

        //java.time.Instant 类似于Date
        Instant now3 = Instant.now();
        System.out.println(now3);//2020-04-26T09:25:42.649069Z
        OffsetDateTime offsetDateTime = now3.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2020-04-26T17:28:20.964161500+08:00
        long l = now3.toEpochMilli();
        //时间戳
        System.out.println(l);//1587893381234
        Instant instant = Instant.ofEpochMilli(1587893381234L);
        System.out.println(instant);//2020-04-26T09:29:41.234Z

        //java.time.format.DateTimeFormatter 类似SimpleDateFormat
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化 日期->字符串
        LocalDateTime now4 = LocalDateTime.now();
        formatter.format(now4);
        System.out.println(now4);//2020-04-26T17:34:19.844345900
        //解析
        String text = "2020-04-26T17:34:19.844345900";
        TemporalAccessor parse = formatter.parse(text);
        System.out.println(parse);//{},ISO resolved to 2020-04-26T17:34:19.844345900

        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String format = formatter1.format(now4);
        System.out.println(format);//2020/4/26 下午5:37

        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format1 = formatter2.format(now4);
        System.out.println(format1);//2020年4月26日星期日

        //自定义格式
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format2 = formatter3.format(now4);
        System.out.println(format2);//2020-04-26 05:42:00
        //解析
        TemporalAccessor parse1 = formatter3.parse("2020-04-26 05:42:00");
        System.out.println(parse1);//{MicroOfSecond=0, MilliOfSecond=0, SecondOfMinute=0, NanoOfSecond=0, MinuteOfHour=42, HourOfAmPm=5},ISO resolved to 2020-04-26
posted @ 2020-04-26 17:52  pengxiaowei  阅读(208)  评论(0)    收藏  举报