两种定义出来的结果一样,这中间的具体区别在哪儿?
String hobby = new String("爱慕课");
String url = new String("www.imooc.com");
String hobby ="爱慕课";
String url = "www.imooc.com";
String hobby = new String("爱慕课");
String url = new String("www.imooc.com");
String hobby ="爱慕课";
String url = "www.imooc.com";
2018-08-22
public class HelloWorld { public static void main(String[] args) { //定义字符串 String hobby = "爱慕课"; int hashCode = System.identityHashCode(hobby); System.out.println( hashCode ); System.out.println("hobby:" + hobby ); String hobby1 = "爱慕课"; int hashCode1 = System.identityHashCode(hobby1); System.out.println( hashCode1 ); System.out.println("hobby1:" + hobby1 ); String hobby2 = new String("爱慕课"); int hashCode2 = System.identityHashCode(hobby2); System.out.println( hashCode2 ); System.out.println("hobby2:" + hobby2 ); //String url = "www.imooc.com"; //String url1 = new String("www.imooc.com"); //输出字符串 //System.out.println("url:" + url ); }}
举报