java中的常见类:
目录
1、Object
2、Scanner
3、String
4、StringBuffer/StringBuilder
5、Arrays
6、Integer
7、Character
8、Pattern/Matcher
9、Match
10、Random
11、System
12、BigInteger
13、BigDecimal
14、Date
说起常见类,直到今天我才真正的意识到英语的重要性,话不多说,看这些常见了类,你认识几个吧。
- Object类的概述:
- Object是java.lang包下的类,故不需要导包。
- 类
Object
是类层次结构的根类。每个类都使用Object
作为超类。- 每个类都直接或者间接的继承自object类。
- Object类的构造方法:
- public Object()
- 子类的构造方法默认访问父类的无参构造方法,原因是他们共同的父亲只有一个无参构造。
- Object类的hashCode()方法:
- public int hashCode():返回该对象的哈希码值。
- Object类的getClass()方法:
- public final Class getClass():返回此
Object
的运行时类。- Class类的方法:
- public String getName():以String的形式返回此Class对象所表示的实体(类、接口、数组类、基本类型或void)名称。
- Object类的toString()方法:
- public String toString():返回该对象的字符串表示,该返回值没有意义,所以,建议所有子类都重写该方法。
- 其返回值相当于:getClass().getName()+'@'+Integer.toHexString(hashCode())。
- Object类的equals()方法:
- public boolean equals():指示其他某个对象是否与此对象“相等”。
- 该方法默认比较的是地址值。
- 比较地址值一般来说意义不大,所以我们要重写该方法,用来比较对象的成员变量值是否相等。
- Scanner类的概述:
- Scanner是java.util包下的类,故需要导包。
- 用于获取用户的键盘输入。
- Scanner类的构造方法:
- System类下有一个静态的字段:public static final InputStream in;标准输入流,对应着键盘录入。
- InputStream is = System.in;
- public Scanner(InputStream source)
- Scanner类的hasNextXxx()方法:
- public boolean Scanner类的hasNextXxx():判断是否是某种类型的元素。
- 返回的是boolean类型。
- Scanner类的NextXxx()方法:
- public Xxx Scanner类的NextXxx():获取该元素。
- 返回的是Xxx类型,可以是int、double、String等类型。
- String类的概述:
- String是java.lang包下的类,故不需要导包。
- 字符串是由多个字符组成的一串数据,也可以看成是字符数组。
- 字符串字面值"abc"也可以看成是一个字符对象,字符串是一个常量,一旦被赋值就不能被改变。
- String类的构造方法:
- public String()空构造。
- public String(byte[] bytes):把字节数组转成字符串
- public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串。
- public String(char[] value):把字符数组转成字符串
- public String(byte[] value,int index,int count):把字符数组的一部分转成字符串。
- public String(String original):把字符串常量值转成字符串。
- String类的判断方法:
- public boolean equals(Object obj):比较字符串的内容是否相同。
- public boolean equalsIgnoreCase(String str):比较字符串的内容师傅相同,忽略大小写。
- public boolean contains(String str):判断大字符串是否包含小字符串。
- public boolean startsWith(String str):判断字符串是否以某个指定的字符串开头。
- public boolean endWith(String str):判断字符串是否以某个指定的字符串结尾。
- boolean isEmpty():判断字符串是否为空。
- String类的获取功能:
- public int length():获取字符串的长度。
- public char charAt(int index):获取指定索引位置的字符。
- public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
- public int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引。
- public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
- public int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
- public String substring(int start):从指定位置开始截取字符串,默认到末尾。
- public String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
- String类的转换功能:
- public byte[] getBytes():把字符串转换为字节数组。
- public char[] toCharArray():把字符串转换为字符数组。
- public static String valueOf(char[] chs):把字符数组转换成字符串。
- public static String valueOf(int i):把int类型转换成字符串。
- public String toLowerCase():把字符串转换成小写。
- public String toUpperCase():把字符串转换成大写。
- public String concat(String str):把字符串拼接。
- String类的其他功能:
- public String replace(char old,char new):old字符被new字符替换。
- public String replace(String old,String new):old字符串被new字符串替换。
- public String trim():去除字符串两端空格。
- public int compareTo(String str):按字典顺序比较两字符串,区分大小写。
- public int compareToIgnoreCase(String str):按字典顺序比较两字符串,不区分大小写
- StringBuffer类的概述:
- StringBuffer是java.lang包下的类,故不需要导包。
- 线程安全的可变字符串,与String相比,该类做字符串拼接时不会浪费太多的资源。
- StringBuffer类的构造方法:
- public StringBuffer():无参构造方法。
- public StringBuffer(int capacity):指定容量的字符串缓冲区对象。
- public StringBuffer(String str):指定字符串内容的字符串缓冲区对象。
- StringBuffer类的方法:
- public int capacity():返回当前容量。理论值
- public int length():返回长度(字符数)。实际值
- StringBuffer类的添加功能:
- public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区。并返回字符串缓冲区本身。
- public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区,并返回字符串缓冲区本身。
- StringBuffer类的删除功能:
- public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身。
- public stringBuffer delete(int start,int end):删除指定位置开始指定位置结束的内容,并返回本身。
- StringBuffer类的替换功能:
- public StringBuffer replace(int start,int end,String str):从start开始到end结束用str替换,并返回本身。
- StringBuffer类的反转功能:
- public StringBuffer reverse():将字符串反转,并返回本身。
- StringBuffer类的截取功能,返回的是字符串:
- public String substring(int start):
- public String substring(int start,int end):
- Arrays类的概述:
- Arrays是java.util包下的类,故需要导包。
- Arrays:针对数组进行操作的工具类,提供了排序,查找等功能。
- Arrays类的成员方法:
- public static String toString(int[] a):把数组转成字符串。
- public static void sort(int[] a):对数组进行排序。
- public static int binarySearch(int[] a,int key):二分查找。
- Integer类的概述:
- Integer是java.lang包下的类,故不需要导包。
- 用于基本数据类型与字符串之间的转化。
- Integer类的构造方法:
- public Integer(int value)
- public Integer(String s)
- Integer类的成员方法:
- String.valueOf(number):int -- String
- Integer.parseInt(s):String -- int
- public int intValue()
- public static int parseInt(String s)
- public static String toString(int i)
- public static Integer valueOf(int i)
- public static Integer valueOf(String s)
- public static String toBinaryString(int i):十进制转化成二进制
- public static String toOctalString(int i):十进制转化成八进制
- public static String toHexString(int i):十进制转化成十六进制
- public static int parseInteger toString(int i,int radix)其他进制转化成十进制
- Character类的概述:
- Character是java.lang包下的类,故不需要导包。
- 该类在对象中包装一个基本类型char的值,此外还提供了几种方法,以确定字符的类别,并将字符大小写转化。
- Character类的构造方法:
- Character(char value)
- Character类的成员方法:
- public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
- public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
- public static boolean isDigit(char ch):判断给定的字符是否是数字字符
- public static char toUpperCase(char ch):把给定的字符转为大写字符
- public static char toLowerCase(char ch):把给定的字符转为小写字符
- Pattern类的概述:
- Pattern是java.util.regex包下的类,故需要导包。
- 正则表达式:符合一定规则的字符串。
- 字符:
- x 字符 x。举例:'a'表示字符a。
- \\ 反斜线字符。
- \n 换行符。
- \r 回车符。
- 字符类:
- [abc] a、b或c(简单类)。
- [^abc] 任何字符,除了a、b或c。
- [a-zA-Z] a到z或A到Z,两头的字母包括在内。
- [0-9] 0到9的字符都包括。
- 预定字符类:
- . 任何字符。
- \d 数字:[0-9]。
- \w 单词字符:[a-zA-Z_0-9]。
- 边界匹配器:
- ^ 行的开头。
- $ 行的结尾
- \b 单词边界
- 正则表达式的应用:
- public boolean matches(String regex):判断功能。
- public String[] split(String regex):分割功能。
- public StringreplaceAll(String regex,String replacement):替换功能。
- Pattern和Matcher类的使用:获取功能。
- Math类的概述:
- Math是java.lang包下的类,故不需要导包。
- Math类包含用于执行基本数学运算的方法。
- Math类的成员方法:
- public static int abs(int a):绝对值
- public static int double ceil(double a):向上取整
- public static double floor(double a):向下取整
- public static int max(int a,int b):最大值
- public static int min(int a,int b):最小值
- public static double pow(double a,double b):a的b次幂
- public static double(random):随机数[0.0,1.0)
- public static int round(float a):四舍五入
- public static int round(double a):四舍五入
- public static double sqrt(double a):正平方根
- Random类的概述:
- Random是java.util包下的类,故需要导包。
- 用于产生随机数
- Random类的构造方法:
- public Random():没有给种子,用的是默认的种子,是当前时间的毫秒值。
- public Random(long seed):给出指定的种子。
- Random类的成员方法:
- public int nextInt():返回的是int范围内的随机数。
- public int nextInt(int n):返回的是[0,n)范围内的随机数。
- System类的概述:
- System是java.lang包下的类,故不需要导包。
- 系统类,提供一些有用的字段和方法,不能被实例化。
- System类的成员方法:
- public static void gc():运行垃圾回收器
- public static void exit(int status):终止当前正在运行的Java虚拟机。
- public static long currentTimeMillis():返回以毫秒为单位的当前时间
- public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length):从指定源数组中复制一个数组,复制从指定的位置开始,当目标数组的指定位置结束。
- BigInteger类的概述:
- BigInteger是java.math包下的类,故需要导包。
- 该类可以让超过Integer范围内的数据进行运输。
- BigInteger类的构造方法:
- public BigInteger(String val)
- BigInteger类的成员方法:
- public BigInteger add(BigInteger val):加
- public BigInteger subtract(BigInteger val):减
- public BigInteger multiply(BigInteger val):乘
- public BigInteger divide(BigInteger val):除
- public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组。
- BigDecimal类的概述:
- BigDecimal是java.math包下的类,故需要导包。
- 由于在运算的时候,float和double很容易丢失精度,所以引入了该类
- BigDecimal类的构造方法:
- public Bigdecimal(String val)
- BigDecimal类的成员方法:
- public Bigdecimal add(BigInteger augend):加
- public Bigdecimalsubtract(BigInteger subtrahend):减
- public Bigdecimalmultiply(BigInteger multiplicand):乘
- public Bigdecimal divide(BigInteger divisor):除
- public Bigdecimal divide(Bigdecimal divisor,int scale,int roundingMode):商几位小数,如何取舍
- Date类的概述:
- Date是java.util包下的类,故需要导包。
- 表示特定的瞬间,精确到毫秒
- Date类的构造方法:
- Date():根据当前的毫秒值创建日期对象。
- Date(long date):根据给定的毫秒值创建日期对象。
- Date类的成员方法:
- public long getTime():获取时间,以毫秒为单位。
- public void setTime(long time):设置时间。
- getTime()从Date得到一个毫秒值。
- setTime(long time):把一个毫秒值转换为date的构造方法。