用new来为字符串开辟存储单元
否则会被当为垃圾清除的

解决方案 »

  1.   

    q1:
    String str1 = "hello"

    String str1 = new String("hello")
    有什么区别吗?
    当然有,第一个是一个字串变量,第二个是一个字串对象!q2:
    String str1 = "hello";
    String str2 = new String("hello);
    str1和str2有什么区别吗?
    搞不懂你和第一个问题的区别,虽然我知道是两个变量……
    我想你应该是想知道str2和str1在存储上的区别吧?应该是没有区别的,但在使用上因为一个是对象一个是变量,当然会有区别了……
      

  2.   

    new的作用就是表明是个对象啊,分配存储空间
      

  3.   

    public class StringExample 
    {
    public static void main(String[] args) 
    {
    String s1 = "hello";
    String s2 = new String("hello");
    if (s1 == s2)
    {
    System.out.println("That's OK!");
    }
    else if (s1.equals(s2))
    {
    System.out.println("Like this!");
    }
    }
    }
    楼主看看就应该明白了吧,本人写的不好,大家不要介意,呵呵!
      

  4.   

    new 表明为新的对象开辟一块内存空间,String str1 = "hello";
    String str2 = new String("hello);
    但这两个应该没什么区别,
    因为String类型非常特殊,它既是一个变量类型,本身又是一个对象类型,
    (这点不同于其他数据类型如int,boolean)
      

  5.   

    q1:
    String str1 = "hello"

    String str1 = new String("hello")
    有什么区别吗?有区别。第一条语句,如果系统中已经有了"hello"这个字符串,则str1直接指向这个字符串,否则新建一个值为"hello"的String对象;第二条语句则不管系统中有没有值为"hello"的String对象,都会给你新建一个值为"hello"的String对象。新的对象和老的对象,值相同,但地址不同q2:
    String str1 = "hello";
    String str2 = new String("hello);
    str1和str2有什么区别吗?
    两个不是一个对象,值相同,但地址不同q3:
    如果有,有哪些
    参见设计模式的immutable模式,java里的String类就是按这个模式来设计的
      

  6.   

    String s = "asdf"  如果已经存在一个这个asdf对象,那么这个语句不会新作成一个对象。如果不存在的话会新做成一个对象String s = new String("asdf")肯定会新生成一个对象就是说String s1 = "asdf";
    String s2 = "asdf";
    String s3 = "asdf";
    String s4 = "asdf";
    两者指向的是同一片内存空间

    String s1 = new String("asdf")
    String s2 = new String("asdf")
    String s3 = new String("asdf")
    String s4 = new String("asdf")
    两者指向的是不同的内存空间
      

  7.   

    upstairs are not clear. For the reason of performance, jvm will create a literal pool store all literal string objects. String s1="dfasdfsa" is a literal string, while String s2=new String("asdfasdf") is not. When String instantiate a new literal string object, it will first search if the literal pool contains an object with same content. If find, the reference will return to the new object. That's why
    String s3 = "asdf";
    String s4 = "asdf";
    两者指向的是同一片内存空间
      

  8.   

    new 是创建一个内存空间给新的对象,String str1 = "hello";
    String str2 = new String("hello);
    这两个没什么太大区别。