1.Java.lang包的使用
(1)了解Java.lang包
ava.lang包下放置了Java开发中常用的类和接口。
说明:java.lang包下的类不需要导入。
(2)java.lang包下常用的类:
Object
String
StringBuffer和StringBuilder
八种基本数据类型的包装类
Thread……
(3)java.lang.Object
java.lang.Object类是类层次结构中最顶层父类。所有的类(包括数组)
直接或者间接的继承自Object类,同时也继承了该类中的方法
a.Object的构造方法
Object类只有一个无参的构造方法
Object()
b. Object中常用的方法
boolean equals(Object o) 比较两个对象是否相同
int hashCode() 返回一个对象的哈希码
String toString() 将一个对象转换为字符串
说明:哈希码是一种算法,为了让同一个类的对象按照自己不同的特征尽量的有不同的哈希码。
·c. equals方法和==的区别
==可以用于比较基本数据类型的值
==可以用于比较两个对象是否相等
equals只能比较两个对象是否相等
(4)java.lang.String
String类代表字符串,提供了开发中常用的字符串处理的方法;
字符串对象是常量,它的值创建之后就不可以再修改了。
a.构造方法
String类常用构造方法
String() 无参构造方法
String(String str) 有一个字符串参数的构造方法
String(char[]ch) 有一个char类型数组参数的构造方法
String(byte[] b) 有一个byte数组参数的构造方法
例
public class test{
public static void main(String[] args){
String s1=new String();
String s2=new String("abcd");
char[] cc={'a','b','c','d'};
String s3=new(cc);
byte b={96,97,98,99};
String s4=new String(b);
}
}
b.常用方法
参数
返回值 | 方法名 | 参数 | 说明 |
char | charat() | int index | 返回指定索引 处的 char 值。 |
boolean | endsWth | String suffix | 测试此字符串是否以指定的后缀结束 |
int | compareTo | String antherString | 按acs码比较两个字符串大小。 |
boolean | startsWith | String prefix String prefix, int toffset | 测试此字符串是否以指定的前缀开始。测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 |
int | compareToIgnoreCase | String antherString | 按asc比较两个字符串 ,不考虑大小写。 |
byte[] | getBytes | 无参 Charset charset String charsetName | 将此String编码为 byte 序列,并将结 果存储到一个新的 byte 数组中。 使用给定的charset将此String 编码 到 byte 序列,并将结果存储到新的 byte 数组。 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
int | lastIndexOf | int ch int ch, int fromIndex String str String str,int fromIndex | 返回指定字符在此字符串中 最后一次出现处的索引 返回在此字符串中最后一次 出现指定字符处的索引,从 指定的索引开始搜索。 返回指定子字符串在此字符 串中最后一次出现处的索引。 返回指定子字符串在此字符 串中最后一次出现处的索引, 从指定的索引开始。 |
int | indexOf | int ch int ch, int fromIndex String str String str,int fromIndex | 返回指定字符在此字符串中第一次出现处的索引 返回在此字符串中第一次出现指定字符处的 索引,从指定的索引开始搜索。 返回指定子字符串在此字符串中第一次出现处的索引。 返回指定子字符串在此字符串中第一次出现处的 索引,从指定的索引开始。 |
boolean | equals | Object object | 将此字符串与指定的对象比较。 |
boolean | IsEmpty | 当且仅当length()为0时返回true。 | |
boolean | equalsIgnoreVase | String antherString | 将此 String与另一个String 比较,不考虑大小写。 |
String | concat | String str | 将指定字符串 连接到此字符串的结尾。 |
String | subString | int beginIndex, int endIndex | 按照指定位置截取字符串从参数1到参数2-1 |
int | length | 返回此字符串的长度。 | |
char[] | toCharArray | 将此字符串转换为一个新的字符数组。 | |
String | replace | CharSquare target,CharSquare replacement | 使用指定的字面值替换序列替换此字符串 所有匹配字面值目标序列的子字符串。 |
String | toLowCase | 将此 String 中的所有字符都转换为小写。 | |
String[] | split | String regex | 根据给定字符的匹配拆分此字符串。 |