ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
java中==比较的是两个字符串的是否同一个对象 ***** ``` // "static void main" must be defined in a public class. public class Main { public static void main(String[] args) { // initialize String s1 = "Hello World"; System.out.println("s1 is \"" + s1 + "\""); String s2 = s1; System.out.println("s2 is another reference to s1."); String s3 = new String(s1); System.out.println("s3 is a copy of s1."); // compare using '==' System.out.println("Compared by '==':"); // true since string is immutable and s1 is binded to "Hello World" System.out.println("s1 and \"Hello World\": " + (s1 == "Hello World")); // true since s1 and s2 is the reference of the same object System.out.println("s1 and s2: " + (s1 == s2)); // false since s3 is refered to another new object System.out.println("s1 and s3: " + (s1 == s3)); // compare using 'equals' System.out.println("Compared by 'equals':"); System.out.println("s1 and \"Hello World\": " + s1.equals("Hello World")); System.out.println("s1 and s2: " + s1.equals(s2)); System.out.println("s1 and s3: " + s1.equals(s3)); // compare using 'compareTo' System.out.println("Compared by 'compareTo':"); System.out.println("s1 and \"Hello World\": " + (s1.compareTo("Hello World") == 0)); System.out.println("s1 and s2: " + (s1.compareTo(s2) == 0)); System.out.println("s1 and s3: " + (s1.compareTo(s3) == 0)); } } ``` ***** ``` // "static void main" must be defined in a public class. public class Main { public static void main(String[] args) { String s1 = "Hello World"; // 1. concatenate s1 += "!"; System.out.println(s1); // 2. find System.out.println("The position of first 'o' is: " + s1.indexOf('o')); System.out.println("The position of last 'o' is: " + s1.lastIndexOf('o')); // 3. get substring System.out.println(s1.substring(6, 11)); } } ```