JAVA 基础知识整理-08 Object类,Scanner类,String类

1. Object类
1)Object 类是类层次结构的根类,其他所有的类都直接或者间接地继承Object类。
2)Object类的构造方法只有一个且为无参构造方法。
3)方法:
a. toString() 返回对象的字符串表示,默认是由类的全路径+‘@’+hascode的十六进制表示。一般子类都会重写该方法。用eclipse可以自动生成toString方法。

class People{
	private int age = 10;
	
	@Override
	public String toString() {
		return "People [age=" + age + "]";
	}
}

b. equals()
比较两个对象是否相同,默认比较的是地址值是否相等。一般子类也会重写该方法。

class People{
	private int age = 10;
	
	@Override
	public String toString() {
		return "People [age=" + age + "]";
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		People other = (People) obj;//向下转型,将obj 转成 People的类对象,用于下面age参数的调用比较。
		if (age != other.age)
			return false;
		return true;
	}
}

c. hasCode() 返回对象的hashcode,类似于地址编号,但并不是实际地址值。
d. getClass() 返回对象的字节码文件对象。
e. finalize() 用于垃圾回收,再不确定的时间。
f. clone() 可以实现对象的克隆,包括成员变量的数据复制。它和两个引用指向同一个对象有区别。

2. == 和 equals()的区别
==:基本类型–值
引用类型–地址
equals():只能比较引用类型。默认比较的是地址值是否相同。子类中可以重写该方法以适应需求。

3. Scanner
构造方法:
InputStream is = System.in
Scanner(InputStream is)
常用格式:
Scanner sc = new Scanner(System.in);
基本方法格式:
1).hasNextXxx() 判断是否是某种类型的
2).nextXxx() 返回某种类型的元素
常用的方法:
nextInt(): 获取int类型的值
nextLine():获取一个String类型的值
nextInt() 和nextLine()使用时要注意:
先获取一个数值,再获取一个字符串,会出现问题。
解决:
1)先获取一个数值,再创建一个Scanner对象,再用新的Scanner对象获取字符串。

		Scanner sc = new Scanner(System.in);
		Scanner sc1 = new Scanner(System.in);
		int a = sc.nextInt();
		String b = sc1.nextLine();
		System.out.println("a:"+a+"b:"+b);
		
		输出:
		11
		asd
		a:11b:asd

2)把所有的数据都先按照字符串获取,然后再对应转换

4. String类
字符串是由多个字符组成的一串数据。也可看做字符数组。

特点:
1)字符串是常量,创建后不可改变。
2)字符串字面值"abc"也可以看成是一个字符串对象。

常用构造方法:
public String()无参构造
public String(byte[ ] bytes)把字节数组转成字符串
public String(byte[ ] bytes,int offset,int length)把字节数组的一部分转成字符串(int offset代表从截取字符串的第一个字符的下标,int length代表截取的字符个数)
public String(char[ ] value)把字符数组转成字符串
public String(char[ ] value,int offset,int length)把字符数组的一部分转成字符串(int offset代表从截取字符串的第一个字符的下标,int length代表截取的字符个数)
public String(String original)把字符串常量值转成字符串

public int length() 返回此字符串的长度

		byte[] byt = {101,102,123};
		String s = new String(byt);
		System.out.println(s);
输出:ef{

理解:
1)字符串赋值内存图解:
在这里插入图片描述字符串不能被改变,指的是它的值不能被改变,比如“abc”,比如上图中的“hello”,“world”…

2)String s = new String(“hello”) 和 String s = "hello"的区别:
在这里插入图片描述3)字符串如果是变量相加,先开空间,再拼接;字符串如果是常量相加,先拼接,然后到常量池找,如果有就直接返回,没有就创建再返回。

		String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);
		System.out.println(s3.equals(s1+s2));
		System.out.println(s3 == "hello" + "world");
		System.out.println(s3.equals("hello" + "world"));
输出:
false
true
true
true

判断功能:
boolean equals(object obj) 比较字符串内容是否相同
boolean equalsIgnoreCase(String str) 比较字符串时不区分大小写
boolean contains(String str) 判断字符串中是否包含特定子字符串
boolean startsWith(String str)判断是否以某个子字符串开头
boolean endsWith(String str)判断是否以某个字符串结尾
boolean isEmpty()判断字符串内容是否为空(注意要区别字符串内容为空String s = ""和字符串对象为空String s = null; 如果字符串对象为空那么便不能调用方法,如果调用就会报错)

获取功能:
int length() 获取字符串长度
char charAt(int index) 获取某个索引的字符
int indexOf(int ch)获取某个字符在字符串中第一次出现的索引(‘a’和97都可以代表‘a’)
int indexOf(String str)获取某个子字符串在字符串中第一次出现的索引(包含指定索引)
int indexOf(int ch,int fromIndex)获取指定字符从指定位置往后第一次出现的索引(包含指定索引)
int indexOf(String str, int fromIndex)获取指定字符串从指定位置往后第一次出现的索引
String substring(int start)从指定位置开始截取字符串,默认到末尾(含start)
String substring(int start,int end)从指定位置开始到指定位置结束截取字符串。(含start,不含end)

转换功能:
byte[ ] getBytes() 把字符串转为字节数组
char[ ] toCharArray() 把字符串转为字符数组
static String valueOf(char[ ] chs) 把字符数组转为字符串(通过静态方法)
static String valueOf(int i)把整数转为字符串(通过静态方法)
String toLowerCase()将字符串中的字符转成小写的生成一个新的字符串
String toUpperCase()将字符串中的字符转成大写的生成一个新的字符串
String concat(String str)把字符串拼接

		char[] chs = {'a','b','c'};
		String newS = String.valueOf(chs);
		System.out.println(newS);
		System.out.println(newS.toUpperCase());
		System.out.println(newS.concat("LOVE"));
		byte[] byt0 = newS.getBytes();
		for(int x = 0; x<byt.length;x++){
			System.out.println(byt0[x]);
		}
		
		输出结果:
		abc
		ABC
		abcLOVE
		97
		98
		99


案例–将字符串首字母大写其他小写:

String s = "helloWORLD";
String newS = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase())

其他功能:
String replace(char old, char new)将字符串中的指定字符替换为新的字符
String replace(String old, String new)将字符串中的指定字符串替换为新的字符串
String trim()去除字符串两端的空格
int compareTo(String str) 按照字典顺序比较两个字符串,
int compareToIgnoreCase(String str)按照字典顺序比较两个字符串
注:compareTo先遍历两个字符串,如果其中一个已经遍历结束,还没有出现有不相同的字符,那么返回的就是两个字符串长度的差值,外字符串长-内字符(括号内被当做参数的字符串)串长,如果在此之前出现不同的字符,那么就比较这两个字符在字典中的大小(字母表中排序越靠后越大),并返回差值。
compareTo 源码:

public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

5. StringBuffer类
线程安全的可变字符串。
String和StringBuffer的区别:
String:长度和内容不可变。字符串拼接浪费空间。
StringBuffer:长度和内容可变。字符串拼接不会造成过多浪费。
安全–同步–数据不安全
不安全–不同步–效率高一些

构造方法:
public StringBuffer();
public StringBuffer(int capacity);指定容量的字符串缓冲去对象
public StringBuffer(String str);指定字符串内容的字符串缓冲区对象

方法:
public int capacity();返回当前容量,理论值
public int length();返回长度(字符数),实际值

		StringBuffer sb = new 				StringBuffer("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		//sb = ;
		System.out.println(sb.capacity());
		System.out.println(sb.length());
		输出:
		47  //16+length();16+31 = 47
		31

添加功能:
public StringBuffer append(String str):
可以把任意类型的数据添加到字符串缓冲区中,并返回字符串缓冲区本身。
public StringBuffer insert(int offset, String str) 在指定位置将指定类型的数据插入字符串缓冲区里面。返回字符串缓冲区本身。

删除功能:
public StringBuffer delete(int start, int end) 删除指定开始位置到指定结束位置的字符,并返回字符串缓冲区本身(包左不包右)
public StringBuffer deleteCharAt(int index) 删除指定位置的字符,并返回字符串缓冲区本身

替换功能:
StringBuffer replace(int start, int end, String str) 用指定的String中的字符替换此序列的子字符串中的String 。 子串开始于指定start并延伸到字符索引end - 1 ,或如果没有这样的字符存在的序列的结束。

反转功能:
StringBuffer reverse() 将字符串缓冲区中的字符倒序

截取功能:
String substring(int start) 返回一个截取的新的字符串,而字符串缓冲区本身不变,从start一直截取到末尾
String substring(int start, int end) 返回一个截取的新的字符串,而字符串缓冲区本身不变,从start一直截取到end-1

String和StringBuffer的相互转换:
String toString() 将StringBuffer转成String类型
StringBuffer(String str)将String转成StringBuffer类型

StringBuilder类
一个可变的字符序列。 此类提供与StringBuffer的API,但不保证同步。 此类设计用作简易替换为StringBuffer在正在使用由单个线程字符串缓冲区的地方(如通常是这种情况)。 在可能的情况下,建议使用这个类别优先于StringBuffer ,因为它在大多数实现中将更快。

String,StringBuffer和StringBuilder的区别:
String的内容是不可变的,而StringBuffer和StringBuilder都是内容可变的。
StringBuffer是同步的,数据安全,效率低;
StringBuilder是不同步的,数据不安全效率高。

StringBuffer和数组的区别:
两者都可以看做是一个容器,StringBuffer的数据最终是一个字符串数据。而数组可以放多种类型的数据,但必须是同一个数组中必须放同一种数据。

String和StringBuffer作为参数传递:

基本类型:形式参数的改变不影响实际参数
引用类型:形式参数的改变直接影响实际参数

注:String 作为参数传递,效果和基本类型作为参数传递是一样的。

public class StringBufferDemo {
	public static void main(String[] args) {
		StringBuffer sb0 = new StringBuffer("hello");
		StringBuffer sb1 = new StringBuffer("world");
		System.out.println("sb0:"+sb0+sb0.hashCode());
		System.out.println("sb1:"+sb1+sb1.hashCode());
		System.out.println("============================");
		change(sb0,sb1);
		System.out.println("============================");
		System.out.println("sb0:"+sb0+sb0.hashCode());
		System.out.println("sb1:"+sb1+sb1.hashCode());
	}
	
	public static void change(StringBuffer sb0, StringBuffer sb1){
		sb0 = sb1;
		sb1.append(sb0);
		System.out.println("sb0:"+sb0+sb0.hashCode());
		System.out.println("sb1:"+sb1+sb1.hashCode());
	}
}

输出:

sb0:hello9949215
sb1:world14721926
============================
sb0:worldworld14721926
sb1:worldworld14721926
============================
sb0:hello9949215
sb1:worldworld14721926
发布了55 篇原创文章 · 获赞 0 · 访问量 2068

猜你喜欢

转载自blog.csdn.net/KathyLJQ/article/details/104468847