String类的构造方法:
package cn.itcast_01;
/*
* 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
* 通过查看API,我们可以知道
* A:字符串字面值"abc"也可以看成是一个字符串对象。
* B:字符串是常量,一旦被赋值,就不能被改变。
*
* 构造方法:
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
*
* 字符串的方法:
* public int length():返回此字符串的长度。
*/
public class StringDemo {
public static void main(String[] args) {
// public String():空构造
String s1 = new String();
System.out.println("s1:" + s1); //s1:
System.out.println("s1.length():" + s1.length()); //s1.length():0
// public String(byte[] bytes):把字节数组转成字符串
byte[] bys = { 97, 98, 99, 100, 101 };
String s2 = new String(bys);
System.out.println("s2:" + s2); //s2:abcde
System.out.println("s2.length():" + s2.length()); //s2.length():5
// public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
// 我想得到字符串"bcd"
String s3 = new String(bys, 1, 3);
System.out.println("s3:" + s3); //s3:bcd
System.out.println("s3.length():" + s3.length()); //s3.length():3
// public String(char[] value):把字符数组转成字符串
char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };
String s4 = new String(chs);
System.out.println("s4:" + s4); //s4:abcde爱林亲
System.out.println("s4.length():" + s4.length()); //s4.length():8
// public String(char[] value,int index,int count):把字符数组的一部分转成字符串
String s5 = new String(chs, 2, 4);
System.out.println("s5:" + s5); //s5:cde爱
System.out.println("s5.length():" + s5.length()); //s5.length():4
//public String(String original):把字符串常量值转成字符串
//这样写意义不大
String s6 = new String("abcde");
System.out.println("s6:" + s6); //s6:abcde
System.out.println("s6.length():" + s6.length()); //s6.length():5
//字符串字面值"abc"也可以看成是一个字符串对象。
String s7 = "abcde";
System.out.println("s7:"+s7); //s7:abcde
System.out.println("s7.length():"+s7.length()); //s7.length():5
//补充:最后一个这样写比较好,大部分情况下用这个,总之要知道字节数组和字符数组都可以转成字符串
}
}
String的特点一旦被赋值就不能改变:
package cn.itcast_02;
/*
* 字符串的特点:一旦被赋值,就不能改变。
*/
public class StringDemo {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println("s:" + s); // helloworld
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
面试题:String字面值对象和构造方法创建对象的区别
package cn.itcast_02;
/*
* String s = new String(“hello”)和String s = “hello”;的区别?
* 有。前者会创建2个对象,后者创建1个对象。
*
* ==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
*/
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
}
}
看程序写结果:
package cn.itcast_02;
/*
* 看程序写结果
*/
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2));// true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4); // false
System.out.println(s3.equals(s4));// true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6); // true
System.out.println(s5.equals(s6));// true
}
}
package cn.itcast_02;
/*
* 看程序写结果
* 字符串如果是变量相加,先开空间,再拼接。
* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
*/
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2); // false,变量相加,先开空间,再拼接
System.out.println(s3.equals((s1 + s2)));// true
// 通过反编译看源码,我们知道这里已经做好了处理。
System.out.println(s3 == "hello" + "world"); // true,常量相加,是先加,然后在常量池找,如果有就直接返回
// System.out.println(s3 == "helloworld");
System.out.println(s3.equals("hello" + "world"));// true
}
}
String类的判断功能:
package cn.itcast_03;
/*
* String类的判断功能:
* 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;对象都不存在哪能调方法呀
*/
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
// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("equals:" + s1.equalsIgnoreCase(s2)); //true
System.out.println("equals:" + s1.equalsIgnoreCase(s3)); //true
// boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println("contains:" + s1.contains("hello")); //true
System.out.println("contains:" + s1.contains("hw")); //false,h和w必须连一起的
// boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System.out.println("startsWith:" + s1.startsWith("h")); //true
System.out.println("startsWith:" + s1.startsWith("hello"));//true
System.out.println("startsWith:" + s1.startsWith("world"));//false
// 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
// boolean isEmpty():判断字符串是否为空。
System.out.println("isEmpty:" + s1.isEmpty()); //false
String s4 = "";
System.out.println("isEmpty:" + s4.isEmpty()); //true
String s5 = null;
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());// NullPointerException
}
}
补充:
String中对NULL和""的判断:
//错误用法一:
if (name == "") {
//do something
}
//错误用法二:
if (name.equals("")) {
//do something
}
//错误用法三:
if (!name.equals("")) {
//do something
}
//正确的写法应该先加上name != null的条件,如例:
if (name != null && !name.equals("")) {
//do something
}
//或者
if (!"".equals(name)) {//将""写在前头,这样,不管name是否为null,都不会出错。
//do something
}
再补充:
1.
null表示这个字符串不指向任何的东西,如果这时候你调用它的方法,那么就会出现空指针异常。
""表示它指向一个长度为0的字符串,这时候调用它的方法是安全的。
2.
null不是对象,""是对象,所以null没有分配空间,""分配了空间,例如:
String s1= ""; s1引用一个空串,s1已经实例化
String s2 = null; s2引用为空,s2还不是一个实例化的对象
3.
对象用equals比较,null用等号比较。
如果str=null;下面的写法错误:
if(str.equals("")||str==null){
正确的写法是:
//先判断是不是对象,如果是,再判断是不是空字符串
if(str==null||str.equals("")){
}
4.
判断一个字符串是否为空,首先就要确保他不是null,然后再判断他的长度。
String str = xxx;
if(str != null && str.length() != 0) {
}
再补充:今天看到我们项目中判断字符串是否为空是这样写的
if(str == null || str.trim().length() <= 0){
}
5.判断字符串是否为空的效率
补充:如果在其他类中想要调用Hello类中的function2()方法,要么将方法加static改成静态方法,要么new出Helllo对象调function2()方法
参考:http://blog.youkuaiyun.com/qq_27918787/article/details/52506406