我想運行下面的代碼。這是給我的錯誤簡單的Java錯誤代碼,同時編譯
運營商+未定義的參數類型的字符串,無效
public class CompStrings{
String s1=null;
String s2=null;
public void testMethod(){
s1 = new String("Hello");
s2 = new String("Hello");
String str3="abc";
String str4 ="abc";
/**
* ==
*/
if(str3==str4){
System.out.println("str3==str4 is true");
}else{
System.out.println("str3==str4 is false");
}
if(str3.equals(str4)){
System.out.println("str1.equals(str2) is true");
}else{
System.out.println("str1.equals(str2) is false");
}
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
/*Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.out.println(i1.hashCode());
System.out.println(i2.hashCode());
1)String s1="hello";
String s2="hello";
2)String s1 = new String("Hello");
String s2 = new String("Hello");
3)String s1="hello";
string s2=s1;
**/
}
public static void main(String argv[]){
CompStrings obj= new CompStrings();
// \System.out.println("Calling My Method"+ obj.testMethod());
System.out.println("Hashcode for emp1 = " + obj.testMethod());// Here it gives Error
}
public boolean equals(Object o){
String s = (String) o;
if (s1.equals(s)){
return true;
}else{
return false;
}
}
public int hashCode(){
return s1.hashCode();
}
}
感謝每一個機構 –