1.String 为引用数据类型,打印输出应该是地址,但结果并不是我们想的那样
如:String s1=new String("hello");//结果不是地址,而是hello
2.String 的直接赋值和调用构造方法赋值 所指向的地址值不同
如 String s1=new String("hello");//这里的s1先指向堆内存中new出来的对象, 再通过new出来的对象指向方法区常量池中的hello
String s2="hello";//这里的s2是直接指向常量池中的hello的
因此 s1和s2的地址是不同的
虽然地址不同,但在打印结果上都是hello (按照常理来说s1应当是地址值,结果不是,这就是String的特殊之处)
3.String 虽说是引用数据类型,但形式参数的改变不影响实际参数的改变 这点上和基本数据类型是相同的
例如:
public static void main(String[] args) {
String s=new String("hello");
add(s); //helloworld
System.out.println(s); //hello
}
public static void add(String s){
String s1="world";
s+=s1;
System.out.println(s);
}
如:String s1=new String("hello");//结果不是地址,而是hello
2.String 的直接赋值和调用构造方法赋值 所指向的地址值不同
如 String s1=new String("hello");//这里的s1先指向堆内存中new出来的对象, 再通过new出来的对象指向方法区常量池中的hello
String s2="hello";//这里的s2是直接指向常量池中的hello的
因此 s1和s2的地址是不同的
虽然地址不同,但在打印结果上都是hello (按照常理来说s1应当是地址值,结果不是,这就是String的特殊之处)
3.String 虽说是引用数据类型,但形式参数的改变不影响实际参数的改变 这点上和基本数据类型是相同的
例如:
public static void main(String[] args) {
String s=new String("hello");
add(s); //helloworld
System.out.println(s); //hello
}
public static void add(String s){
String s1="world";
s+=s1;
System.out.println(s);
}