天天看点

java类api_JAVA API常见的使用类(一)

Object类

1,是定义在java.lang包下的类,是所有类的超类(父类),所有的类都直接或间接的继承Object类。

父类:超类、根类、基类。

子类:派生类。

2,需要掌握的方法:

pulic String toString():返回对象字符串的表示形式。

默认情况下的组合:类名的全路径名称+@+对象的哈希值无符号十六进制表示形式。

这种做法没有意义,所以一般情况下重写。

重写做法:把所有成员变量组合成一个字符串返回。

public boolean equals(Object obj):比较对象的地址值是否相同。

默认的情况下,是比较对象的地址值是否相同。

如果有需要,重写Object类的方法,该怎么重写?

//重写Object类

public booleanequals(Object obj){if(this==obj){returnture;

}if(!(obj instanceofStudent)){return false;

}

Student s=Student(obj);return this.name.equals(s.name)&&this.age==s.age;

}

如果多个成员变量,把多个比较用&&连接,

引用类型用equals()方法,

基本类型用==。

==与equals()的区别:

==:

可以比较基本类型,也可以比较引用类型。

比较基本类型,比较的是值是否相等。

比较引用类型,比较的是地址值是否相等。

equals():

只能比较引用类型。

默认的情况下比较的是地址值是否相等。

如果需要比较内容,需要重写Object类的equals()方法。

如下所示:

//==与equals()的区别

String s="hello";

String s1="hello";

String s2=new String("hello");

String s3="world";

Striing s4=new String("world")

String s5="helloworld";

System.out.println(s==s1); //true

System.out.println(s.equals(s1)); //true

System.out.println(s==s2); //false

System.out.println(s.equals(s2)); //true

System.out.println(s5==s+s3); //false

System.out.println(s5.equals(s+s3)); //true

System.out.println(s5.equals(s+s4)); //ture

System.out.println(s5=="hello"+"world"); //true

System.out.println(s5.equals("hello"+"world")) //true

1 //equals()陷阱

2

3 if(s.equals("world")){4 }5

6 if("world".equals(s)){7 }8

9 //假如s是一个外界传递值,如果给null,s就会NullPointerException10 //改善方案:把常量写在前面,定义成为对象。

Scanner

1,Scanner是JDK5以后出现的功能, 方便从键盘接收数据的类。

2,Scanner的格式构造:

Scanner sc=new Scanner(System.in);

System.in是System类下面有一个静态的成员变量in,它在类型是InputStream。代表的是标准键盘输入流。

Scanner是对其封装,提供了各种转换功能。方便获取各种需要的数据类型数据。

3,需要掌握的两个功能:

a:返回int类型

public int nextInt()

b:返回String类型

public String nextLine()

注意事项:

两个一起组合使用的时候 ,先用nextInt(),再用nextLine()会有问题。

解决方案:

1,重新建立Scanner对象。

2,把所有数据都输入成String类型,再对其进行转换。

String

1,由多个字符组成的一组数据。

2,构造方法:

//构造方法

1 String s=newString();2 String s=new String(byte[] bys);3 String s=new String(byte[] bys,int startIndex,intcount);4 String s=new String(char[] chs);5 String s=new String(char[] chs,int startIndex,intcount);6 String s=newString(String s2);7 String s="hello";

常使用的几种:2、3、4、5、7。

3,字符串常见的功能:

1,判断:

boolean equals(Object obj):比较字符串的内容是否相同。

boolean equalsIgnoreCse(String str):比较字符串的内容是否相同,不区分大小写。

boolean contains(String str):判断该字符串对象是否包含给定的字符串。

boolean startWith(String str):判断该字符串对象是否以给定的字符串开头。

boolean endWith(String str):判断该字符串对象是否以给定的字符串结束。

boolean isEmpty(String str):判断该字符串对象是否为空,是指的内容(””) 还是地址(null)?

//isEmpty 测试

String s="helloworld";

System.out.println(s.isEmpty());//false

System.out.println("".isEmpty()); //true

String s=null;

System.out.println(s.isEmpty());//java.lang.NullPointerException

2,获取:

int length():返回该字符串对象的长度。

char charAt():返回指定索引的字符。

int indexOf(int ch):返回指定字符第一次出现的索引位置。'A' 65

int indexOf(String str):返回指定字符串第一次出现的索引位置。

int indexOf(int ch,int fromIndex):从指定索引位置开始,返回指定字符第一次出现的索引位置。

String subString(int start):从指定索引位置开始截取字符串,包含指定索引位置本身。

String subString(int start,int end):从指定的索引位置开始截取字符串,到指定的索引位置结束截取字符串。包左不包右。

3,转换:

byte getBytes():把字符串转换成字节数组。

char toCharArray():把字符串转换成字符数组。

static String copyValueOf(char[] chs):把字符数组转换成字符串。

static String valueOf(char[] chs):把字符数组转换成字符串。

static String valueOf(int i ):把int类型的数据转换成字符串。

String toLowerCase():把字符串转换成小写。

String toUpperCase():把字符串转换成大写。

String concat(String str):字符串的拼接。

4,其它:

String replace(char old,char new):把该字符串中的指定字符替换成新的指定字符。

String replace(String old,String new):把该字符串中的指字符串替换成新的指定字符串。

String split(String regex):把字符串按指定的标记分割成字符串数组。

//分割简易示例

String ages="20-30-35-40";

String[] ageArray=ages.split("-");for(int i=0;i

System.out.println(ageArray[i]);

}-----------------------------------------------------

20

30

35

40

String trim():去掉字符串两端的空格。

int compreTo(String str):按字典顺序比较两个字符串。

int compreToIngoreCase(String str):按字典顺序比较两个字符串,不区分大小写。