介绍一下几个java中经常用到的类
1. String类
1.1 String 类的常用方法
- trim():去除前后空格的 trim() 方法
- split(String regex): 把字符串拆分成字符串数组
- equals(): 比较字符串内容是否相等必须使用该方法, 而不能直接使用 ==
2. 关于字符串缓冲池: 直接通过 = 为字符串赋值, 会先在缓冲池中查找有没有一样的字符串,
若果有就把那个引用赋给字符串变量, 否则, 会创建一个新的字符串, 并把对应的字符串放入到缓冲池中.
2.StringBuilder、StringBuffer 类
public class StringTest { /** * 1. append() 方法: 把字符串加入到以后的字符序列的后面 * 注意: append() 方法的返回值还是当前的 StringBuffer 对象. 可以使用方法的连缀(只要调用一个方法,这个方法的返回值是调用者本身,就可以实现方法的连缀,这里指连续使用append). * * 2. StringBuilder VS StringBuffer: * StringBuilder 是线程不安全的, 效率更高. 所以更多的时候使用 StringBuilder * StringBuffer 是线程安全的, 效率偏低, 在多线程的情况下下使用. */ @Test public void testAppend(){ StringBuilder stringBuilder = new StringBuilder(); // ... = new StringBuilder(5); 如果预置一个容量,当超出这个容量时,会继续加入 stringBuilder.append("<html>") .append( "<body>") .append( "</body>") .append("</html>"); System.out.println(stringBuilder); } @Test public void testStringBuilder() { StringBuffer stringBuffer = new StringBuffer("abcde"); System.out.println(stringBuffer); stringBuffer.replace(1, 3, "mvp"); System.out.println(stringBuffer); } }
3.Data,DateFormat类
DateFormat: 把日期对象格式化为一个字符串
public SimpleDateFormat(String pattern);
该构造方法可以用 参数pattern 指定的格式创建一个对象,该对象调用:
/** * Date() 封装了时间和日期. * * DateFormat: 把日期对象格式化为一个字符串 & 把一个字符串转为一个 Date 对象 * 1. DateFormat: 是一个抽象类. * 抽象类获取对象的方式: * 1). 创建其子类对象 * 2). 有的抽象类中提供了静态工厂方法来获取抽象类的实例. */ public class DateTest { //创建其子类对象 //这种样式是自定义的(多用) @Test public void testSimpleDateFormat() throws Exception{ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println(dateFormat.format(date)); //字符串转化为Date,字符串形式必须是dateFormat的格式 String dateStr = "1990-12-12 12:12:12"; Date date2 = dateFormat.parse(dateStr); System.out.println(date2); } //提供了静态工厂方法来获取抽象类的实例,可以不用new //这种样式只能是系统指定的,系统提供的样式 @Test public void testDateFormat() throws Exception{ DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); Date date = new Date(); String dateStr = dateFormat.format(date); System.out.println(dateStr); //字符串转化为Date,字符串形式必须是dateFormat的格式 dateStr = "2013年10月10日 下午08时08分08秒"; Date date2 = dateFormat.parse(dateStr); System.out.println(date2); } @Test public void testDate() { Date date = new Date(); System.out.println(date); } }
4. Random,Math类
import java.util.Random; import org.junit.Test; import static java.lang.Math.*; /** * Random 中封装了随机相关的方法: 返回随机的基本数据类型的值 * Math: 中封装了常用的数学方法. * 静态导入. 基本语法: * import static java.lang.Math.*; * 导入指定类的静态属性和静态方法,意思就是把该命名空间的静态属性和静态方法都导入到当前类中. * 缺点是分不清某个属性或者方法是导入的还是类中定义的/ public class RandomTest { @Test public void testMath(){ System.out.println(sin(PI/3));//使用了静态导入 System.out.println(Math.sin(Math.PI/3));//没有使用静态导入 } @Test public void testRandom() { Random random = new Random(); // System.out.println(random.nextInt()); System.out.println(random.nextInt(10)); } }
总结
1). String 是一个不可变的字符序列!
2). StringBuffer, StringBuilder 是可变的字符序列.
> StringBuffer 是线程安全的, 所以效率较低.
> StringBuilder 是线程不安全的, 效率较高. 大部分时使用 StringBuilder.
3). Date: 封装了时间和日期.
4). DateFormat
-SimpleDateFormat
如果需要把 Date 类型的对象格式化为一个字符串, 或把一个字符串转为一个 Date 对象 则使用 DateFormat.
//1. 调用静态工厂方法来获取 DateFormat 对象.
//传入的参数是日期或时间的样式.
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
Date date = new Date();
//格式化日期对象的 format 方法
String dateStr = dateFormat.format(date);
System.out.println(dateStr);
dateStr = "2013年6月10日 下午03时48分06秒";
//解析字符串到日期对象的 parse 方法.
Date date2 = dateFormat.parse(dateStr);
System.out.println(date2);
//创建 SimpleDateFormat 对象.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
5). Random & Math