1.1、String的两种实例化方式
public String (String original) |
public class StringDemo01
{
public static void main(String args[])
{
String str1 = "hello" ;
String str2 = new String("AUST") ;
System.out.println(str1 + " " + str2);
System.out.println() ;
}
} |
1.2、String的比较方式
public class StringDemo02
{
public static void main(String args[])
{
String str1 = "hello" ;
String str2 = new String("hello") ;
String str3 = str2 ;
System.out.println("str1 == str2 -->" + (str1==str2)); //false
System.out.println("str1 == str3 -->" + (str1==str3)); //false
System.out.println("str2 == str3 -->" + (str2==str3)); //true
}
} |
public class StringDemo03
{
public static void main(String args[])
{
String str1 = "hello" ;
String str2 = new String("hello") ;
String str3 = str2 ;
System.out.println("str1 equals str2 -->" + str1.equals(str2)); //true
System.out.println("str1 equals str3 -->" + str1.equals(str3)); //true
System.out.println("str2 equals str3 -->" + str2.equals(str3)); //true
}
} |
1.3、一个字符串实际上就是String的匿名对象
public class StringDemo04
{
public static void main(String args[])
{
String str1 = "hello" ;
System.out.println("hello".equals(str1));
}
} |
1.4、String的两种实例化方式的区别
String str1 = “hello” ; |
String str2 = new String(“hello”) ; |
public class StringDemo05
{
public static void main(String args[])
{
String str1 = "hello" ;
String str2 = "hello" ;
String str3= "hello" ;
System.out.println("str1 == str2 -->" + (str1==str2)); //true
System.out.println("str1 == str3 -->" + (str1==str3)); //true
System.out.println("str2 == str3 -->" + (str2==str3)); //true
}
} |
1.5、字符串的内容一旦声明后则无法修改
public class StringDemo06
{
public static void main(String args[])
{
String str1 = "hello" ;
str1 += " world" ;
System.out.println(str1);
}
} |
public class StringDemo07
{
public static void main(String args[])
{
String str1 = "hello" ;
for (int i = 0 ; i< 100 ; i ++ )
{
str1 += i ;
}
System.out.println(str1);
}
} |