String
1.String a=new String(“aaa”):
a引用放在栈中,new会在常量池中检查有没有这个字符串,没有的话就在常量池中创建这个字符串对象,但是不管常量池中有没有达欧汇在堆中创建这个字符串对象也就是说这种方法会创建一个或两个字符串对象。
string a=“aaa”;a引用同样在栈中则会在常量池中检查,没有的话在常量池中创建这个对象。
对象地址只想问题
例子
public class TestArgs4{
public static void main(String[] args){
Student s = new Student(30);
change(s);
System.out.println(s.age);//结果55
}
public static void change(Student stu){
//Student stu = s;
Student s1 = new Student(100);
stu.age = 55;
stu=s1;
stu.age=111;
System.out.println(stu.age);//1结果11
System.out.println(s1.age);//结果111
}//stu是局部变量
}
class Student{
int age;
public Student(int age){
this.age = age;
}
}
注如果对象没有new则和传的值使用一个地址
Student s = new Student(30);
public static void change(Student stu){}
s和stu在传值时使用一个地址
Student s1 = new Student(100);
stu=s1;//此时stu地址给改为了s1所在的地址
stu.age=111;
用一个引用变量给两一个引用形变量赋值穿的是改变量的地址