CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2000
    Location
    phoenix, az
    Posts
    14

    Basic String question

    Are there any advantages/disadvantages between declaring a String without creating an object:
    String str = "Hello World"

    and creating an object:

    String str = new String("Hello World")
    ?


  2. #2
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: Basic String question

    Hi
    In a Java program if you do the following :


    String str = "One";
    String str1 = "One";



    There is only ONE actual String Object,
    but there are two references pointing to this Object, where as if you do this :

    String str = "One";
    String str1 = new String("One");



    There are two String Objects, because you
    have created a "new String".
    You can test this with the "==" operator,
    if you test the first example, it will result
    in true, whereas the second example will
    result in false.
    Whether this is an advantage or not depends
    on how you use it.
    Phill.


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured