文章目录
(一)JAVA API 文档的使用
Java API下载(密码:rsk8)
JAVA API线上文档入口
1.前言
掌握了基本的JAVA面向对象后,学会读API文档JAVA的很重要,就像读一本英语词典,而且你不能只是遇到问题才会去查,对于初学者更要熟悉整个API文档的结构,以宏观的角度去查阅文档和学习JAVA,这样也更有助于理解和使用JAVA中各种工具类;
由于本人水平有限,是JAVA初学者,还请各位及时指正
2.目录总结
在文档中还可以产看类的构造函数,方法(静态,非静态),字段,变量等
包名介绍
- 以 java.* 开头的是Java的核心包,所有程序都会使用这些包中的类;
- 以 javax.* 开头的是扩展包,x 是 extension 的意思,也就是扩展。虽然 javax.* 是对 java.* 的优化和扩展,但是由于 javax.* 使用的越来越多,很多程序都依赖于 javax.,所以 javax. 也是核心的一部分了,也随JDK一起发布。
- 以 org.* 开头的是各个机构或组织发布的包,因为这些组织很有影响力,它们的代码质量很高,所以也将它们开发的部分常用的类随JDK一起发布。
- 还有以com. 开头的包,是盈利性公司开发出的,使用时会有版权问题
推荐文章:Java类-Java常用类库详解
应用举例
对于异常时,正确用法的查询
Boolean b = new boolean[];
//此处报错Array initializer expected
当然,API文档在JAVA中起到字典的作用,用处不言而喻
(二)常用工具类
工具类 | 重点掌握 |
---|---|
1. 数据类型操作 | 字符串与可变字符串 |
2.字符串操作 | 日期类型转换与格式化 |
3.数学运算 | 自定义异常 |
5.时间日期处理 | 输入流与输出流的实现 |
6.文件处理 | 线程控制,并发与锁 |
7.流处理 | |
8.多线程操作 |
1.数据类型操作
(1)Short
Short是short 的包装类,常用方法如下及构造方法如下:
构造函数
-
Short(short value) 通过指定的short构造一个Short对象。
-
Short(String s) 通过指定的String构造一个Short对象。
方法
-
byte byteValue() 将此对象转化为byte。
-
double doubleValue() 将此对象转化为double。
-
float floatValue() 将此对象转化为float。
-
int intValue() 将此对象转化为int。
-
long longValue() 将此对象转化为long。
-
short shortValue() 将此对象转化为short。
-
boolean equals(Object obj)
将此对象与指定的对象进行比较。
package com.demo4_2;
public class Test04 {
public static void main(String[] args) {
Short S = 10;//自动装箱
Byte B= S.byteValue();
System.out.println(B.getClass());
System.out.println(B.equals(S));
//输出class java.lang.Byte
//false
}
}
- int hashCode() 返回此哈希码Short; 等于调用的结果intValue()。
- int compareTo(Short anotherShort) 用Short数字比较两个对象。
Short S = 10;//自动装箱
Short S2 = 14;
System.out.println(S.compareTo(S2));
//输出-4
- String toString() 返回String表示此Short值的对象
Short S = 10;//自动装箱 System.out.println(S.toString()); //输出10
静态方法
- static int toUnsignedInt(short x) 通过无符号转换将参数转换为int。
- static long toUnsignedLong(short x) 通过无符号转换将参数转换为long 。
- static Short valueOf(short s) 将short转化为Short。
- static Short valueOf(String s) 将String转化为Short。
String str ="1";
Short S = Short.valueOf(str);
System.out.println(S.getClass()+" "+S);
//输出 class java.lang.Short 1
- static Short valueOf(String s, int radix)
返回一个Short对象,该对象保存String使用第二个参数给出的基数(计算进制)解析时从指定的值中提取的值。 - static short parseShort(String s) 将字符串参数解析为带符号的short。
- static short parseShort(String s, int radix)
将字符串参数解析为short,第二个参数指定计算进制 。 - static short reverseBytes(short i) 返回通过反转指定short值的二进制补码的值。
- static Short decode(String nm)
将String解码为Short。(String可以为10进制、16进制。8进制) - static int compare(short x, short y) 以short数字方式比较两个值。
short s1 = 1;
short s2 = 10;
int i = Short.compare(s1,s2);
System.out.println(i);
//输出-9
- static int hashCode(short value) 返回short值的哈希码; 兼容 Short.hashCode()。
- static String toString(short s) 将指定的short转化为String。
short s = 1;
String str = Short.toString(s);
System.out.println(str);
//输出1
(2)Integer
Integer 是int的包装类
构造函数
- Integer(int value) 通过指定的int值构成一个Integer对象。
- Integer(String s) 通过指定的String值构成一个Integer对象。。
public static void main(String[] args) {
Integer In = new Integer(10);
System.out.println(In);
Integer Str = new Integer("12345");
System.out.println(Str);
//输出10
//12345
}
方法
- int intValue() 将此对象转化为int。
- long longValue() 将此对象转化为long。
- byte byteValue() 将此对象转化为byte。
- short shortValue() 将此对象转化为short。
- double doubleValue() 将此对象转化为double。
- float floatValue() 将此对象转化为float。
public static void main(String[] args) {
Integer In = 10;
double d = In.doubleValue();
System.out.println(d);
//输出10.0
}
- boolean equals(Object obj) 将此对象与指定的对象进行比较。
- int compareTo(Integer anotherInteger) 用Integer数字比较两个对象。
public static void main(String[] args) {
Integer In = 10;
Integer In2 = 100;
System.out.println(In.equals(In2));
System.out.println(In.compareTo(In2));
//false
//-1
}
int hashCode()
返回此的哈希码Integer。
String toString()
返回String表示此Integer值的对象 。
//写到这里应该发现Integer类与之前的Short类中的方法极为类似,将省略部分实例,
//但依然列出很多的方法有助于产生一定的印象和加深理解
静态方法
- static Integer valueOf(int i) 将int转化为Integer。
- static Integer valueOf(String s) 将String转化为Integer。
- static Integer valueOf(String s, int radix)
返回一个Integer对象,该对象String包含使用第二个参数给出的基数(计算机进制)解析时从指定的值中提取的值。 - static int max(int a, int b) 返回两个int值中的较大值,就像通过调用Math.max。一样
- static int min(int a, int b) 返回两个int值中较小的一个,就像通过调用Math.min一样。
- static int sum(int a, int b)
根据+运算符将两个整数相加。
public static void main(String[] args) {
int a = 100;
int b = 10;
System.out.println(Integer.max(a,b));
System.out.println(Integer.min(a,b));
//输出100
//10
//110
}
- static String toBinaryString(int i) 返回整数参数的字符串表示形式,作为基数为2中的无符号整数。
- static String toHexString(int i) 返回整数参数的字符串表示形式,作为基数为16的无符号整数。
- static String toOctalString(int i) 返回整数参数的字符串表示形式,作为基数为8的无符号整数。
- static long toUnsignedLong(int x) 通过无符号转换将参数转换为long。
- static String toUnsignedString(int i) 以无符号十进制值的形式返回参数的字符串表示形式。
- static String toUnsignedString(int i, int radix)
返回第一个参数的字符串表示形式,作为第二个参数指定的基数中的无符号整数值。 - static int signum(int i) 返回指定int值的signum(符号)函数。
- static int bitCount(int i) 返回指定int值的二进制补码表示形式中的一位数。
- static Integer getInteger(String nm) 根据指定的系统属性名称获取Integer。
- static Integer getInteger(String nm, int val)
根据指定的系统属性名称获取Integer,如果没有则返回val - static Integer getInteger(String nm, Integer val)
根据指定的系统属性名称获取Integer。如果没有则返回val
String str = "110";
System.out.println(Integer.getInteger(str,-1).getClass());
//class java.lang.Integer
- static Integer decode(String nm) 将String解码为Integer。(0xf,041,99)
- static int divideUnsigned(int dividend, int divisor)
返回将第一个参数除以第二个参数的无符号商,其中每个参数和结果都被解释为无符号值。 - static int lowestOneBit(int i)
返回一个int最多只有一位的值,位于指定int值中最低位(“最右侧”)一位的位置 。 - static int highestOneBit(int i)
返回一个int最多只有一位的值,位于指定int值中最高位(“最左侧”)一位的位置 - static int numberOfLeadingZeros(int i)
返回指定int值的二进制补码表达式中最高位(“最左侧”)一位之前的零位数。 - static int numberOfTrailingZeros(int i)
返回指定int值的二进制补码表达式中最低位(“最右侧”)一位之后的零位数 。 - static int parseInt(String s) 将字符串参数解析为带符号的十进制整数。
- static int parseInt(String s, int radix)
将字符串参数解析为第二个参数指定的基数(计算机进制)中的有符号整数。 - static int parseUnsignedInt(String s) 将字符串参数解析为无符号十进制整数。
- static int parseUnsignedInt(String s, int radix)
将字符串参数解析为第二个参数指定的基数(计算机进制)中的无符号整数。 - static int remainderUnsigned(int dividend, int divisor)
返回将第一个参数除以第二个参数的无符号余数,其中每个参数和结果都被解释为无符号值。 - static int reverse(int i) 返回通过反转指定int 值的二进制补码二进制表示中的位顺序而获得的值。
- static int reverseBytes(int i) 返回通过反转指定int值的二进制补码表示中的字节顺序获得的值。
- static int rotateLeft(int i, int distance)
返回通过旋转int指定位数左侧的指定值的二进制补码表示获得的值。 - static int rotateRight(int i, int distance)
返回通过将指定int值的二进制补码表示法旋转指定的位数而获得的值。 - static int hashCode(int value) 返回int值的哈希码; 兼容 Integer.hashCode()。
- static int compare(int x, int y) 以int数字方式比较两个值。
- static int compareUnsigned(int x, int y) 比较两个int值,以数值方式将值视为无符号。
- static String toString(int i) 返回String表示指定整数的对象。
- static String toString(int i, int radix)
(3)Character
本部分参考Character 类的常用方法
- 构造方法:public Character(char value)
构造一个新分配的 Character 对象,用以表示指定的 char值 - Character类的判断功能: public static boolean isDigit(char ch) 确定指定字符是否为数字。
- public static boolean isLetter(char ch) 确定指定字符是否为字母。
- public static boolean isLowerCase(char ch) 确定是否是小写字母字符
- public static boolean isUpperCase(char ch) 确定是否大写字母字符
public static void main(String[] args) {
Character Ch = new Character('c');
System.out.println(Ch.getClass()+" "+Ch);
//输出class java.lang.Character c
char ch = 'z';
System.out.println(Character.isLetter(ch));//也可以直接写成isLetter('z')
//true
System.out.println(Character.isLowerCase(ch));
//true
System.out.println(Character.isUpperCase(ch));
//flase
}
两个转换功能:
- public static char toLowerCase(char ch)
使用UnicodeData文件中的案例映射信息将字符参数转换为小写。 - public static char toUpperCase(char ch) 使用取自
使用UnicodeData文件中的案例映射信息将字符参数转换为大写。
char ch = 'z';
System.out.println(Character.toUpperCase(ch));
System.out.println(Character.toLowerCase(ch));
//输出//Z z
(4)Boolean
本部分参考 Boolean
构造函数
- Boolean(boolean value) 通过指定的boolean构建一个新的Boolean。
- Boolean(String s) 通过指定的String构建一个新的Boolean
public static void main(String[] args) {
String str = "tru";
Boolean B =new Boolean(true);
Boolean B2 = new Boolean(str);
System.out.println(B.getClass());
System.out.println(B2);
//class java.lang.Boolean
//flase
}
方法
- boolean booleanValue() 将此对象转化为boolean。
- boolean equals(Object obj) 比较两个对象是否相等。
- int hashCode() 返回此Boolean对象的哈希码。
- int compareTo(Boolean b) 将此Boolean实例与另一个实例进行比较
- String toString() 返回String表示此Boolean值的对象。
静态方法
- static boolean getBoolean(String name)
当且仅当存在由参数命名的系统属性等于字符串"true"时返回true。 - static int hashCode(boolean value) 返回boolean值的哈希码; 兼容
Boolean.hashCode()。 - static boolean logicalAnd(boolean a, boolean b)
返回将逻辑AND运算符应用于指定boolean操作数的结果。 - static boolean logicalOr(boolean a, boolean b)
返回将逻辑OR运算符应用于指定boolean操作数的结果。 - static boolean logicalXor(boolean a, boolean b)
返回将逻辑XOR运算符应用于指定boolean操作数的结果。 - static boolean parseBoolean(String s) 将字符串参数解析为布尔值。
public static void main(String[] args) {
String s = "string";
String s2 = "true";
System.out.println(Boolean.parseBoolean(s));
System.out.println(Boolean.parseBoolean(s2));
//false
//true
}
- static String toString(boolean b) 返回String表示指定布尔值的对象。
- static Boolean valueOf(boolean b) 返回Boolean表示指定boolean值的实例 。
- static Boolean valueOf(String s) 返回Boolean带有指定字符串表示的值的a。
- static int compare(boolean x, boolean y)
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 =true;
System.out.println(Boolean.compare(b1,b2));
System.out.println(Boolean.compare(b1,b3));
//1
//0
}
(5)Boolean
本部分参考Boolean常用方法
构造函数
- Double(double value) 通过指定的double值构建一个Double对象。
- Double(String s) 通过指定的String值构建一个Double对象。
public static void main(String[] args) {
Double D = new Double(10.0);
System.out.println(D.getClass()+" "+D);
Double D2 = new Double("10.0");
System.out.println(D2.getClass()+" "+D2);
//class java.lang.Double 10.0
//class java.lang.Double 10.0
}
方法
double doubleValue()
将此对象转化为double。
long longValue()
将此对象转化为long。
short shortValue()
将此对象转化为short。
float floatValue()
将此对象转化为float。
int intValue()
将此对象转化为int。
public static void main(String[] args) {
Double D = 10.0;
int i = D.intValue();
System.out.println(i);
//输出 10
}
- boolean isInfinite() 如果这个Double值是无限大返回true,否则返回false。
- boolean isNaN() 如果此Double值是非数字(NaN)返回true,否则返回false。
- int hashCode() 返回此Double对象的哈希码。
- boolean equals(Object obj) 将此对象与指定的对象进行比较。
- int compareTo(Double anotherDouble) 用Double数字比较两个对象。
- String toString() 返回此Double对象的字符串表示形式。
静态方法
- static boolean isInfinite(double v) 如果指定的数是无限大返回true,否则返回false。
- static double max(double a, double b)
返回两个double值中的较大值,就像通过调用一样Math.max。 - static double min(double a, double b)
返回两个double值中较小的一个,就像通过调用一样Math.min。 - static double sum(double a, double b) 计算(a+b)。
public static void main(String[] args) {
double d1 = 10.0;
double d2 = 100.11;
System.out.println(Double.max(d1,d2));
System.out.println(Double.min(d1,d2));
System.out.println(Double.sum(d1,d2));
//100.11
//10.0
//110.11
}
- static String toHexString(double d) 返回参数的十六进制字符串表示形式double。
- static boolean isNaN(double v) 如果指定的数字是非数字(NaN)值返回true,否则返回false。
- static double longBitsToDouble(long bits) 返回与double给定位表示相对应的值。
- static Double valueOf(double d) 将指定double转化为Double。
public static void main(String[] args) {
double d = 10.0;
Object D= Double.valueOf(d);
System.out.println(D.getClass());
//class java.lang.Double
}
- static Double valueOf(String s) 将指定String转化为Double。
- static boolean isFinite(double d) 如果参数是有限浮点值,则返回true;
否则返回false(对于NaN和无穷大参数)。 - static long doubleToLongBits(double value) 根据IEEE
754浮点“双格式”位布局返回指定浮点值的表示形式。 - static long doubleToRawLongBits(double value) 根据IEEE
754浮点“双格式”位布局返回指定浮点值的表示形式,保留非数字(NaN)值。 - static double parseDouble(String s) 将指定的String转化为double。
- static int hashCode(double value) 返回double值的哈希码; 兼容
Double.hashCode()。 - static int compare(double d1, double d2) 比较两个指定的double值。
- static String toString(double d)
2.字符串操作
本部分参考JAVA字符串操作汇总
(1)字符串的连接
public String concat(String str)
public static void main(String[] args) {
String str1 = "hello";
String str2 ="World";
System.out.println(str1.concat(str2));
//helloWorld
}
(2)求字符串的长度
public int length()
String str1 = "hello";
System.out.println(str1.length());
//5
(3)求字符串中某一位置的字符
public char charAt(int index)
注意:index是从0开始的
public static void main(String[] args) {
String str = "hello";
System.out.println(str.charAt(0));
//h
}
(4)字符串的比较
比较字符串可以利用String类提供的下列方法:
- public int compareTo(String anotherString)
- public boolean equals(Object anObject)
- public boolean equalsIgnoreCase(String anotherString)
该方法和equals方法相似,不同的地方在于,equalsIgnoreCase方法将忽略字母大小写的区别.
(5)从字符串中提取子串
利用substring方法可以提取字串,方法如下:
- public String substring(int beginIndex) 该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回.
- public String substring(int beginIndex, int endIndex)
该方法从当前字符串中取出一个子串,该子串从beginIndex位置起至endIndex-1为结束.子串返的长度为endIndex-beginIndex.
public static void main(String[] args) {
String str = "hello";
System.out.println(str.substring(4));
System.out.println(str.substring(1,4));
//o
//ell
}
(6)判断字符串的前缀和后缀
判断字符串的前缀是否为指定的字符串利用String类提供的下列方法:
- public boolean startsWith(String prefix) , 该方法用于判断当前字符串的前缀是否和参数中指定的字符串prefix一致,如果是,返回true,否则返回false.
- public boolean startsWith(String prefix, int toffset)
该方法用于判断当前字符串从toffset位置开始的子串的前缀是否和参数中指定的字符串prefix一致,如果是,返回true,否则返回false. - 判断字符串的后缀是否为指定的字符串利用String类提供的方法: public boolean endsWith(String suffix) 该方法用于判断当前字符串的后缀是否和参数中指定的字符串suffix一致,如果是,返回true,否则返回false.
public static void main(String[] args) {
String str = "hello";
System.out.println(str.startsWith("he"));
//true
System.out.println(str.startsWith("lo",3));
//true
System.out.println(str.endsWith("lo"));
//true
}
(7)字符串中单个字符的查找
字符串中单个字符的查找可以利用String类提供的下列方法:
- public int indexOf(int ch) 如果找到返回位置,找不到则返回-1
- public int indexOf(int ch, int fromIndex)
该方法和第一种方法类似,不同的地方在于,该方法从fromIndex位置向后查找,返回的仍然是字符ch在字符串第一次出现的位置. - public int lastIndexOf(int ch)
该方法和第一种方法类似,不同的地方在于,该方法从字符串的末尾位置向前查找,返回的仍然是字符ch在字符串第一次出现的位置. - public int lastIndexOf(int ch, int fromIndex)
该方法和第二种方法类似,不同的地方在于,该方法从fromIndex位置向前查找,返回的仍然是字符ch在字符串第一次出现的位置.
public static void main(String[] args) {
String str = "hello";
System.out.println(str.indexOf('o'));
//4
System.out.println(str.indexOf('l',1));
//2
System.out.println();
}
(8)字符串中子串的查找
字符串中子串的查找与字符串中单个字符的查找十分相似,可以利用String类提供的下列方法:
- public int indexOf(String str)
- public int indexOf(String str, int fromIndex)
- public int lastIndexOf(String str)
- public int lastIndexOf(String str, int fromIndex)
(9)字符串中字符大小写的转换
- public String toLowerCase() 该方法将字符串中所有字符转换成小写,并返回转换后的新串.
- public String toUpperCase() 该方法将字符串中所有字符转换成大写,并返回转换后的新串.
public static void main(String[] args) {
String str = "hello";
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
// HELLO
//hello
}
(10)字符串中多余空格的去除
public String trim()
该方法只是去掉开头和结尾的空格,并返回得到的新字符串.值得注意的是,在原来字符串中间的空格并不去掉.
public static void main(String[] args) {
String str = " W orld ";
String str2 = "hello";
System.out.println(str2+str);
//hello W orld
System.out.println(str2+str.trim());
//helloW orld
}
顺带一提它的实际应用:在用户注册账号密码时若一不小心在结尾添加空格,因为不可视,若服务器直接储存,则后续会对用户登陆造成困扰
(11)字符串中字符的替换
- public String replace(char oldChar,char newChar)
该方法用字符newChar替换当前字符串中所有的字符oldChar,并返回一个新的字符串. - public String replaceFirst(String regex, String replacement)
该方法用字符串replacement的内容替换当前字符串中遇到的第一个和字符串regex相一致的子串,并将产生的新字符串返回. - public String replaceAll(String regex, String replacement)
该方法用字符串replacement的内容替换当前字符串中遇到的所有和字符串regex相一致的子串,并将产生的新字符串返回.
public static void main(String[] args) {
String str = "FindGirlFriend";
System.out.println(str.replace('i','o'));
//FondGorlFroend
System.out.println(str.replaceFirst("Find","Have"));
//HaveGirlFriend
System.out.println(str.replaceAll("nd","d"));
//FidGirlFried
}
(12)可变字符串
主要指StringBuilder和StringBuffer
构造函数
- StringBuilder()
- StringBuilder(Char c)
- StringBuilder(int i)
- StringBuilder(String str)
StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder('a');
StringBuilder builder = new StringBuilder(10);
StringBuilder builder = new StringBuilder("abc");
主要方法
- append(type s):
将type类型的s追加到序列(type可以是8种基本数据类型和String,Object形式)。
StringBuilder Builder = new StringBuilder("abc");
System.out.println(Builder.append("de"));
//abcde
- reverse(): 将序列倒序
StringBuffer Buffer = new StringBuffer("abcde");
System.out.println(Buffer.reverse());
//edcba
- delete(int start, int end): 移除序列中指定的的字符,指定开始位置和结束位置(大于等于start,小于end)。
StringBuffer Buffer = new StringBuffer("abcde");
System.out.println(Buffer.delete(1, 4));
//ae
- insert(int s, type i): 在序列中s处,将type类型的字符串插入序列中(type可以是8种基本数据类型和String,Object形式)。
StringBuffer Buffer = new StringBuffer("abcde");
System.out.println(Buffer.insert(3,"cba"));
//abccbade
- replace(int start, int end, String str): 用给定的str替换序列中指定的字符,指定开始位置和结束位置(大于等于start,小于end)。
StringBuffer Buffer = new StringBuffer("abcde");
System.out.println(Buffer.replace(1,4,"dcbtest"));
//adcbteste
6.int capacity():返回长度
StringBuffer Buffer = new StringBuffer("abcde");
System.out.println(Buffer.capacity());
//21 默认长度加上所赋值的长度
-
ensureCapacity(int mix)
设定字符串的容量至少等于mix。 -
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从序列复制到目标字符数组dst。
被复制的第一个字符是srcBegin处;被复制的最后一个字符是srcEnd - 1处,字符复制到dst数组,在dst数组dstBegin处开始粘贴。 -
setCharAt(int index, char c)
将index处的字符设置为c。 -
setLength(int i)
设置字符序列的长度为i。 -
subSequence(int start, int end)
截取字符串,指定开始位置和结束位置(大于等于start,小于end)。
StringBuilder和StringBuffer的其他方法(和上文中String方法基本一致):
-
char charAt(int index)
-
int indexOf(String str)
-
int indexOf(String str, int fromIndex)
-
int lastIndexOf(String str)
-
int lastIndexOf(String str, int fromIndex)
-
int length()
-
substring(int start)
-
substring(int start, int end)
-
toString()
字符串变量与StringBuffer类
1.创建StringBuffer类对象
StringBuffer类对象表示的是字符串变量,每一个StringBuffer类对象都是可以扩充和修改的字符串变量.以下是常用的StringBuffer类构造函数:
(1)public StringBuffer()
(2)public StringBuffer(int length)
(3)public StringBuffer(String str)