1.Object
1.1概述
1.在JDK的java.lang包下定义了Object这个类。
2.java中所有类都直接或者间接继承Object,所有任意类的对象都可以使用Object中定义的方法,不过方法内 部的实现可能在不同子类中有不同的重写。
3.我们可以直接使用空参构造( Object o = new Object() )创建Object的对象,但一般我们不会直接创建 Object对象,更多的是使用多态的方法( Object o1 = new Person() )。
(!!!查看源码Ctrl+鼠标左键)
1.2常用方法
1.2.1 toString方法
取得对象信息,返回该对象的字符串表示。Object的toString方法返回值是类的全类名和对象hash值的拼接。
内部实现如下:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
正常情况下Object的toString的返回值对我们来说没有什么意义,所以需要的话就在自己的类里重写toString方法。
1.2.2equals方法
比较两个对象是否相同
内部实现如下:
public boolean equals(Object obj) {
return (this == obj);
}
很多情况下判断两个对象是否相同的时候不能单纯的使用==去判断,这种情况下就可以重写equal方法,实现自己的逻辑判断。
2.String
2.1.String创建
public class Demo07 {
public static void main(String[] args) {
//直接使用字面值
String s = "小明";
//空参构造
String s1 = new String();
//byte数组 String(byte[] bytes)
byte[] bytes = {97, 98, 99};
String s2 = new String(bytes);
//byte数组,偏移量,长度 String(byte[] bytes,int offset,int length)
String s3 = new String(bytes,0,1);
//String(char[] value)
char[] chars = {'a','b','c','d'};
String s4 = new String(chars);
//String(char[] value,int offset,int count)
String s5 = new String(chars,0,3);
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
}
}
运行结果:
2.2.常用方法
利用好API:点击跳转到API
1.split:
String[] split(String regex):
把一个字符串按照指定的分隔符切割成多个字符串,把多个字符串放在一个字符串数组中返回。
public class Demo08 {
public static void main(String[] args) {
String s= "小明,男,13";
String[] sp = s.split(",");
for (int i = 0; i < sp.length; i++) {
System.out.println(sp[i]);
}
}
}
运行结果:
2.char[] toCharArray():
将此字符串转换为新的字符数组。
public class Demo09 {
public static void main(String[] args) {
String s = "abcd";
char[] chars = s.toCharArray();
System.out.println(chars);
}
}
3.byte[] getBytes():
把一个字符串的内容转换成byte数组
4.String substring(int Index):
将某个字符串从index索引开始
String substring(int begin, int end):
把某个字符串索引begin到索引end截取出来
public class Demo09 {
public static void main(String[] args) {
String s = "abcd";
String su= s.substring(1);
System.out.println(su); //bcd
String su1 = s.substring(1,2);
System.out.println(su1); //b 不包括右边
}
}
5.boolean equals(Object anObject):
判断两个字符串内容是否相同
public class Demo09 {
public static void main(String[] args) {
String s = "abcd";
String s1 = "bcd";
System.out.println(s.equals(s1)); //false
}
}
2.3特点
一个字符串一旦被定义就无法改变
字符串效果上相当于char[]字符数组,但底层其实是byte[]字节数组
3.API高效学习法
大胆猜测(参数列表,返回值类型,方法名)
小心验证(编程idea,api)
例如:判断一个字符串是否以某个字符串开头
参数列表:String
返回值类型:Boolean
4.包装类
4.1概述
java为每一个基本数据类型提供了对应的一个包装类,包装类是一个类,这样java就可以在包装类里提供常用的方法等供我们使用。
基本数据类型 | 包装类 |
---|---|
byte | Byte |
boolean | Boolean |
short | Short |
char | Character |
int | Integer |
long | Long |
float | Float |
double | Double |
4.2创建对象
//直接使用字面值
public class Demo10 {
public static void main(String[] args) {
Integer i = 10;
}
}
//使用构造方法
public class Demo10 {
public static void main(String[] args) {
Integer i = new Integer(10);
}
}
public class Demo10 {
public static void main(String[] args) {
Integer i = new Integer("10");
}
}
//使用静态方法valueOf
public class Demo10 {
public static void main(String[] args) {
Integer i1 = Integer.valueOf(10);
Integer i2 = Integer.valueOf("11");
}
}
4.3常用方法
static Integer valueOf(int i) 将int转换成integer对象返回
static Integer valueOf(String s) 将字符串转换成integer对象返回,字符串里必须是纯数字
static int parseint(String s) 把字符串转换成int返回
public class Demo10 {
public static void main(String[] args) {
int i = Integer.parseInt("10");
}
}
4.4自动装箱和自动拆箱
基本数据类型和对应的包装类自动转换
原理:自动装箱是通过包装类的valueOf来实现的
//自动装箱
public class Demo10 {
public static void main(String[] args) {
int i = 10;
Integer i1 = i;
}
}
//自动拆箱
public class Demo10 {
public static void main(String[] args) {
Integer m = new Integer(10);
int i = m;
System.out.println(i);
}
}
4.4.1 一道笔试题
public class Demo10 {
public static void main(String[] args) {
Integer i1 = 10;
Integer i2 = 10;
Integer i3 = 300;
Integer i4 = 300;
System.out.println(i1==i2); //true
System.out.println(i3==i4); //false
}
}
看源码去分析:不需要看懂,抓重点看。
如下面三图,如果这个数在-128和127之间,可以从其中直接取到
但是不在的话,每次就需要新new一个
Debug—> i1和i2地址值都是555
明显i3和i4地址值不同
总结:其实这就是Integer的缓存机制,-128到127间的数字比较常见,将它们放在数组中,不用每次都新建,可以节省很多空间。
5.StringBuilder
5.1概述
专门用来字符串拼接的(以前都是用+就可以拼接了)
//+拼接的,占用资源,性能不好
public class Demo11 {
public static void main(String[] args) {
String s =""; //字符串一旦定义就无法改变,所以每次拼接都创建了新的字符串
for (int i = 1; i <= 10; i++) {
s=s+i;
}
System.out.println(s); //12345678910
}
}
其实最后只有“12345678910”这个字符串有用,其余十个字符串没有用。
5.2创建
用构造方法创建
public class Demo11 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("小明");
StringBuilder sb1 = new StringBuilder();
}
}
5.3常用方法
5.3.1append拼接
public class Demo11 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("1");
sb.append("2").append("3").append("4");
System.out.println(sb); //1234
}
}
5.3.2reverse反转
public class Demo11 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("123");
sb.reverse();
System.out.println(sb); //321
}
}
5.3.3toString把StringBuilder转换成String
public class Demo11 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("123");
String s = sb.toString();
System.out.println(s); //123
}
}