Java——Object类,String类,StringBuffer类

一、Object类

Object类是Java语言中的根类,即所有类的父类。它中描述的所有方法子类都可以使用。所有类在创建对象的时候,最终找的父类就是Object

1、equals方法

equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较Object类中的equals方法内部使用的就是==比较运算符。

Person.java

package cn.xinge.demo1;

/*
 	描述人这个类,并定义功能根据年龄判断是否是同龄人
 	由于要根据指定类的属性进行比较,这时只要覆盖Object中的equals方法
 	在方法体中根据类的属性值进行比较
 */
class Person{
    int age ;
    public Person(int age){
        this.age = age;
    }
    //复写父类的equals方法,实现自己的比较方式
    public boolean equals(Object obj) {
        //判断当前调用equals方法的对象和传递进来的对象是否是同一个
        if(this == obj){
            return true;
        }
        //判断传递进来的对象是否是Person类型
        if(!(obj instanceof Person)){
            return false;
        }
        //将obj向下转型为Perosn引用,访问其属性
        Person p = (Person)obj;
        return this.age == p.age;
    }
}

TestPerson.java

package cn.xinge.demo1;

public class TestPerson {
    public static void main(String[] args) {
        Person p1 = new Person(18);     //向上转型为Object类型
        Person p2 = new Person(19);
        boolean b = p1.equals(p2);
        System.out.println(b);
    }
}

2、toString方法

toString方法返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值。

由于toString方法返回的结果是内存地址,而在开发中,经常需要按照对象的属性得到相应的字符串表现形式,因此也需要重写它。

package cn.xinge.demo1;


public class TestToString {
    public static void main(String[] args) {
        //调用Person类的方法toString()
        //输出语句中,写的是一个对象,默认调用对象的toString方法
        Person p = new Person(20);
        String s = p.toString();
        System.out.println(p);
        System.out.println(s);
    }
}

二、String类 

Java中万物皆对象,字符串也是对象,类是描述事物,String类描述字符串对象的类,字符串是一个常量,一旦创建,不能改变

package cn.xinge.demo2.demo1;

public class StringDemo1 {
    public static void main(String[] args) {
        //字符串定义方式2个, 直接=  使用String类的构造方法
        String str1 = new String("abc");
        String str2 = "abc";
        System.out.println(str1);
        System.out.println(str2);

        System.out.println(str1==str2);//引用数据类型,比较对象的地址 false
        System.out.println(str1.equals(str2));//true
    }
}

原因

1、构造方法

a.传递字符数组

/*
 * String(char[] value) 传递字符数组
 * 将字符数组,转成字符串, 字符数组的参数,不查询编码表
 *
 * String(char[] value, int offset, int count) 传递字符数组
 * 将字符数组的一部分转成字符串
 * offset  数组开始索引
 * count   个数
 */
public static void function_1(){
    char[] ch = {'a','b','c','d','e','f'};
    //调用String构造方法,传递字符数组
    String s = new String(ch);
    System.out.println(s);

    String s1 = new String(ch,1,4);
    System.out.println(s1);
}

b.传递字节型数组

/*
 *  定义方法,String类的构造方法
 *  String(byte[] bytes)  传递字节数组
 *  字节数组转成字符串
 *  通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
 *  平台 : 机器操作系统
 *  默认字符集: 操作系统中的默认编码表, windows默认编码表GBK
 *  将字节数组中的每个字节,查询了编码表,得到的结果
 *  字节是负数,汉字的字节编码就是负数, GBK编码表 ,一个汉字采用2个字节表示
 *
 *  String(byte[] bytes, int offset, int length) 传递字节数组
 *  字节数组的一部分转成字符串
 *  offset 数组的起始的索引
 *  length 个数,转几个   , 不是结束的索引
 */
public static void function(){
    byte[] bytes = {97,98,99,100};
    //调用String类的构造方法,传递字节数组
    String s = new String(bytes);
    System.out.println(s);

    byte[] bytes1 ={65,66,67,68,69};
    //调用String构造方法,传递数组,传递2个int值
    String s1 = new String(bytes1,1,3);
    System.out.println(s1);
}

2、各种方法

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

public static void function(){
    String str = "cfxdf#$REFewfrt54GT";
    //调用String类方法length,获取字符串长度
    int length = str.length();
    System.out.println(length);
}

b)String substring 获取字符串的一部分

/*
 *  String substring(int beginIndex,int endIndex) 获取字符串的一部分
 *  返回新的字符串
 *  包含头,不包含尾巴
 *
 *  String substring(int beginIndex)获取字符串的一部分
 *  包含头,后面的字符全要
 */
public static void function_2(){
    String str = "howareyou";
    //调用String类方法substring获取字符串一部分
    str= str.substring(1, 5);
    System.out.println(str);

    String str2 = "HelloWorld";
    str2 = str2.substring(1);
    System.out.println(str2);
}

c)boolean startsWith 判断一个字符串是不是另一个字符串的前缀

/*
 * boolean startsWith(String prefix)
 * 判断一个字符串是不是另一个字符串的前缀,开头
 * howareyou
 * hOw
 */
public static void function_3(){
    String str = "howareyou";
    //调用String类的方法startsWith
    boolean b = str.startsWith("hOw");
    System.out.println(b);
}

d)boolean endsWith 判断一个字符串是不是另一个字符串的后缀,结尾

/*
 * boolean endsWith(String prefix)
 * 判断一个字符串是不是另一个字符串的后缀,结尾
 * Demo.java
 *     .java
 */
public static void function_4(){
	String str = "Demo.java";
	//调用String类方法endsWith
	boolean b = str.endsWith(".java");
	System.out.println(b);
}

e)boolean contains 判断一个字符串中,是否包含另一个字符串

/*
 *  boolean contains (String s)
 *  判断一个字符串中,是否包含另一个字符串
 */
public static void function_5(){
	String str = "itcast.cn";
	//调用String类的方法contains
	boolean b =str.contains("ac");
	System.out.println(b);
}

f)int indexOf(char ch) 查找一个字符,在字符串中第一次出现的索引

/*
 *  int indexOf(char ch)
 *  查找一个字符,在字符串中第一次出现的索引
 *  被查找的字符不存在,返回-1
 */
public static void function_6(){
	String str = "itcast.cn";
	//调用String类的方法indexOf
	int index = str.indexOf('x');
	System.out.println(index);
}

g)byte[] getBytes()  将字符串转成字节数组

/*
 *  byte[] getBytes() 将字符串转成字节数组
 *  此功能和String构造方法相反
 *  byte数组相关的功能,查询编码表
 */
public static void function_7(){
	String str = "abc";
	//调用String类方法getBytes字符串转成字节数组
	byte[] bytes = str.getBytes();
	for(int i = 0 ; i < bytes.length ; i++){
		System.out.println(bytes[i]);
	}
}

h)char[] toCharArray() 将字符串转成字符数组

/*
 * char[] toCharArray() 将字符串转成字符数组
 * 功能和构造方法相反
 */
public static void function_8(){
	String str = "itcast";
	//调用String类的方法toCharArray()
	char[] ch = str.toCharArray();
	for(int i = 0 ; i < ch.length ; i++){
		System.out.println(ch[i]);
	}
}

i)boolean equals(Object obj) 判断字符串中的字符是否完全相同

/*
 *  boolean equals(Object obj)
 *  方法传递字符串,判断字符串中的字符是否完全相同,如果完全相同返回true
 *  
 *  boolean equalsIgnoreCase(String s)
 *  传递字符串,判断字符串中的字符是否相同,忽略大小写
 */
public static void function_9(){
	String str1 = "Abc";
	String str2 = "abc";
	//分别调用equals和equalsIgnoreCase
	boolean b1 = str1.equals(str2);
	boolean b2 = str1.equalsIgnoreCase(str2);
	System.out.println(b1);
	System.out.println(b2);
}

示例:

题目一:获取指定字符串中,大写字母、小写字母、数字的个数。

/*
 * 获取指定字符串中,大写字母、小写字母、数字的个数。
 * 思想:
 *   1. 计数器,就是int变量,满足一个条件 ++
 *   2. 遍历字符串, 长度方法length() + charAt() 遍历
 *   3. 字符判断是大写,是小写,还是数字
 */
public static void getCount(String str){
	//定义三个变量,计数
	int upper = 0;
	int lower = 0;
	int digit = 0;
	//对字符串遍历
	for(int i = 0 ; i < str.length() ; i++){
		//String方法charAt,索引,获取字符
		char c = str.charAt(i);
		//利用编码表 65-90  97-122  48-57
		if(c >='A' && c <=90){
			upper++;
		}else if( c >= 97 && c <= 122){
			lower++;
		}else if( c >= 48 && c <='9'){
			digit++;
		}
	}
	System.out.println(upper);
	System.out.println(lower);
	System.out.println(digit);
}

题目二:将字符串中,第一个字母转换成大写,其他字母转换成小写,并打印改变后的字符串。

/*
 *  将字符串的首字母转成大写,其他内容转成小写
 *  思想:
 *    获取首字母, charAt(0)  substring(0,1)
 *    转成大写 toUpperCase()
 *    
 *    获取剩余字符串, substring(1)  toLowerCase()
 */
public static String toConvert(String str){
	//定义变量,保存首字母,和剩余字符
	String first = str.substring(0,1);
	String after = str.substring(1);
	//调用String类方法,大写,小写转换
	first = first.toUpperCase();
	after = after.toLowerCase();
	return first+after;
}

题目三:查询大字符串中,出现指定小字符串的次数。如“hellojava,nihaojava,javazhenbang”中查询出现“java”的次数。

/*
 *  获取一个字符串中,另一个字符串出现的次数
 *  思想:
 *    1. indexOf到字符串中到第一次出现的索引
 *    2. 找到的索引+被找字符串长度,截取字符串
 *    3. 计数器++
 */
public static int getStringCount(String str, String key){
	//定义计数器
	int count = 0;
	//定义变量,保存indexOf查找后的索引的结果
	int index = 0;
	//开始循环找,条件,indexOf==-1 字符串没有了
	while(( index = str.indexOf(key) )!= -1){
		count++;
		//获取到的索引,和字符串长度求和,截取字符串
		str = str.substring(index+key.length());
	}
	return count;
} 

三、StringBuffer类

字符串缓冲区支持可变的字符串,StringBuffer是个字符串的缓冲区,即就是它是一个容器,容器中可以装很多字符串。并且能够对其中的字符串进行各种操作。

相比较String类,省空间

1、append方法

/*
 *  StringBuffer类方法
 *   StringBuffer append, 将任意类型的数据,添加缓冲区
 *   append 返回值,写return this
 *   调用者是谁,返回值就是谁
 */
public static void function(){
	StringBuffer buffer = new StringBuffer();
	//调用StringBuffer方法append向缓冲区追加内容
	buffer.append(6).append(false).append('a').append(1.5);
	System.out.println(buffer);
}

2、delete方法

/*
 * StringBuffer类方法
 *   delete(int start,int end) 删除缓冲区中字符
 *   开始索引包含,结尾索引不包含
 */
public static void function_1(){
	StringBuffer buffer = new StringBuffer();
	buffer.append("abcdef");
	
	buffer.delete(1,5);
	System.out.println(buffer);
}

3、insert方法

/*
 *  StringBuffer类方法 insert
 *    insert(int index, 任意类型)
 *  将任意类型数据,插入到缓冲区的指定索引上
 */
 public static void function_2(){
	 StringBuffer buffer = new StringBuffer();
	 buffer.append("abcdef");	 
	 
	 buffer.insert(3, 9.5);
	 System.out.println(buffer);
 }

4、replace方法

/*
 *  StringBuffer类方法
 *    replace(int start,int end, String str)
 *    将指定的索引范围内的所有字符,替换成新的字符串
 */
public static void function_3(){
	StringBuffer buffer = new StringBuffer();
	buffer.append("abcdef");
	
	buffer.replace(1, 4, "Q");
	
	System.out.println(buffer);
}

5、reverse方法

/*
 *  StringBuffer类的方法
 *    reverse() 将缓冲区中的字符反转
 */
public static void function_4(){
	StringBuffer buffer = new StringBuffer();
	buffer.append("abcdef");
	
	buffer.reverse();
	
	System.out.println(buffer);
}

6、toString方法

/*
 *  StringBuffer类的方法
 *   String toString() 继承Object,重写toString()
 *   将缓冲区中的所有字符,变成字符串
 */
public static void function_5(){
	StringBuffer buffer = new StringBuffer();
	buffer.append("abcdef");
	buffer.append(12345);
	
	//将可变的字符串缓冲区对象,变成了不可变String对象
	String s = buffer.toString();
	System.out.println(s);
}

四、StringBuilder

它也是一个可变的字符序列。此类提供一个与StringBuffer 兼容的API,但不保证同步。该类被设计用作StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(该类线程不安全的)。如果可能,建议优先采用该类,因为在大多数实现中,它比StringBuffer 要快。 

原文地址:https://www.cnblogs.com/x54256/p/8421943.html