java code :
import java.io.*;
class Test
{
private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out,true);
private static PrintWriter stdErr = new PrintWriter(System.err,true);
public static void main(String[] args) throws IOException{
String a = stdIn.readLine(); //我从键盘输入 Y
stdOut.println(a == "Y");
}
}//Output:
false
为什么一直是false,我知道readLine会在末尾加上一个/n表示读取结束,但当我把a == "Y"
改为a == 'Y/n" 时,为什么还是false,郁闷啊,请问有什么解决的办法吗?
import java.io.*;
class Test
{
private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out,true);
private static PrintWriter stdErr = new PrintWriter(System.err,true);
public static void main(String[] args) throws IOException{
String a = stdIn.readLine(); //我从键盘输入 Y
stdOut.println(a == "Y");
}
}//Output:
false
为什么一直是false,我知道readLine会在末尾加上一个/n表示读取结束,但当我把a == "Y"
改为a == 'Y/n" 时,为什么还是false,郁闷啊,请问有什么解决的办法吗?
==比较的是两个对象的地址。Object类的equals方法和==比较的都是地址。String类覆盖了Object类的equals方法,相等的业务逻辑被覆盖为只要所含字符序列相等,即认为两者相等。
C#用ReferenceEquals比较地址相同,类似java的==