niusouti.com
参考答案和解析
正确答案:

 

没有。因为String 被设计成不可变(immutable)类,所以它的所有对象都是不可变对象。

在这段代码中,s 原先指向一个String 对象,内容是 "Hello",然后我们对s 进行了+

操作,那么s 所指向的那个对象是否发生了改变呢?答案是没有。这时,s 不指向原来那个对象了,而指向了另一个 String 对象,内容为"Hello world!",原来那个对象还

存在于内存之中,只是s 这个引用变量不再指向它了。

通过上面的说明,我们很容易导出另一个结论,如果经常对字符串进行各种各样的修

改,或者说,不可预见的修改,那么使用String 来代表字符串的话会引起很大的内存

开销。因为 String 对象建立之后不能再改变,所以对于每一个不同的字符串,都需要

一个String 对象来表示。这时,应该考虑使用StringBuffer 类,它允许修改,而不是每

个不同的字符串都要生成一个新的对象。并且,这两种类的对象转换十分容易。

同时,我们还可以知道,如果要使用内容相同的字符串,不必每次都new 一个String。

例如我们要在构造器中对一个名叫s 的String 引用变量进行初始化,把它设置为初始

值,应当这样做:

public class Demo {

private String s;

...

public Demo {

s = "Initial Value";

}

...

}

而非

s = new String("Initial Value");

后者每次都会调用构造器,生成新对象,性能低下且内存开销大,并且没有意义,因

为String 对象不可改变,所以对于内容相同的字符串,只要一个String 对象来表示就

可以了。也就说,多次调用上面的构造器创建多个对象,他们的String 类型属性s 都

指向同一个对象。

上面的结论还基于这样一个事实:对于字符串常量,如果内容相同,Java 认为它们代

表同一个String 对象。而用关键字new 调用构造器,总是会创建一个新的对象,无论

内容是否相同。

至于为什么要把String 类设计成不可变类,是它的用途决定的。其实不只String,很

多Java 标准类库中的类都是不可变的。在开发一个系统的时候,我们有时候也需要设

计不可变类,来传递一组相关的值,这也是面向对象思想的体现。不可变类有一些优

点,比如因为它的对象是只读的,所以多线程并发访问也不会有任何问题。当然也有

一些缺点,比如每个不同的状态都要一个对象来代表,可能会造成性能上的问题。所

以Java 标准类库还提供了一个可变版本,即 StringBuffer。

更多“String s = "Hello";s = s + " world!";这两行代码执行后,原始的String 对象中的内容到底变了没有?”相关问题
  • 第1题:

    publicclassX{publicstaticvoidmain(String[]args){strings=newstring(Hello”);modify(s);System.out.printIn(s);}publicstaticvoidmodify(Strings){s+=world!”;}}Whatistheresult?()

    A.Theprogramrunsandprints“Hello”

    B.Anerrorcausescompilationtofail.

    C.Theprogramrunsandprints“Helloworld!”

    D.Theprogramrunsbutabortswithanexception.


    参考答案:A

  • 第2题:

    下列哪个程序段可能导致错误?

    A.String s="hello"; String t= "good"; String k=s+ t;

    B.String s="hello"; String t; t=s[3]+"one";

    C.String s="hello"; String standard=s. toUpperCase

    D.String s="hello"; String t =s+ "good"


    正确答案:B
    解析:选项A)String类型可以直接使用“+”运算符进行连接运算。选项B)String是一种Object,而不是简单的字符数组,不能使用下标运算符取其值的某个元素,错误。选项C)toUpperCase()方法是String对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String类型)。选项D)同选项A)。

  • 第3题:

    下列代码的执行结果是( )。 public class Test{ public static void main String args[]){ String s1=new String("welcome"); String s2=new String("welcome"); System.out.println(s1==s2); System.out.println(s1.equals(s2)); } }

    A.false,false

    B.false,true

    C.true,true

    D.true,false


    正确答案:B

  • 第4题:

    下列代码的执行结果是______。 public class ex55 { public static void main(String args[] ) { String s1=new String("hello"); String s2=new String("hello"); System.out.print (s1==s2); System.out.print (","); System.out.println (s1.equals (s2)); } }

    A.true, false

    B.true, true

    C.false, true

    D.false, false


    正确答案:C

  • 第5题:

    下列的( )程序段可能导致错误。

    A.String s="hello": Sting t="good"; String k=s+t;

    B.Sting s="hello"; String t; t=s [3] + "one";

    C.Sting s="hello"; String standard=s.toUpperCase( );

    D.String s="hello": Stringt s +"good";


    正确答案:B

  • 第6题:

    下列选项中,()是正确的表达式。 

    • A、<% String s = “hello world ” ;%>  
    • B、<% = “hello world ” ;%> 
    • C、<% = “hello world ” %>  
    • D、<% ! “hello world ” %>

    正确答案:C

  • 第7题:

    设String对象s="H",运行语句System.out.println(s.concat("ello!"));后String对象s的内容为"Hello!",所以语句输出为"Hello!"。


    正确答案:错误

  • 第8题:

    有语句String s=”hello world”; ,以下操作哪个是不合法的()

    • A、int i=s.length();
    • B、s>>>=3;
    • C、String ts=s.trim();
    • D、String t=s+”!”

    正确答案:B

  • 第9题:

     public class X {   public static void main (String[]args)   {   string s = new string (“Hello”);   modify(s);   System.out.printIn(s);   }   public static void modify (String s)  {   s += “world!”;      }   }      What is the result?()    

    • A、 The program runs and prints “Hello”
    • B、 An error causes compilation to fail.
    • C、 The program runs and prints “Hello world!”
    • D、 The program runs but aborts with an exception.

    正确答案:A

  • 第10题:

    单选题
    有语句String s=”hello world”; ,以下操作哪个是不合法的()
    A

    int i=s.length();

    B

    s>>>=3;

    C

    String ts=s.trim();

    D

    String t=s+”!”


    正确答案: B
    解析: 暂无解析

  • 第11题:

    判断题
    设String对象s="H",运行语句System.out.println(s.concat("ello!"));后String对象s的内容为"Hello!",所以语句输出为"Hello!"。
    A

    B


    正确答案:
    解析: 暂无解析

  • 第12题:

    单选题
    下列选项中,()是正确的表达式。
    A

    <% String s = “hello world ” ;%>  

    B

    <% = “hello world ” ;%> 

    C

    <% = “hello world ” %>  

    D

    <% ! “hello world ” %>


    正确答案: D
    解析: 暂无解析

  • 第13题:

    下列的哪个程序段可能导致错误? ( )

    A.String s="hello"; String t="good"; String k=s+t;

    B.String s="hello"; String t; t=s[3]+"one";

    C.String s="hello"; String standard=s.toUpperCase();

    D.String s="hello"; String t=s+"good";


    正确答案:B

  • 第14题:

    给出下列的代码,则以下哪个选项返回true? String s="hello"; String t="hello"; char c []= {'h','e','1','1','o'};A)s.equals(t);

    A.t. equals(

    B.;

    C.s==t;

    D.t==c;


    正确答案:A
    解析:==操作符比较的是操作符两端的操作数是否是同一个对象,而String的equals()方法比较的是两个 String对象的内容是否一样。s. equals(t)方法比较字符串s与字符串t中的内容是否一致,两个字符串中的内容一致,所以返回true。

  • 第15题:

    下列代码段的执行结果是( )。 public class Test { public static void main(String args[ ]) { String s1= new String("hello"); String s2= new String("hello"); System.out.println(s1==s2); System.out.println(s1.equal(s2)); } }

    A.true false

    B.true true

    C.false true

    D.false false


    正确答案:C
    解析:本题考查比较运算符(==)的使用。比较运算符不仅可以用于基本数据类型的数据之间的比较,还可以用于复合数据类型的数据之间的比较。题中s1和s2的值虽然都是hello,但是由于它们是不同的对象,因此运算后的结果为false。如果需要比较两个对象的值是否相同,则可以调用equals()方法。所以程序最后输出false和true。

  • 第16题:

    给出下列的代码则以下哪个选项返回true? String s = "hello" ; String s = "hello" ; char c[] = { 'h' ,'e','l','o'};

    A.s.equals(t);

    B.t.equals(c);

    C.s = =t

    D.t = = c;


    正确答案:A
    解析:==操作符比较的是操作符两端的操作数是否是同一个对象,String的equals()方法比较的是两个String对象的内内容是否一样。s.equals(1)方法比较字符串s与字符串t中的内容是否一致,所以返回true。

  • 第17题:

    下列的哪个程序段可能导致错误?

    A.String s = "hello"; String t = "good"; String k = s + t;

    B.String s = "hello"; String t; t = s[3] + "one";

    C.String s = " hello "; String standard = s.toUpperCase( );

    D.String s = "hello"; String t = s + "good";


    正确答案:B
    解析:选项A)String类型可以直接使用“+”运算符进行连接运算。选项B)String是一种Object,而不是简单的字符数组,不能使用下标运算符取其值的某个元素,错误。选项C)toUpperCase()方法是String对象的一个方法,作用是将字符串的内容全部转换为大写并返回转换后的结果(String类型)。选项D)同选项A)。

  • 第18题:

    下面哪段语法执行正确()

    • A、 String s = "Gone with the wind";String t = " good ";String k = s + t;
    • B、 String s = "Gone with the wind";String t; t = s[3] + "one";
    • C、 String s = "Gone with the wind";String standard = s.toUpperCase();
    • D、 String s = "home directory";String t = s - "directory";

    正确答案:A,C

  • 第19题:

    request对象可以使用()方法获取表单中某输入框提交的信息。

    • A、getParameter(String s)
    • B、getValue(String s)
    • C、getParameterNames(String s)
    • D、getParameterValue(String s)

    正确答案:A

  • 第20题:

    String s= "hello";     String t = "hello";  char c[] = {’h’,’e’,’l’,’l’,’o’} ;     Which return true?()   

    • A、 s.equals(t);
    • B、 t.equals(c);
    • C、 s==t;
    • D、 t.equals(new String("hello"));
    • E、 t==c;

    正确答案:A,C,D

  • 第21题:

    public class X {   public static void main (Stringargs) {   string s = new string (“Hello”);   modify(s);   System.out.printIn(s);   }   public static void modify (String s) {   s += “world!”;   }   }   What is the result?() 

    • A、The program runs and prints “Hello”
    • B、An error causes compilation to fail.
    • C、The program runs and prints “Hello world!”
    • D、The program runs but aborts with an exception.

    正确答案:A

  • 第22题:

    单选题
    public class X {   public static void main (Stringargs) {   string s = new string (“Hello”);   modify(s);   System.out.printIn(s);   }   public static void modify (String s) {   s += “world!”;   }   }   What is the result?()
    A

    The program runs and prints “Hello”

    B

    An error causes compilation to fail.

    C

    The program runs and prints “Hello world!”

    D

    The program runs but aborts with an exception.


    正确答案: D
    解析: 暂无解析

  • 第23题:

    单选题
    public class X {   public static void main (String[]args)   {   string s = new string (“Hello”);   modify(s);   System.out.printIn(s);   }   public static void modify (String s)  {   s += “world!”;      }   }      What is the result?()
    A

     The program runs and prints “Hello”

    B

     An error causes compilation to fail.

    C

     The program runs and prints “Hello world!”

    D

     The program runs but aborts with an exception.


    正确答案: B
    解析: 暂无解析