java常用类

介绍一下几个java中经常用到的类

  •String 类
  •StringBuffer、StringBuilder 类
  •Date 类
  •DateFormat 类
  •Random、Math 等
 

1. String类

1.1 String 类的常用方法

  •public int indexOf (String s):从当前字符串的头开始检索字符串 s,并返回首次出现 s 的位置
  •indexOf(String s ,int startpoint) 求指定字符的索引
  •lastIndexOf (String s)
  •public String substring(int startpoint):获得一个当前字符串的子串  
  •substring(int start ,int end)
  •public String trim(): 得到一个 去掉前后空格后 的字符串对象
  • trim():去除前后空格的 trim() 方法 
  • split(String regex): 把字符串拆分成字符串数组
  • equals(): 比较字符串内容是否相等必须使用该方法, 而不能直接使用 ==
 
1.2 字符串与基本数据的相互转化
  •public static int parseInt(String s):可以将由“数字”字符组成的字符串
  •类似地,使用java.lang包中的 Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符 组成的字符串,转化为相应的 基本数据类型。
 
1.3字符串与字符数组
  •String 类的构造方法:String(char[]) 和 String(char[],int offset,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象
  •String类提供了将字符串存放到数组中的方法:
    –public void getChars(int start,int end,char c[],int offset )
  •将字符串中的全部字符存放在一个字符数组中的方法:
    –public char[] toCharArray()                       
 
1.4字符串与字节数组
  •String(byte[])用指定的字节数组构造一个字符串对象。String(byte[],int offset,int length) 用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。
  •public byte[] getBytes() 方法使用平台默认的字符编码,将当前字符串转化为一个字节数组。
  •public byte[] getBytes(String charsetName) 使用参数指定字符编码,将当前字符串转化为一个字节数组。
 
总结:
   1. String 是不可变的字符序列!
   2. 关于字符串缓冲池: 直接通过 = 为字符串赋值, 会先在缓冲池中查找有没有一样的字符串,
      若果有就把那个引用赋给字符串变量, 否则, 会创建一个新的字符串, 并把对应的字符串放入到缓冲池中.
  •String使用陷阱:
    – string s="a"; //创建了一个字符串 s=s+"b"; //实际上原来的"a"字符串对象已经丢弃了,现在又产生了一个字符串s+"b"(也就是"ab")。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。
 

2.StringBuilder、StringBuffer 类

  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类

   Date() 封装了时间和日期.
   DateFormat: 把日期对象格式化为一个字符串 
              把一个字符串转为一个 Date 对象
 
  •Date类在java.util包中。使用Date类的无参数构造方法创建的对象可以获取本地当前时间。
  •SimpleDateFormat来实现日期的格式化。

      public SimpleDateFormat(String pattern);

      该构造方法可以用 参数pattern 指定的格式创建一个对象,该对象调用:

  •public String format(Date date):方法格式化时间对象date
 
/**
 * 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 

原文地址:https://www.cnblogs.com/tech-bird/p/3526983.html