常用类
1.String类
charAt(int index) 返回指定位置的字符 toCharArray() 将字符串转换为char数组 replace(String oldStr,String newStr) 替换指定内容的字符串 replaceAll(String regx,String newStr) 支持正则表达式的字符串替换 isEmpty() 判断是否为null 长度为0 返回为true 否则false
package com.qfedu.test1;/*** charAt(int index) 返回指定位置的字符* toCharArray() 将字符串转换为char数组* replace(String oldStr,String newStr) 替换指定内容的字符串* replaceAll(String regx,String newStr) 支持正则表达式的字符串替换* isEmpty() 判断是否为null 长度为0 返回为true 否则false* @author WHD**/public class Test1 {public static void main(String[] args) {String str1 = "abcdefg";System.out.println(str1.charAt(3));char[] charArray = str1.toCharArray();for (int i = 0; i < charArray.length; i++) {System.out.println(charArray[i]);}String str2 = "abcdefabcdef";System.out.println(str2.replace("a", "A"));String str3 = "1a1b1c1d1e1f";System.out.println(str3.replace("1", "hello"));String str4 = "1a2b3c4d5e6f";System.out.println(str4.replaceAll("\\d", "Hello"));String str5 = " sasa we rt v ty ytr vfd d ";System.out.println(str5.replace(" ", ""));String str6 = " ewewq ewqewq ewqewqewq vdssfdsgfsd cfdfsdw rerrew ";System.out.println(str6.replaceAll("\\s", ""));// 如果出现空指针异常// 表示肯定访问了一个指向null(空的)的引用 调属性或者方法String str7 = new String();System.out.println(str7.length());System.out.println(str7.isEmpty());}}
2.String类和包装类相关面试题
==和equals有什么区别? String 类直接使用=号赋值 将先从常量池中查找有没有同内容的变量 如果有 则直接使用已存在内容的地址 如果没有 将此内容存在常量池 完成赋值 这样做的好处是为了节省内存空间
package com.qfedu.test2;/*** ==和equals有什么区别?* String 类直接使用=号赋值 将先从常量池中查找有没有同内容的变量* 如果有 则直接使用已存在内容的地址* 如果没有 将此内容存在常量池 完成赋值** 这样做的好处是为了节省内存空间* @author WHD**/public class Test1 {public static void main(String[] args) {String str1 = "abc";String str2 = "abc";String str3 = "abc";String str4 = new String("abc");System.out.println(str1.equals(str2));System.out.println(str1 == str2);System.out.println(str1 == str3);System.out.println(str1 == str4);}}
==和equals有什么区别? 整型包装类和char包装类直接使用=号赋值 取值范围在byte以内 数值相同 ==比较为true 超过byte取值范围 ==比较为false 因为JDK的开发人员为了节省内存空间 将byte取值范围内的数值存放在一个缓存数组中 如果在byte取值 范围 那么将从数组中取出对应的值 如果不在byte取值范围 直接new一个新的对象
package com.qfedu.test2;/*** ==和equals有什么区别?* 整型包装类和char包装类直接使用=号赋值 取值范围在byte以内* 数值相同 ==比较为true* 超过byte取值范围 ==比较为false** 因为JDK的开发人员为了节省内存空间 将byte取值范围内的数值存放在一个缓存数组中* 如果在byte取值 范围 那么将从数组中取出对应的值* 如果不在byte取值范围 直接new一个新的对象* @author WHD**/public class Test2 {public static void main(String[] args) {System.out.println("int-----------------------------------");Integer i1 = 25;Integer i2 = 25;System.out.println(i1 == i2);Integer i3 = 200;Integer i4 = 200;System.out.println(i3 == i4);Integer i5 = 127;Integer i6 = 127;System.out.println(i5 == i6);Integer i7 = 128;Integer i8 = 128;System.out.println(i7 == i8);System.out.println("byte----------------------------------");Byte b1 = 20;Byte b2 = 20;System.out.println(b1 == b2);System.out.println("short---------------------------------");Short s1 = 120;Short s2 = 120;System.out.println(s1 == s2);Short s3 = 200;Short s4 = 200;System.out.println(s3 == s4);System.out.println("long------------------------------------");Long l1 = 20L;Long l2 = 20L;System.out.println(l1 == l2);Long l3 = 300L;Long l4 = 300L;System.out.println(l3 == l4);System.out.println("char-----------------------------------------");Character ch1 = 20;Character ch2 = 20;System.out.println(ch1 == ch2);Character ch3 = 200;Character ch4 = 200;System.out.println(ch3 == ch4);}}
3.StringBuffer&StringBuilder
String类是一个不可变对象 因为String类底层维护的是一个final修饰的char数组 任何对原字符串进行的增删改操作 都将产生一个新的字符串 所以 当我们需要频繁改变一个字符串的内容 推荐使用StringBuffer或者StringBuilder
面试题: StringBuffer StringBuilder 区别?
StringBuffer是线程安全的 JDK1.0 StringBuilder线程不安全 JDK1.5
package com.qfedu.test3;/*** String类是一个不可变对象 因为String类底层维护的是一个final修饰的char数组* 任何对原字符串进行的增删改操作 都将产生一个新的字符串* 所以 当我们需要频繁改变一个字符串的内容 推荐使用StringBuffer或者StringBuilder* 面试题:* StringBuffer StringBuilder 区别?* StringBuffer是线程安全的 JDK1.0* StringBuilder线程不安全 JDK1.5* @author WHD**/public class Test1 {public static void main(String[] args) {String str1 = "a";str1 += "b";System.out.println(str1);String condition = "笔记本";condition += "戴尔";condition += "至强铂金";condition += "32GB";condition += "1TB";condition += "2颗CPU";condition += 2;// ……System.out.println(condition);// StringBuffer sb = new StringBuffer();StringBuilder sb = new StringBuilder();sb.append(false);System.out.println(sb.length());sb.append("hello world");System.out.println(sb.length());System.out.println(sb.charAt(3));// sb = sb.delete(0, 5);System.out.println(sb.delete(0, 5));sb.insert(3, "666");System.out.println(sb);sb.replace(0, 3, "abcd");System.out.println(sb);sb.reverse();System.out.println(sb);}}
4.System类
System类 提供的有获取当前系统信息的一系列方法
package com.qfedu.test4;/*** System类 提供的有获取当前系统信息的一些列方法* @author WHD**/public class TestSystem {public static void main(String[] args) {System.out.println("普通hello world");System.err.println("特殊hello world");// 返回1970年元月1日0点0分0秒到目前的毫秒数// 1秒 等于 1000 毫秒 等于 10亿纳秒System.out.println(System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 365);System.out.println(System.getProperty("user.name"));System.out.println(System.getProperty("java.version"));System.out.println(System.getProperty("os.version"));System.out.println(System.getProperty("os.name"));System.getProperties().list(System.out);String str = "abc";// gc garbage collectionSystem.gc(); // 此时调用垃圾回收器 但不是真正立即回收垃圾System.out.println(str);}}
5.Runtime类
Runtime类提供了用于获取程序运行期间信息的方法
package com.qfedu.test4;import java.io.IOException;/*** Runtime类提供了用于获取程序运行期间信息的方法* @author WHD**/public class TestRuntime {public static void main(String[] args) throws IOException {System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 );System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);Runtime.getRuntime().gc();Runtime.getRuntime().exec("D:\\apps\\Git-2.21.0-64-bit.exe");}}
6.java.util.Date类
java.util.Date 日期类
package com.qfedu.test5;import java.text.SimpleDateFormat;import java.util.Date;/*** java.util.Date* 日期类* @author WHD**/public class TestDate {public static void main(String[] args) {Date date = new Date();System.out.println(date.getYear() + 1900 + "年");System.out.println(date.getMonth() + 1 + "月");System.out.println(date.getDay() + "一周第几天");System.out.println(date.getDate() + "一个月中第几天");System.out.println(date.getHours() + "小时");System.out.println(date.getMinutes() + "分钟");System.out.println(date.getSeconds() + "秒钟");System.out.println(date.toString());// yyyy/MM/dd HH:mm:ss 年月日 时分秒SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");System.out.println(sdf.format(date));// 传入毫秒数获取日期对象Date date1 = new Date(System.currentTimeMillis());// 根据日期对象获取毫秒数System.out.println(date1.getTime());}}
7.Calendar类
Calendar类 日历类 也提供了获取年月日时分秒方法 Calendar类不能new对象
package com.qfedu.test5;import java.util.Calendar;/*** Calendar类 日历类 也提供了获取年月日时分秒方法* Calendar类不能new对象* @author WHD**/public class TestCalendar {public static void main(String[] args) {Calendar instance = Calendar.getInstance();System.out.println(instance.get(Calendar.YEAR) + "年");System.out.println(instance.get(Calendar.MONTH) + 1 + "月");System.out.println(instance.get(Calendar.DAY_OF_MONTH) + "日");System.out.println(instance.get(Calendar.HOUR) + "时");System.out.println(instance.get(Calendar.HOUR_OF_DAY) + "时");System.out.println(instance.get(Calendar.MINUTE) + "分");System.out.println(instance.get(Calendar.SECOND) + "秒");}}
