JAVASE常用类总结

使用API中的如下常用类或者其他类是,要注意:看返回类型(确定定义什么类型)、看参数(确定要传入什么参数)、看构造方法(确定如何构造)

1.Object类

  (1)Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类。

  (2)Object类的构造方法有一个,并且是无参构造,这其实就是理解当时我们说过,子类构造方法默认访问父类的构造是无参构造。

  (3)要掌握的方法:

    A:toString()      返回对象的字符串表示,默认是由类的全路径+'@'+哈希值的十六进制表示。这个表示其实是没有意义的,一般子类都会重写该方法。如何重写呢?过程我也讲解过了,基本上就是要求信息简单明了。但是最终还是自动生成。

    B:equals()        比较两个对象是否相同。默认情况下,比较的是地址值是否相同。而比较地址值是没有意义的,所以,一般子类也会重写该方法。重写过程,我也详细的讲解和分析了。但是最终还是自动生成。

  (4)要了解的方法:

    A:hashCode()   返回对象的哈希值。不是实际地址值,可以理解为地址值。

    B:getClass()     返回对象的字节码文件对象,反射中我们会详细讲解

    C:finalize()        用于垃圾回收,在不确定的时间

    D:clone()          可以实现对象的克隆,包括成员变量的数据复制,但是它和两个引用指向同一个对象是有区别的。

2.Scanner类

  (1)在JDK5以后出现的用于键盘录入数据的类。

  (2)构造方法:

    A:讲解了System.in这个东西。它其实是标准的输入流,对应于键盘录入。

    B:构造方法 InputStream is = System.in,Scanner(InputStream is)。

    C:常用的格式:Scanner sc = new Scanner(System.in)。

  (3)基本方法格式:

    A:hasNextXxx() 判断是否是某种类型的。

    B:nextXxx() 返回某种类型的元素。

  (4)要掌握的两个方法:

    A:public int nextInt()

    B:public String nextLine()

  (5)需要注意的小问题:

    A:同一个Scanner对象,先获取数值,再获取字符串会出现一个小问题。

    B:解决方案:a:重新定义一个Scanner对象;b:把所有的数据都用字符串获取,然后再进行相应的转换。

3.String类

  (1)多个字符组成的一串数据。其实它可以和字符数组进行相互转换。

  (2)构造方法:

    A:public String()

    B:public String(byte[] bytes)

    C:public String(byte[] bytes,int offset,int length)

    D:public String(char[] value)

    E:public String(char[] value,int offset,int count)

    F:public String(String original)

    下面的这一个虽然不是构造方法,但是结果也是一个字符串对象   G:String s = "hello";

  (3)字符串的特点:

    A:字符串一旦被赋值,就不能改变。注意:这里指的是字符串的内容不能改变,而不是引用不能改变。

    B:字面值作为字符串对象和通过构造方法创建对象的不同,String s = new String("hello");和String s = "hello"的区别。

  (4)字符串的功能:

    A:判断功能

      boolean equals(Object obj)

      boolean equalsIgnoreCase(String str)

      boolean contains(String str)

      boolean startsWith(String str)

      boolean endsWith(String str)

      boolean isEmpty()

    B:获取功能

      int length()

      char charAt(int index)

      int indexOf(int ch)

      int indexOf(String str)

      int indexOf(int ch,int fromIndex)

      int indexOf(String str,int fromIndex)

      String substring(int start)

      String substring(int start,int end)

    C:转换功能

      byte[] getBytes()

      char[] toCharArray()

      static String valueOf(char[] chs)

      static String valueOf(int i)

      String toLowerCase()

      String toUpperCase()

      String concat(String str)

    D:其他功能

      a:替换功能 

        String replace(char old,char new)

        String replace(String old,String new)

      b:去空格功能

        String trim()

      c:按字典比较功能

        int compareTo(String str)

        int compareToIgnoreCase(String str)

4.StringBuffer类

  (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了一个字符串缓冲区类。StringBuffer供我们使用。

  (2)StringBuffer的构造方法:

    A:StringBuffer()

    B:StringBuffer(int size)

    C:StringBuffer(String str)

  (3)StringBuffer的常见功能

    A:添加功能

      a.public StringBuffer append(String str)

      b.public StringBuffer insert(int offset,String str)

    B:删除功能

      a.public StringBuffer deleteCharAt(int index)

      b.public StringBuffer delete(int start,int end)

    C:替换功能

      a.public StringBuffer repalce(int start,int end,String str)

    D:反转功能

      a.public StringBuffer reverse()

    E:截取功能(注意这个返回值)

      a.public String substring(int start)

      b.public String substring(int start,int end)

5.StringBuilder类(存在一些差别,但可以将StringBuffer处替换为StringBuilder)

6.Arrays类

  A.public static String toString(int[] a)

  B.public static void  sort(int[] a)    采用的是快排序

  c.public static int binarySearch(int[] a,int key)

7.基本类的封装类(这里讲了Integer类和Character类)

  (1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型

    byte           Byte

    short          Short

    int              Integer

    long           Long

    float           Float

    double       Double

    char           Character

    boolean     Boolean

  (2)Integer的构造方法

    A:Integer i = new Integer(100);

    B:Integer i = new Integer("100");注意:这里的字符串必须是由数字字符组成

  (3)String和int的相互转换

    A:String -- int         Integer.parseInt("100");

    B:int -- String    String.valueOf(100);

  (4)其他的功能(了解)        

    进制转换

  (5)Character构造方法

    Character ch = new Character('a');

  (6)要掌握的方法

    A.public static boolean isUpperCase(char ch)

    B.public static boolean isLowerCase(char ch)

    C.public static boolean isDigit(char ch)

    D.public static char toUpperCase(char ch)

    E.public static char toLowerCase(char ch)

8.正则表达式

  (1)就是符合一定规则的字符串

  (2)常见规则

    A:字符
      x 字符 x。举例:'a'表示字符a
      \ 反斜线字符。
       新行(换行)符 ('u000A')
       回车符 ('u000D')

    B:字符类

      [abc] a、b 或 c(简单类)

      [^abc] 任何字符,除了 a、b 或 c(否定)

      [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)

      [0-9] 0到9的字符都包括

    C:预定义字符类

      . 任何字符。我的就是.字符本身,怎么表示呢? .

      d 数字:[0-9]

      w 单词字符:[a-zA-Z_0-9]

      在正则表达式里面组成单词的东西必须有这些东西组成

    D:边界匹配器

      ^ 行的开头

      $ 行的结尾

       单词边界  

         就是不是单词字符的地方。

          举例:hello world?haha;xixi

    E:Greedy 数量词 

      X? X,一次或一次也没有

      X* X,零次或多次

      X+ X,一次或多次

      X{n} X,恰好 n 次 

      X{n,} X,至少 n 次 

      X{n,m} X,至少 n 次,但是不超过 m 次 

  (3)常见功能:(分别用的是谁呢?)

    A:判断功能

      String类的public boolean matches(String regex)

    B:分割功能

      String类的public String[] split(String regex)

    C:替换功能

      String类的public String replaceAll(String regex,String replacement)

    D:获取功能

      Pattern和Matcher

      Pattern p = Pattern.compile("a*b");  

      Matcher m = p.matcher("aaaaab");

      find():查找存不存在

      group():获取刚才查找过的数据

9.Math类

  (1)针对数学运算进行操作的类

  (2)常见方法(自己补齐)

    A:绝对值

      public static int abs(int a)

    B:向上取整

      public static double ceil(double a)

    C:向下取整

      public static double floor(double a)

    D:两个数据中的大值

      public static int max(int a,int b)

    E:a的b次幂

      public static double pow(double a,double b)

    F:随机数

      public static double random()

    G:四舍五入

      public static int round(int a)

    H:正平方根

      public static double sqrt(double a)

10.Random类

  (1)用于产生随机数的类

  (2)构造方法:

    A:Random() 默认种子,每次产生的随机数不同

    B:Random(long seed) 指定种子,每次种子相同,随机数就相同

  (3)成员方法:

    A:int nextInt() 返回int范围内的随机数

    B:int nextInt(int n) 返回[0,n)范围内的随机数

11.System类

  (1)系统类,提供了一些有用的字段和方法

  (2)成员方法

    A:运行垃圾回收器

      public static void gc()

    B:退出jvm

      public static void exit(int status)

    C:获取当前时间的毫秒值

      public static long currentTimeMillis()

    D:数组复制

      public static void arraycopy(Object src,int srcPos,Object dest, int destPos,int length)

12.BigInteger

  (1)针对大整数的运算

  (2)构造方法 

    A:BigInteger(String s)

  (3)成员方法(自己补齐)

    A:加

      public BigInteger add(BigInteger val)

    B:减

      public BigInteger subtract(BigInteger val)

    C:乘

      public BigInteger multiply(BigInteger val)

    D:除

      public BigInteger divide(BigInteger val)

    E:商和余数

      public BigInteger[] divideAndRemainder(BigInteger val)

13.BigDecimal类

  (1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)

  (2)构造方法

    A:BigDecimal(String s)

  (3)成员方法:

    A:加

      public BigDecimal add(BigDecimal augend)

    B:减

      public BigDecimal substract(BigDecimal subtrahend)

    C:乘

      public BigDecimal multiply(BigDecimal multiplicand)

    D:除

      public BigDecimal divide(BigDecimal divisor)

    E:自己保留小数几位

      public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

14.Date/DateFormat类

  (1)Date是日期类,可以精确到毫秒。

    A:构造方法

      Date()

      Date(long time)

    B:成员方法

      getTime()

      setTime(long time)

    C:日期和毫秒值的相互转换

  (2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat

    A:SimpleDateFormat(String pattern) 给定模式yyyy-MM-dd HH:mm:ss

    B:日期和字符串的转换

      a:Date -- String

        format()

      b:String -- Date

        parse()

15.Calendar类

  (1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。

  (2)如何得到一个日历对象呢?

    Calendar rightNow = Calendar.getInstance();本质返回的是子类对象

  (3)成员方法

    A:根据日历字段得到对应的值

      public static Calendar getlnstance()

    B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值

      public void add(int field,int amount)

    C:设置日历对象的年月日

      public final void set(int year,int month,int date)

16.Collection/Map(接口)

  Collection

    |--List

      |--ArrayList

      |--Vector

      |--LinkedList

    |--Set

      |--HashSet

      |--TreeSet

  (1)Collection的功能概述

    A:添加功能

      boolean add(E e)

      boolean addAll(Collection c)

    B:删除功能

      boolean remove(Object o)

      boolean removeAll(Collection c)

    C:判断功能

      boolean contains(Object o)

      boolean containsAll(Collection c)

    D:获取功能

    E:长度功能

      int size()

    F:交集(了解)

    G:把集合转数组(了解)

      Object[] toArray()

  Map

    HashMap

    TreeMap

  (1)Map的功能概述

    A:添加功能

      V put(K key,V value)

    B:删除功能

      V remove(Object key)

      void clean()

    C:判断功能

      boolean containsKey(Object obj)

      boolean containsValue(Object value)

      boolean isEmpty()

    D:获取功能

      V get(Object obj)

    E:长度功能

      int size()

  集合总结:

    Collection(单列集合)

      List(有序,可重复)

        ArrayList  底层数据结构是数组,查询快,增删慢 ,线程不安全,效率高

        Vector    底层数据结构是数组,查询快,增删慢 ,线程安全,效率低

        LinkedList  底层数据结构是链表,查询慢,增删快 ,线程不安全,效率高

      Set(无序,唯一)

        HashSet 底层数据结构是哈希表。哈希表依赖两个方法:hashCode()和equals()。

          执行顺序:首先判断hashCode()值是否相同

            是:继续执行equals(),看其返回值

              是true:说明元素重复,不添加

              是false:就直接添加到集合

            否:就直接添加到集合

          最终的方法:自动生成hashCode()和equals()即可

        LinkedHashSet 底层数据结构由链表和哈希表组成:由链表保证元素有序;由哈希表保证元素唯一。

        TreeSet 底层数据结构是红黑树。(是一种自平衡的二叉树)

          如何保证元素唯一性呢?根据比较的返回值是否是0来决定

          如何保证元素的排序呢?两种方式

            自然排序(元素具备比较性)  让元素所属的类实现Comparable接口

            比较器排序(集合具备比较性)  让集合接收一个Comparator的实现类对象

    Map(双列集合)

      A:Map集合的数据结构仅仅针对键有效,与值无关。

      B:存储的是键值对形式的元素,键唯一,值可重复。

      HashMap 底层数据结构是哈希表。线程不安全,效率高。哈希表依赖两个方法:hashCode()和equals()

          执行顺序:首先判断hashCode()值是否相同

            是:继续执行equals(),看其返回值

              是true:说明元素重复,不添加

              是false:就直接添加到集合

            否:就直接添加到集合

          最终的方法:自动生成hashCode()和equals()即可

      LinkedHashMap  底层数据结构由链表和哈希表组成:由链表保证元素有序;由哈希表保证元素唯一。

      Hashtable  底层数据结构是哈希表。线程安全,效率低。哈希表依赖两个方法:hashCode()和equals()
          执行顺序:首先判断hashCode()值是否相同

            是:继续执行equals(),看其返回值

              是true:说明元素重复,不添加

              是false:就直接添加到集合

            否:就直接添加到集合

          最终的方法:自动生成hashCode()和equals()即可

      TreeMap  底层数据结构是红黑树。(是一种自平衡的二叉树)

          如何保证元素唯一性呢?根据比较的返回值是否是0来决定

          如何保证元素的排序呢?两种方式

            自然排序(元素具备比较性)  让元素所属的类实现Comparable接口

            比较器排序(集合具备比较性)  让集合接收一个Comparator的实现类对象

  到底使用那种集合

    看需求:是否是键值对象形式:

      是:Map

        键是否需要排序:

          是:TreeMap

          否:HashMap

          不知道,就使用HashMap。

      否:Collection

        元素是否唯一:

          是:Set

            元素是否需要排序:

              是:TreeSet

              否:HashSet 

              不知道,就使用HashSet

          否:List

            要安全吗:

              是:Vector(其实我们也不用它,后面我们讲解了多线程以后,我在给你回顾用谁)

              否:ArrayList或者LinkedList

                增删多:LinkedList

                查询多:ArrayList

                不知道,就使用ArrayList

              不知道,就使用ArrayList

  集合的常见方法及遍历方式

    Collection:

      add()

      remove()

      contains()

      iterator()

      size()

    遍历:增强for迭代器

      |--List

        get()

        遍历:普通for

      |--Set

    Map:

      put()

      remove()

      containskey(),containsValue()

      keySet()

      get()

      value() 

      entrySet()

      size()

      遍历:根据键找值;根据键值对对象分别找键和值

 

17.Collections类

  Collections成员方法

    A:public static <T> void sort(List<T> list)

    B:public static <T> int binarySearch(List<?> list,T key)

    C:public static <T> T max(Collection<?> coll)

    D:public static void reverse(List<?> list)

    E:public static void shuffle(List<?> list)

18.IO流

  (1)异常

    Throwable

      |--Error 严重问题,我们不处理。

      |--Exception

        |--RuntimeException 运行期异常,我们需要修正代码

        |--非RuntimeException 编译期异常,必须处理的,否则程序编译不通过

  (2)异常处理

    A:JVM的默认处理

      把异常的名称,原因,位置等信息输出在控制台,但是呢程序不能继续执行了。

    B:自己处理

      a:try...catch...finally 自己编写处理代码,后面的程序可以继续执行

      b:throws  把自己处理不了的,在方法上声明,告诉调用者,这里有问题

  (3)File操作

    A.构造方法:

      File file = new File("e:\demo\a.txt");

      File file = new File("e:\demo","a.txt");

      File file = new File("e:\demo");

      File file2 = new File(file,"a.txt");

    B.File类的功能

      创建功能

        public boolean createNewFile()

        public boolean mkdir()

        public boolean mkdirs()

      删除功能

        public boolean delete()

      重命名功能

        public boolean renameTo(File dest)

      判断功能

        public boolean isDirectory()

        public boolean isFile()

        public boolean exists()

        public boolean canRead()

        public boolean canWrite()

        public boolean isHidden()

      获取功能

        public String getAbsolutePath()

        public String getPath()

        public String getName()

        public long length()

        public long lastModified()

      高级获取功能

        public String[] list()

        public File[] listFiles()

      过滤器功能

        public String[] list(FilenameFilter filter)

        public File[] listFiles(FilenameFilter filter)

  (3)IO流

    |--字节流
      |--字节输入流
        InputStream
          int read():一次读取一个字节
          int read(byte[] bys):一次读取一个字节数组

          |--FileInputStream
          |--BufferedInputStream
      |--字节输出流
        OutputStream
          void write(int by):一次写一个字节
          void write(byte[] bys,int index,int len):一次写一个字节数组的一部分
          |--FileOutputStream
          |--BufferedOutputStream
    |--字符流
      |--字符输入流
        Reader
          int read():一次读取一个字符
          int read(char[] chs):一次读取一个字符数组

          |--InputStreamReader
            |--FileReader
          |--BufferedReader
            String readLine():一次读取一个字符串
      |--字符输出流
        Writer
          void write(int ch):一次写一个字符
          void write(char[] chs,int index,int len):一次写一个字符数组的一部分

          |--OutputStreamWriter
            |--FileWriter
          |--BufferedWriter
            void newLine():写一个换行符
            void write(String line):一次写一个字符串

20.多线程

19.GUI

20.网络编程

原文地址:https://www.cnblogs.com/yunkaiL/p/9338493.html