Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: String objects

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String objects

    Respected PROGRAMMERS,
    Kindly tell me
    String s= "God";
    String k= "God";
    then will both s and k be considered to be referring to the same String object?
    Is there a single instance of String object?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String objects

    Is there a single instance of String object?
    Sometimes the compiler will optimize things and use the same object.
    You can easily test if s==k and see if they are equal today.
    Not something that you should assume will always be true.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: String objects

    You can guarantee them to be the same object in memory by internalizing the strings. Other than that, there's no guarantee that they are (could be true, could not be true).

    See: String (Java Platform SE 6)

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String objects

    Thank You SIR for the reply.
    In my programs they turned out to be same,
    that's why I asked this question.
    Have a very lovely day
    Last edited by god1gracious; September 25th, 2011 at 02:28 AM.

Similar Threads

  1. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM

Tags for this Thread