java基础知识(9)
01-API介绍
-
介绍 : API 应用程序编程接口
-
- 简单记 : 就是别人已经写好的代码
-
学习别人已经写好的代码, 使用的是 API 帮助文档
-
帮助文档的使用流程 :
API帮助文档的使用流程 :
- 在索引的位置搜索自己要查看的类
- 思考: 我怎么知道, 自己要看哪一个类?
- 回答: 开发中常用的类, 在课程中都会讲解到
- 看该类所在的包
- 目标: 看这个包是不是 java.lang (核心包)
是 : 使用的时候不需要导包
不是 : 使用的时候必须要编写导包代码
- 看该类的介绍
- 目标: 明确出这个类的作用
- 看该类的构造方法
- 目标: 能够根据构造方法, 创建出该类的对象
- 看该类的成员方法
- 1. 方法名
- 2. 方法的参数
- 3. 方法的返回值
- 4. 方法的介绍
02-String类的特点
- Java程序中, 所有双引号字符串, 都是String这个类的对象
- String类, 多了一种创建对象的方式
- String字符串, 一旦被创建, 就[不可改变]
- 不可改变: 字符串内容不能改变, 要想做修改, 只能使用新的对象做替换.
String s = “abc”;
s = “def”;
System.out.println(s); // def
- 字符串虽然不可改变, 但是其内容可以被共享
- 字符串常量池: 当使用双引号创建字符串对象的时候, 会检查常量池中是否存在该数据
没有 : 创建
有 : 复用
String s1 = “abc”;
String s2 = “abc”;
System.out.println(s1 == s2); // true
public static void main(String[] args) {
// 引用数据类型, ==号比较的时候, 比较的是地址值
String s1 = "abc";
String s2 = "abc";
// 疑惑: s1和s2是两个对象, 地址值不相同, 比较出来的结果, 应该是false
// 回答: 结果是true
System.out.println(s1 == s2);
}
}
- 字符串和字符数组之间允许相互转换
char[] chs = {‘a’,‘b’,‘c’}; <-----------> String s = “abc”;
- 字符串数组转换字符串
String s = new String(chs);
- 字符串拆分为字符数组
char[] chs = s.toCharArray();
03-String类常见的构造方法
-
public String() : 创建一个空白的字符串对象
-
public String(char[] chs) : 根据传入的字符数组, 来创建字符串对象
-
public String(String s) : 根据传入的字符串, 来创建字符串对象
细节: 打印字符串对象, 为什么没有地址值?
- 请先暂时记住, 打印字符串对象名, 直接展示内容, 并非地址值.
Java程序中,所有双引号字符串,都是String这个类的对象
- 理解:String类,多了一种创建对象的方式
String s1 = “abc”;
- 问题: 那既然String是一个对象, 打印对象名, 应该看到地址值啊?
- 回答:
目前暂时记忆: 打印字符串对象名,会直接看到内容
TODO: 1. 继承 2. 方法重写 3. Object 4. toString方法
TODO: 知道了以上4个知识点之后,才能够清楚为什么不是地址值.
想法: 这三种创建方式, 都没有双引号直接创建, 来的方便
String s = “abc”;
public static void main(String[] args) {
// public String() : 创建一个空白的字符串对象
String s1 = new String();
System.out.println(s1);
// public String(char[] chs) : 根据传入的字符数组, 来创建字符串对象
char[] chs = {'a','b','c'};
String s2 = new String(chs);
System.out.println(s2);
// public String(String s) : 根据传入的字符串, 来创建字符串对象
// 问题: 这一句代码, 创建了几个字符串对象 ?
// 回答: 两个!
String s3 = new String("abc");
System.out.println(s3);
}
}
3.1 创建字符串对象, 两种方式的区别
- 双引号直接创建 : 在字符串常量池中维护
- 通过构造方法 new 出来 : 在堆内存中有自己独立的内存地址
3.2 字符串面试题
package com.itheima.test;
public class StringTest1 {
/*
String常见面试题 (看程序, 说结果, 并说明原因)
*/
public static void main(String[] args) {
String s1 = "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1 == s2);
}
*/
Java存在常量优化机制:
byte b = 3 + 4;
javac(编译) : byte b = 7;
------------------------------
String s2 = "a" + "b" + "c";
javac(编译) : 就会完成拼接 String s2 = "abc";
-----------------------------------
字节码文件中:
/*
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // true
String s1 = "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1 == s2); // true
}
private static void method3() {
String s1 = "abc";
String s2 = "ab";
String s3 = s2 + "c";
System.out.println(s1 == s3); // false
}
private static void method2() {
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2); // false
}
private static void method1() {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // true
}
}
04-String类成员方法
- 字符串中用于比较的方法 :
public boolean equals(String anObject) : 字符串之间进行内容的比较, 返回true, false
public boolean equalsIgnoreCase(String anotherString) : 字符串之间进行内容的比较, 返回true, false (不考虑大小写)
public class StringMethodDemo1 {
*/
String类中用于比较的方法:
public boolean equals(String anObject) :字符串之间进行内容的比较,返回true,false
public boolean equalsIgnoreCase(String anotherString) :字符串之间进行内容的比较,返回true, false (不考虑大小写)
/*
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2); // false*
System.out.println(s1.equals(s2)); // true*
System.out.println("------------------------");
String ss1 = "abc";
String ss2 = "ABC";
System.out.println(ss1.equals(ss2)); // false
System.out.println(ss1.equalsIgnoreCase(ss2)); // true
}
}
- 字符串转字符数组的方法 :
public char[] toCharArray() : 将此字符串转换为一个新的字符数组
public static void main(String[] args) {
String s = "itheima";
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
System.out.println(c);
}
System.out.println("-------------------------");
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
}
}
public static void main(String[] args) {
String s = “itheima”;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
System.out.println©;
}
System.out.println(“-------------------------”);
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
}
}
- 字符串用于遍历方法 :
public char charAt(int index) : 根据索引找字符
public int length() : 返回字符串的长度(字符的个数)
注意事项:
数组名.length : 属性
字符串对象.length() : 方法
- 字符串用于截取方法 :
public String substring(int beginIndex) : 根据传入的索引, 截取字符串, 截取到字符串的末尾, 返回新的字符串
public String substring(int beginIndex, int endIndex) :
根据传入的开始和结束索引, 截取字符串 (包含头, 不包含尾), 返回新的字符串
public class StringMethodDemo3 {
*/
String类的截取方法:
public String substring(int beginIndex) :根据传入的索引,截取当前字符串,一直截取到末尾,并返回新的字符串
public String substring(int beginIndex, int endIndex) :根据传入的开始和结束索引,截取字符串(包含头,不包含尾)
注意事项:都会将截取后的内容,作为新的字符串返回!!!
/*
public static void main(String[] args) {
String s = "itheima";
s = s.substring(0,2);
System.out.println(s);
}
}
- 字符串用于替换方法 :
public String replace(String target, String replacement) :
参数1: 旧值
参数2: 新值
public class StringMethodDemo4 {
*/
String类的替换方法*:
public String replace(String target, String replacement) :
参数1:旧值
参数2:新值
替换字符串,并返回新的字符串
/*
public static void main(String[] args) {
String s = "itheima";
s = s.replace("heima","baima");
System.out.println(s);
}
}
- 字符串用于切割的方法 :
public String[] split(String regex) : 根据传入的字符串作为规则, 切割当前字符串, 并返回字符串数组.
注意: 指定切割规则的时候, 如果没有按照预想的切割出数据
可以在规则前面, 加入 \
public static void main(String[] args) {
String info = "192+168+1+1";
String[] sArr = info.split("\\+");
for (int i = 0; i < sArr.length; i++) {
System.out.println(sArr[i]);
}
}
}
String s = "itheima";
s = s.replace("heima","baima");
System.out.println(s);
}
}
- 字符串用于切割的方法 :
public String[] split(String regex) : 根据传入的字符串作为规则, 切割当前字符串, 并返回字符串数组.
注意: 指定切割规则的时候, 如果没有按照预想的切割出数据
可以在规则前面, 加入 \\
```java
public static void main(String[] args) {
String info = "192+168+1+1";
String[] sArr = info.split("\\+");
for (int i = 0; i < sArr.length; i++) {
System.out.println(sArr[i]);
}
}
}