not long after learning java, I encountered a relatively basic problem. I don"t know the cause. I hope I can get an answer! Thank you.
System.out.println ((StuArr [I] .SName = = "Zhang San"));
System.out.println) ((findName== "Zhang San"));
these two statements, the first is ture, and the second is false.
StuArr [I] .SName assigns an object to StuArr [I], and then the object has a SName member. The content is "Zhang San".
findName is of type String. The content assigned is the content of enter after typing one line on the br.readLine (), keyboard, and the content is also "Zhang San".
although I know that comparison objects need to use equal, why is it the same string comparison? the second string obtained from the screen, "Zhang San" and "Zhang San", returns false?
if I analyze it from the perspective of what I have learned so far, StuArr [I] .SName and findName have the same hashcode, their addresses should be the same, so they should return ture. It didn"t turn out like this.
in addition, I have a little guess whether the string obtained by br.readLine () is in the heap and the SName member variable is in the stack, so it will be different by comparison, but how can this be verified?
the specific code is as follows:
import java.io.*;
import java.util.*;
class students
{
private String SName;
private int SAge;
private static students[] StuArr;
{
StuArr = new students[10];
}
//
students()
{
}
students(String sname,int sage)
{
this.SName = sname;
this.SAge = sage;
}
//SNamesetget
public void setSName(String sname)
{
this.SName = sname;
}
public String getSName()
{
System.out.println(":" + this.SName);
return this.SName;
}
//SAgesetget
public void setSAge(int sage)
{
this.SAge = sage;
}
public int getSAge()
{
System.out.println(":" + this.SAge);
return this.SAge;
}
//studentsStuArr[]
public void setStuArr(students stu)
{
for (int i = 0;i<10 ;iPP )
{
if (StuArr[i] == null)
{
StuArr[i] = stu;
return;
}
}
System.out.println("");
}
public static void main(String[] args) throws Exception
{
students stu1 = new students();
//2
stu1.setSName("");
stu1.setSAge(18);
students stu2 = new students("",19);
//StuArr
stu1.setStuArr(stu1);
stu2.setStuArr(stu2);
System.out.println("============================");
System.out.println(":");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String findName = null;
while ((findName=br.readLine()) != null)
{
for (int i = 0; i < 10 ;iPP )
{
if (StuArr[i] == null)
{
continue;
}
else
{
System.out.println((""==findName));
if (StuArr[i].SName == findName)
{
System.out.println(":");
System.out.println(":"+StuArr[i].SName);
System.out.println(":"+StuArr[i].SAge);
}
}
}
}
}
}