gpt4 book ai didi

java - 编译时的简单 Java 错误代码

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:32 25 4
gpt4 key购买 nike

我正在尝试运行以下代码。它给我错误

The operator + is undefined for the argument type(s) String, void

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();

}

}

最佳答案

testMethod() 显然不返回任何内容,这就是没有任何内容可打印的原因。

关于java - 编译时的简单 Java 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com