早教吧 育儿知识 作业答案 考试题库 百科 知识分享

java程序,假设s1,s2,s3和s4是四个字符串引用,分别对它们进行如下赋值:Strings1="Welcometojava";Strings2=s1;Strings3=newString("Welcometojava");Strings4=s3.intern();分析并写出下列表达式的结果,然后编程验

题目详情
java程序,假设s1,s2,s3和s4是四个字符串引用,分别对它们进行如下赋值:
String s1="Welcome to java";
String s2=s1;
String s3=new String("Welcome to java");
String s4=s3.intern();
分析并写出下列表达式的结果,然后编程验证你的分析.
(1)s1=s2 (13)s1.length()
(2)s2=s3 (14)s1.subString(5)
(3)s1.equals(s2) (15)s1.SubString(5,11)
(4)s2.equals(s3) (16)s1.startsWith("wel")
(5)s1.compareTo(s2) (17)s1.endsWith("java")
(6)s2.compareTo(s3) (18)s1.toLowerCase()
(7)s1==s4 (19)s1.toUpperCase()
(8)s1.charAt(0) (20)" Welcome ".trim()
(9)s1.indexOf('j') (21)s1.replace('o','T')
(10)s1.indexOf('to') (22)s1.replaceAll("o","T")
(11)s1.lastindexOf('j') (23)s1.replaceFirst("o","T")
(12)s1.lastindexOf("o",15) (24)s1.toCharArray()
▼优质解答
答案和解析
public class Test {
    public static void main(String[] args) {
        String s1="Welcome to java";
        String s2=s1;
        String s3=new String("Welcome to java");
        String s4=s3.intern();
        
        System.out.println("s1=s2:" + (s1==s2));
        System.out.println("s2=s3:" + (s2==s3));
        System.out.println("s1.equals(s2):" + s1.equals(s2));
        System.out.println("s2.equals(s3):" + s2.equals(s3));
        System.out.println("s1.compareTo(s2):" + s1.compareTo(s2));
        System.out.println("s2.compareTo(s3) :" + s2.compareTo(s3));
        System.out.println("s1==s4:" + (s1==s4));
        System.out.println("s1.charAt(0) :" + s1.charAt(0) );
        System.out.println("s1.indexOf('j')  :" + s1.indexOf('j')  );
        System.out.println("s1.indexOf('to'):" + s1.indexOf("to"));
        System.out.println("s1.lastindexOf('j') :" + s1.lastIndexOf("j"));
        System.out.println("s1.lastindexOf(\"o\",15):" + s1.lastIndexOf("o",15));
        System.out.println("s1.length():" + s1.length());
        System.out.println("s1.subString(5):" + s1.substring(5));
        System.out.println("s1.SubString(5,11):" + s1.substring(5,11));
        System.out.println("s1.startsWith(\"wel\"):" + s1.startsWith("wel"));
        System.out.println("s1.endsWith(\"java\"):" + s1.endsWith("java"));
        System.out.println("s1.toLowerCase():" + s1.toLowerCase());
        System.out.println("s1.toUpperCase():" + s1.toUpperCase());
        System.out.println("\" Welcome \".trim():" + " Welcome ".trim());
        System.out.println("s1.replace('o','T'):" + s1.replace('o','T'));
        System.out.println("s1.replaceAll(\"o\",\"T\"):" + s1.replaceAll("o","T"));
        System.out.println("s1.replaceFirst(\"o\",\"T\"):" + s1.replaceFirst("o","T"));
        System.out.println("s1.toCharArray():" + s1.toCharArray());
    }
}