代码如下:
byte[] b = new byte[20];
System.in.read(b);
String str = new String(b).trim(); if(str == "XYZ") {
System.out.println("Good");
}
else {
System.out.println("Bad");
};此时我在控制台输入XYZ,但为什么执行的是else中的代码?
byte[] b = new byte[20];
System.in.read(b);
String str = new String(b).trim(); if(str == "XYZ") {
System.out.println("Good");
}
else {
System.out.println("Bad");
};此时我在控制台输入XYZ,但为什么执行的是else中的代码?
即
if(str.equals("XYZ")) {
System.out.println("Good");
}P.S :楼主可以去研究下equals和==的区别。
if("XYZ".equals(str))
byte[] b = new byte[20];
try {
System.in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
String str = new String(b).trim();
//System.out.println(str); if (str.equals("XYZ")) {
System.out.println("Good");
} else {
System.out.println("Bad");
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
byte[] inBytes = new byte[3];
System.in.read(inBytes);
String content = new String(inBytes); System.out.println(("XYZ".equals(content))); }