------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
一.API的概述
1.应用程序编程接口。
Application Programming Interface
2.API就是JDK提供给开发者的一些提高编程效率的java类。
二.Object类
1.Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类。
2.Object类的构造方法有一个,就是无参构造
子类构造方法默认访问父类的构造是无参构造
3.常用的方法:
A:toString()
返回对象的字符串表现形式,默认是
getClass().getName() + "@" + Integer.toHexString(hashCode())
一般子类都会重写toString()方法
最终使用Eclipse自动生成的
B:equals()
比较两个对象是否相同。默认情况下,比较的是地址值是否相同(this == obj)
一般子类也会重写equals方法。
最终使用Eclipse自动生成的
4.其他方法:
A:hashCode() 返回对象的哈希值,可以理解为地址值。
B:getClass() 返回对象的字节码文件对象
C:finalize() 用于垃圾回收
D:clone() 可以实现对象的克隆,包括成员变量的数据复制
(和两个引用指向同一个对象是不是一个概念)
5.注意问题;
A:直接输出一个对象名称,实际上是默认调用了该对象的toString()方法。
B:==和equals()的区别
A:==
基本数据类型:比较的是值是否相同
引用数据类型:比较的是地址值是否相同
B:equals()
只能比较引用数据类型。默认比较的是地址值是否相同
可以根据需要重写equals方法
6.练习:
toString()
equals()
三.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()
public String next()
注意事项:
先next(),再nextLine()会出现问题。
解决方案:
重新建立Scanner对象
4.练习:
<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>package cn.itcast;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String string= sc.nextLine();
System.out.println(string);
int num = sc.nextInt();
System.out.println(num);
String next =sc.next();
System.out.println(next);
}
}
</strong></span>
四.String类的概述和使用
1.由多个字符组成的一串数据。
2.构造方法:
A:String s = new String();
B:String s = new String(byte[] bys);
C:String s = new String(byte[] bys,int startIndex,int count);
D:String s = new String(char[] chs);
E:String s = new String(char[] chs,int startIndex,int count);
F:String s = new String(String s2);
G:String s = "hello";
常用:
B C D E G
3.字符串的常见功能:
A:判断功能
boolean equals(Object obj)
字符串与另外一个对象做比较,完全相同为true
boolean equalsIgnoreCase(String str)
不区分大小写两个字符串做比较,相同为true
boolean contains(String str)
字符串中包含指定的字符序列是,结果为true
boolean startsWith(String str)
字符串中以给定的字符序列开始,结果为true
boolean endsWith(String str)
字符串中以指定的字符序列结束,结果为true
boolean isEmpty()
当字符串为空也就是长度为零时,结果为true
B:获取功能
int length()
获取字符串的长度
char charAt(int index)
获取给定索引位置的字符
int indexOf(int ch)
获取指定字符在字符串中第一次出现的索引值
int indexOf(String str);
获取指定字符串在字符串中第一次出现的索引值
int indexOf(int ch,int fromIndex)
获取指定字符在指定的索引开始第一次出现的索引值
int indexOf(String str,int fromIndex)
获取指定字符串在指定的索引开始第一次出现的索引值
String substring(int start)
获取从指定索引开始到字符串末尾的子字符串
String substring(int start,int end)
获取从指定索引开始到最后一个end-1处索引的子字符串
C:转换功能
byte[] getBytes()
将字符串转换为byte数组
char[] toCharArray()
将字符串转换为char数组
static String copyValueOf(char[] chs)
返回指定字符数组中表示该字符序列的String
static String valueOf(char[] chs)
返回char数组的字符串表现形式
static String valueOf(int i)
返回int参数的字符串表现形式
String toLowerCase()
将字符串中的字符全部转换为小写
String toUpperCase()
将字符串中的字符全部转换为大写
String concat(String str)
将指定字符串连接到此字符串的末尾
D:其他功能
String replace(char old,char new)
用新的字符代替字符串中的所有老的字符
String replace(String old,String new)
用新的字符串代替字符串中所有老的字符串
String[] split(String regex)
根据给定的字符串,拆分字符串,末尾没有空字符
String trim()
返回首尾没有空字符的新字符串
int compareTo(String str)
按照字典顺序比较两个字符串,返回一个整数
int compareToIgnoreCase(String str)
按照字典顺序不区分大小写比较两个字符串,返回一个整数
4.关于String注意事项:
A:字符串一旦被赋值就不能被改动。
注意:这里的改动指的是字符串的内容,而不是字符串对象的引用。
因为字符串被赋值后会放在方法区的常量池中.可以同时被多个对象使用
B:String s = new String("hello");和String s = "hello";有区别吗?是什么呢?
分析: 有区别
前者创建了两个对象,一个在对象在堆内存中,一个在方法区中的常量池,s的值是堆内存地址值
后者创建了一个对象,在方法区中的常量池中,s的值是字符串在常量池的地址值
C:看程序,写结果
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);//比较的是地址值
System.out.println(s1.equals(s2));//比较的是字符串是否相等
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);//比较的是地址值
System.out.println(s3.equals(s4));//比较的是字符串是否相等
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//比较的是地址值
System.out.println(s5.equals(s6));//比较的是字符串是否相等
结果:false true,false true,true true
D:看程序,写结果
String s7 = "hello";
String s8 = "world";
String s9 = "helloworld";
System.out.println(s9==s7+s8);//比较的是地址值
System.out.println(s9=="hello"+"world");//比较的是字符串是否相等
结果: false,ture
总结:变量是直接赋值
常量是在常量池中找,有就使用,没有就直接创建.
5.案例练习:
A:遍历字符串
<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>package cn.itcast;
public class Test1 {
public static void main(String[] args) {
//定义字符串
String s= "abcde";
//将字符串转换为字符数组
char[] c= s.toCharArray();
//将字符数组遍历打印
for (int i =0 ; i<c.length;i++){
System.out.println(c[i]);
}
}
}
</strong></span>
B:统计字符串中大写字母,小写字母以及数字字符出现的次数
<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>package cn.itcast;
public class Demo3 {
public static void main(String[] args) {
//定义字符串
String s = "aaaHHHHssshjkHkhUkgft";
//将字符串转换为字符数组
char[] arr = s.toCharArray();
//定义计数变量
int maxnum = 0;
int minnum = 0;
//遍历统计
for (int i = 0; i < arr.length; i++) {
char cha = arr[i];
if (cha >= 'a' && cha <= 'z') {
minnum++;
} else if (cha >= 'A' && cha <= 'Z') {
maxnum++;
}
}
//输出结果
System.out.println("大写为:" + maxnum + "个" + "小写为:" + minnum + "个");
}
}
</strong></span>
C:把一个字符串的首字母变成大写,其他的全部小写
<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>package cn.itcast;
public class Test4 {
/**
* 将字符串首字母变为大写,其余字母变为小写。
*/
public static void main(String[] args) {
// 定义字符串
String str = "aaaNNHGhhgFHggj";
// 第一步将字符串全部变为小写
String s = str.toLowerCase();
// 对字符串转换为字符数组进行遍历并将首字母转换为大写
char[] arr = s.toCharArray();
String end = "";
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
String d = arr[0] + "";
String d1 = d.toUpperCase();
end += d1;
} else {
end += arr[i];
}
}
System.out.println(end);
}
}
</strong></span>
D:统计大串中小串出现的次数
<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>package cn.itcast;
public class Test5 {
/**
* 给定一个字符串找到子串在字符串中出现的次数。String s = “abcitcastabcxxxabc”中的“abc”
*/
public static void main(String[] args) {
// 定义字符串
String str = "abcitcastabcxxxabc";
/*
* 首先判断"abc"在str中是否存在
* int x= str.indexOf("abc",0);
* 如果x>=0,则存在,然后去掉前边的字符串
* int y=str.indexOf("abc", x+3);
* 如果y>=0,继续去掉字符,进行判断
* int z=str.indexOf("abc", y+3);
* 根据这种情况,我们发现规律,可以进行循环改进
*/
//定义索引与计数
int index = 0;
int count = 0;
//先判断字符串中是否有小串
int a = str.indexOf("abc", index);
//有进入循环语句进行统计
while (a >= 0) {
index = a + 3;
count++;
a = str.indexOf("abc", index);
if (a < 0) {
break;
}
}
//输出结果
System.out.println(count);
}
}
</strong></span>