天天看点

String类——判断功能String类

String类——判断功能:断点设置、字符串、查看API文档、String类的构造方法、字符串的长度、内存结构图

断点设置:一般情况下,需要在每个方法的第一条有效语句设置断点。

String类

0、String类所在的包:java.lang (所以不需要自己受到导入,系统自动导入)。

1、字符串

(1)字符串:就是由多个字符组成的一串数据,也可以看做是一个字符数组。

2、查看API文档

    (1)字符串字面值常量"abcde"也可以看作是一个字符串对象。

    (2)字符串时常量,一旦被赋值,就不能被更改。 

3、String类的构造方法(学习其中的6个):

(1)public String( )

        空构造方法,默认的无参构造方法

(2)public String(byte[ ] bytes)

        把字节数组转换成字符串

(3)public String(byte[ ] bytes,int offset,int length)

        把字节数组的一部分转换成字符串:从offset开始的length个字符转换为字符串

(4)public String(char[ ] value)

        把字符数组转换成字符串

(5)public String(char[ ] value,int offset,int count)

        把字符数组的一部分转换成字符串

(6)public String(String original)

        把字符串常量值转换成字符串

4、字符串的长度

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

5、举例:构造方法、字符串长度

package cn.itcast_01;

public class StringDemo {
	public static void main(String[] args) {
		// public String()
		// 空构造方法
		String s1 = new String();
		System.out.println("s1=" + s1);
		System.out.println("s1.length()=" + s1.length());
		System.out.println("-------------");

		// public String(byte[] bytes)
		// 把字节数组转化为字符串
		byte[] bys = { 97, 98, 99, 100, 101 };
		String s2 = new String(bys);
		System.out.println("s2=" + s2);
		System.out.println("s2.length()=" + s2.length());
		System.out.println("-------------");

		// public String(byte[] bytes,int offset,int length)
		// 把字节数组的一部分转换成字符串
		// 我现在想得到字符串“bcd”
		String s3 = new String(bys, 1, 3);
		System.out.println("s3=" + s3);
		System.out.println("s3.length()=" + s3.length());
		System.out.println("-------------");

		// public String(char[] value)
		// 把字符数组转换成字符串
		char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '青' };
		String s4 = new String(chs);
		System.out.println("s4=" + s4);
		System.out.println("s4.length()=" + s4.length());
		System.out.println("-------------");

		// public String(char[] value,int offset,int count)
		// 把字符数组的一部分转换成字符串
		String s5 = new String(chs, 5, 3);
		System.out.println("s5=" + s5);
		System.out.println("s5.length()=" + s5.length());
		System.out.println("-------------");
		
		//public String(String original)
		//把字符串常量值转换成字符串
		String s6 = new String("abcabcabcabcabcabc");
		System.out.println("s6=" + s6);
		System.out.println("s6.length()=" + s6.length());
		System.out.println("-------------");
		
		//字符串字面值常量"abcde"也可以看作是一个字符串对象
		String s7 = "abcabcabcabcabcabc";
		System.out.println("s7=" + s7);
		System.out.println("s7.length()=" + s7.length());

	}

}
           

6、String的特点:一旦被赋值,其值就不能被改变。

(1)字符串被赋值的方法:

        字符串直接被赋值的方法  是先到方法区中的字符串常量池中去寻找该字符串,若没找到,则创建字符串并返回地址值给字符串对象。

        一旦被赋值,就不能被改变。(是其值不能被改变,不是引用不能被改变!)

(2)举例:字符串被赋值

package cn.itcast_02;

public class StringDemo {
	public static void main(String[] args) {
		String s = "hello";
		s += "world";
		System.out.println("s=" + s);
		System.out.println("s.length()="+s.length());
	}

}
           

(3)字符串被赋值的内存结构图:

String类——判断功能String类

(4)面试题:

String s = new String("hello");    与    String s = "hello" 有没有区别?

答案:有区别。前者会创建两个对象,后者会创建1个对象。

(5)== 和 equals( ) 的区别:

    1)==    :比较引用类型时,比较的是地址值是否相同。

    2)equals:比较引用类型时,默认比较的也是地址值是否相同,而String类已经重写了equals()方法,比较的是两个字符串内容是否相同。

    3)代码:

package cn.itcast_02;

public class StringDemo2 {
	public static void main(String[] args) {
		String s1 = new String("hello");
		String s2 = "hello";

		System.out.println(s1 == s2); // false
		System.out.println(s1.equals(s2)); // true
	}
}
           

    4)内存结构图:

String类——判断功能String类

(6)面试题:字符串变量相加、字符串常量相加

        A:如果是字符串变量相加,是 先开辟内存空间,再拼接。

        B:如果是字符串常量相加,是 先拼接,然后在常量池中寻找,如果有就直接返回,否则,就开辟内存空间去创建。

package cn.itcast_02;

public class StringDemo2 {
	public static void main(String[] args) {
		String s1 = new String("hello");
		String s2 = "hello";

		System.out.println(s1 == s2); // false
		System.out.println(s1.equals(s2)); // true

		System.out.println("------------");

		String str1 = new String("hello");
		String str2 = new String("hello");
		System.out.println(str1 == str2); // false
		System.out.println(str1.equals(str2)); // true
		System.out.println("------------");

		String str3 = new String("hello");
		String str4 = "hello";
		System.out.println(str3 == str4); // false
		System.out.println(str3.equals(str4)); // true
		System.out.println("------------");

		String str5 = "hello";
		String str6 = "hello";
		System.out.println(str5 == str6); // true
		System.out.println(str5.equals(str6)); // true
		System.out.println("------------");

		// 如果是字符串变量相加,是 先开辟内存空间,再拼接。
		// 如果是字符串常量相加,是 先拼接,然后在常量池中寻找,如果有就直接返回,
		// 否则,就开辟内存空间去创建。
		String str7 = "hello";
		String str8 = "world";
		String str9 = "helloworld";
		System.out.println(str9 == str7 + str8); // false
		System.out.println(str9.equals(str7 + str8)); // true
		
		System.out.println(str9 == "hello" + "world"); // true
		System.out.println(str9.equals("hello" + "world")); // true
		
		//通过反编译工具看源码发现,这里已经做了处理。
		System.out.println(str9 == "helloworld");
		System.out.println(str9.equals("helloworld"));
		
	}

}
           

7、字符串的判断功能

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

B:字符串的判断功能:

(1)boolean equals(Object obj)

        比较字符串的内容是否相同,区分大小写

(2)boolean equalsIgnoreCase(String str)

        比较字符串的内容是否相同,不区分大小写

(3)boolean contains(String str)

        判断大字符串中是否包含小字符串

(4)boolean startsWith(String str)

        判断字符串是否以某个指定的字符串开头

(5)boolean endsWith(String str)

        判断字符串是否以某个指定的字符串结尾

(6)boolean isEmpty( )

        判断字符串是否为空

C:注意:要想调用方法,必须首先创建对象!

D:字符串内容为空:对象存在,但是内容是空的。    String s = "";

E:字符串对象为空:对象都不存在,何谈内容。       String s = null;

F:举例子:

package cn.itcast_03;

public class StringDemo {
	public static void main(String[] args) {
		String s1 = "helloworld";
		String s2 = "helloworld";
		String s3 = "HelloWorld";

		// boolean equals(Object obj)
		// 比较字符串的内容是否相同,区分大小写
		System.out.println("equals:" + s1.equals(s2));//true
		System.out.println("equals:" + s1.equals(s3));//false
		System.out.println("---------------");

		// boolean equalsIgnoreCase(String str)
		// 比较字符串的内容是否相同,不区分大小写
		System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s2));//true
		System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s3));//true
		System.out.println("---------------");

		// boolean contains(String str)
		// 判断大字符串中是否包含小字符串
		System.out.println("contains:" + s1.contains("hello"));//true
		System.out.println("contains:" + s1.contains("hw"));//false
		System.out.println("---------------");

		// boolean startsWith(String str)
		// 判断字符串是否以某个指定的字符串开头
		System.out.println("startsWith:" + s1.startsWith("hello"));//true
		System.out.println("startsWith:" + s1.startsWith("h"));//true
		System.out.println("startsWith:" + s1.startsWith("e"));//false
		System.out.println("---------------");

		// boolean endsWith(String str)
		// 判断字符串是否以某个指定的字符串结尾
		System.out.println("endsWith:" + s1.endsWith("world"));//true
		System.out.println("endsWith:" + s1.endsWith("ld"));//true
		System.out.println("endsWith:" + s1.endsWith("l"));//false
		System.out.println("---------------");

		// boolean isEmpty()
		// 判断字符串是否为空
		System.out.println("isEmpty:" + s1.isEmpty());//false
		System.out.println("isEmpty:" + s2.isEmpty());//false

		String s4 = "";
		String s5 = null;
		System.out.println("isEmpty:" + s4.isEmpty());//true
		//java.lang.NullPointerException:空指针异常
		//System.out.println("isEmpty:" + s5.isEmpty());//报错误!
		System.out.println("---------------");
	}

}
           

继续阅读