Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default String vs new String

    Java Code:
    String myString = “hello world”;
    String myString1 = new String(“Hello World”);
    Whats the difference between the statements above?

  2. #2
    m_srikanth is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:


    String str = "abc";
    is equivalent to:


    char data[] = {'a', 'b', 'c'};
    String str = new String(data);

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks for the explanation. So it means that when we create a String using the following syntax, we create String buffer that can be changed unlike String constants.

    Java Code:
    String str = new String("String Msg");

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Ok. If the buffer has the same size, as the number of characters in the string, how about the followinf example:

    Java Code:
    String str = new String("Austarlia");
    str = "Australia Football Team";
    str was declared with buffer size 9. Then I put a String of size 23 characters. Of course its not a syntax error but does this mean that the buffer is flexible and grow according to the String??

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Try it and see.:p

    Simply print some characters or find the size of the buffer. Then you can see what's going on there.

  8. #8
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    I tried the following:
    Java Code:
    String myString = new String("Hello");
    System.out.println("Buffer size: " + myString.length());
    myString = "Hello-Hello-Hello";
    System.out.println("Buffer size: " + myString.length());
    myString = "hi";
    System.out.println("Buffer size: " + myString.length());
    Output:
    Java Code:
    Buffer size: 5
    Buffer size: 17
    Buffer size: 2
    So buffer grows or shrink according to the String. Makes sense :D

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks all of you. Now I know the difference.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #12
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Well, I discovered something interesting. Review the code below. I think, every time we change the value in String, new object is being created.

    Java Code:
    String myString = new String("Hello");
    System.out.println("Buffer size: " + myString.length() + 
    " Hash code " + myString.hashCode());
    
    myString = "Hello-Hello-Hello";
    System.out.println("Buffer size: " + myString.length() + 
    " Hash code " + myString.hashCode());
    
    myString = "hi";
    System.out.println("Buffer size: " + myString.length() + 
    " Hash code " + myString.hashCode());
    Output:
    Java Code:
    Buffer size: 5 Hash code 69609650
    Buffer size: 17 Hash code -2022942916
    Buffer size: 2 Hash code 3329
    Last edited by javaplus; 11-26-2007 at 11:34 AM.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I don't think so. It use the same object with different hash code, that mean different memory locations. I'm not 100% sure, I want to check it now. May be some explanation can be available in documentation.

  14. #14
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Yes, may be it stores at different memory location as the size changes.
    Please share if you have some interesting information.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sure. As far as I know the effect is that dynamically changing the buffer, first what have happened is destroyed/delete the first buffer, then create a new buffer with new size. So the memory location is differ. Basically memory location is selected randomly.

    If you can just test your code on another machine. I think it give the different hash code for you.

  16. #16
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Makes sense. But if the buffer is deleted and then new is created, isnt it like

    Java Code:
    String a = "a";
    a= "aa";
    ...
    I mean then what is the real difference?

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Real difference comes with real application programming. Working with objects gives the best way to work. That is why OOP is handy.

  18. #18
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Yep - OOP sure is handy.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I have no a best explanation to you on this. Actually you can do above all the things we have tested by using String constant. Depends on the situation of the coding it should be decide, either working with constants or with objects.

  20. #20
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks guys for the fruitful discussion.
    I learned new things.

    Keep educating.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  2. String
    By one198 in forum New To Java
    Replies: 1
    Last Post: 01-07-2008, 09:59 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •