------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
常见对象(一)
一 Java API
API(Aplication Programming Interface) 应用程序编程接口。就是 Java提供给我们使用的类,这些类将底层的实现封装起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用就可以了。
二 Object类
Ⅰ Object类概述
类层次结构的根类,所有类都直接或者间接的继承自该类。
java.lang包下。JDK1.0开始
Ⅱ 构造方法
public Object() 子类的构造方法默认访问父类无参的构造方法,而所有子类的父类都默认继承自Object类的构造方法public Object()
Ⅲ 成员方法
⒈ hashCode()方法 public int hashCode()
概述:⑴返回该对象的哈希玛值,一般情况该方法会根据对应地址来计算
⑵ 不同对象的hashcode()一般来说不会相同(也有可能会相同);但是不同对象的哈希玛值一定不同。
⑶ 哈希玛值不是对象的地址值,是根据其地址值而得到的一个int类型的数值,可以理解为逻辑地址值
代码实现
package cn.itcast.cotest1;
publci class Test1 {
public static void main(String[] args) {ai
String s ="HelloWorld";
int hc = s.hashCode();
System.out.println(hc);
}
}
控制台打印:439329280
⒉ getClass()方法 public final class getClass()
概述:返回此Object类的运行时类,可以通过Class类中的一个方法,获取对象的真实类的全名称。
代码实现
package cn.itcast.cotest1;
public class Test1 {
public static void main(String[] args) {
String s ="HelloWorld";
/*
int hc = s.hashCode();
System.out.println(hc);
*/
System.out.println(s.getClass());
}
控制台打印:class java.lang.String
⒊ toString()方法 public String toString()
概述:返回该对象的字符串表示
注意:重写了toString()方法后打印的是字符串的。若没有重写,继承Objecet类的tiString()方法,返回地址值
getClass().getName() + "@" + Integer.toHexString(hashCode())
代码体现
package cn.itcast.cotest1;
public class Test2 {
public static void main(String[] args) {
Student s = new Student("张三",15);
System.out.println(s.toString());
}
控制台打印:cn.itcast.cotest1.Student@67064
附:Student类
package cn.itcast.cotest1;
public class Student {
private String name ;
private int age ;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
⒋ eauals() 方法
public boolean equals(object obj){return (this == obj)}
“==”和“equals()”的区别
“==”可以比较基本数据类型,也可以比较引用数据类型;
equals()只能比较引用数据类型,一般默认比较地址值,如果打算比较其他的可以重写该方法
代码体现
package cn.itcast.cotest1;
public class Test2 {
public static void main(String[] args) {
Student s = new Student("张三",15);
//System.out.println(s.toString());
Student s1 = new Student("张三",15);
Student s2 = new Student("李四",16);
Student s3 = s;
boolean flag1 = ( s == s1);
boolean flag2 = s.equals(s1);
boolean flag3 = (s == s2);
boolean flag4 = s.equals(s3);
System.out.println(flag1);
System.out.println(flag2);
System.out.println(flag3);
System.out.println(flag4);
}
}
打印结果:false false false true
三 Scanner类
Ⅰ
JDK1.5以后用于键盘录入数据
Scanner(InputStream source)
public static final InputStream in;标准输入流
Ⅱ方法
1 hasNextXxx()
Xxx是指数据类型,可以是Int,Double等 注意如果需要判断是否是字符串可以省略Xxx
2 nextXxx()
获取下一个输入项
代码实现
package cn.itcast_cotest2;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入第一个数据");
if(sc1.hasNextInt()){
int x = sc1.nextInt();
System.out.println(x);
}else{
System.out.println("您的数据非法");
}
}
}
控制台打印
请输入第一个数据 请输入第一个数据
12 HelloWo rld
12 您的数据非法
四 String类
Ⅰ概述
字符串是由多个字符组成的数据,字符序列,字符串可以看成字符数组
字符串字面值"abc"也可以看成是一个字符串对象。字符串是常量,一旦被创建,就不能被改变。
JDK1.0开始。
Ⅱ构造方法
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
index表示的是从第几个索引开始, length表示的是长度
代码演示
package cn.itcast_cotest3;
public class Test1 {
public static void main(String[] args){
String s1 = new String();
System.out.println(s1.length());
System.out.println("----------");
byte[] arr1 = {95,96,97,98};
String s2 = new String(arr1);
System.out.println(s2);
System.out.println("----------");
String s3 = new String(arr1,1,3);
System.out.println(s3);
System.out.println("----------");
char[] arr2 = {'a','b','c','d'};
String s4 = new String(arr2);
System.out.println(s4);
System.out.println("----------");
String s5 = new String(arr2,2,2);
System.out.println(s5);
System.out.println("----------");
String s6 = new String("我爱黑马");
System.out.println(s6);
}
}
打印结果:
Ⅲ 成员方法
String类的判断功能:
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空。
代码实现
package cn.itcast_cotest3;
public class Test2 {
public static void main(String[] args) {
String s1 = "abcd";
String s2 = "abCD";
String s3 = "";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.contains("ab"));
System.out.println(s1.startsWith("abC"));
System.out.println(s1.endsWith("cd"));
System.out.println(s1.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): 从指定位置开始到指定位置结束截取字符
代码实现:
package cn.itcast_cotest3;
public class Test3 {
public static void main(String[] args){
String s = "HelloWorldhello";
System.out.println(s.length());
System.out.println(s.charAt(0));
System.out.println(s.indexOf('l'));
System.out.println(s.indexOf('l',4));
System.out.println(s.indexOf("ll"));
System.out.println(s.indexOf("ll", 5));
System.out.println(s.substring(5));
}
} 打印结果:
String类的转换功能:
public byte[] getBytes(): 把字符串转换为字节数组
public char[] toCharArray(): 把字符串转换为字符数组
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串注意:String类的valueOf方法可以把任意类型的数据转成字符串
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写
public String concat(String str): 把字符串拼接。
代码实现
package cn.itcast_cotest3;
public class Test4 {
public static void main(String[] args) {
String s = "abc";
byte[] arr = s.getBytes();
for(int x = 0 ;x < arr.length; x++){
System.out.println(arr[x]);
}
System.out.println("------------");
char[] arr2 = s.toCharArray();
for(int x = 0 ;x < arr.length; x++){
System.out.println(arr2[x]);
}
System.out.println("------------");
char[] arr3 = {'a','b','c'};
String s2 =String.valueOf(arr3);
System.out.println(s2);
System.out.println("------------");
String s3 = String.valueOf(100);
System.out.println(s3);
System.out.println("------------");
String s4 = "abCDef";
System.out.println(s4.toUpperCase());
System.out.println(s4.toLowerCase());
System.out.println("-----------");
String s5 = s4.concat(s3);
System.out.println(s5);
}
}
打印结果:
Strring类的其他功能
String替换功能
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
String去除字符串两空格
public String trim() 去除两端空格
String按字典顺序比较两个字符串
public int compareTo(String str)
public int compareToIgnoreCase(String str)
package cn.itcast_cotest3;
public class Test5 {
public static void main(String[] args) {
String s1 = " Hello World ";
System.out.println(s1.replace('l','t'));
System.out.println(s1.replace("llo", "len"));
System.out.println(s1.trim());
System.out.println(s1.compareTo(s1.replace('l','t')));
System.out.println(s1.compareToIgnoreCase(s1.replace('l','t')));
}
}
打印结果: