Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 160585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:03:39+00:00 2026-05-11T11:03:39+00:00

please see the following code. String s = Monday; if(s.subString(0,3).equals(Mon){} String s2 = new

  • 0

please see the following code.

String s = 'Monday'; if(s.subString(0,3).equals('Mon'){}  String s2 = new String(s.subString(0,3)); String s3 = s.subString(0,3);   

I know that line 2 will still point to ‘Monday’ and have a new String object with the offset and count set to 0,3.

The line 4 will create a new String ‘Mon’ in string pool and point to it.

But not sure what about line 5 whether it will behave like line 2 or line 4.

If i am wrong for line 2 or 4 also please correct..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:03:40+00:00Added an answer on May 11, 2026 at 11:03 am

    I know that line 2 will still point to ‘Monday’ and have a new String object with the offset and count set to 0,3.

    That is currently true of the Sun JRE implementation. I seem to recall that was not true of the Sun implementation in the past, and is not true of other implementations of the JVM. Do not rely on behaviour which is not specified. GNU classpath might copy the array (I can’t remember off hand what ratio is uses to decide when to copy, but it does copy if the copy is a small enough fraction of the original, which turned one nice O(N) algorithm to O(N^2)).

    The line 4 will create a new String ‘Mon’ in string pool and point to it.

    No, it creates a new string object in the heap, subject to the same garbage collection rules as any other object. Whether or not it shares the same underlying character array is implementation dependant. Do not rely on behaviour which is not specified.

    The String(String) constructor says:

    Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

    The String(char[]) constructor says:

    Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

    Following good OO principles, no method of String actually requires that it is implemented using a character array, so no part of the specification of String requires operations on an character array. Those operations which take an array as input specify that the contents of the array are copied to whatever internal storage is used in the String. A string could use UTF-8 or LZ compression internally and conform to the API.

    However, if your JVM doesn’t make the small-ratio sub-string optimisation, then there’s a chance that it does copy only the relevant portion when you use new String(String), so it’s a case of trying it a seeing if it improves the memory use. Not everything which effects Java runtimes is defined by Java.

    To obtain a string in the string pool which is equal to a string, use the intern() method. This will either retrieve a string from the pool if one with the value already has been interned, or create a new string and put it in the pool. Note that pooled strings have different (again implementation dependent) garbage collection behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is, that every plugin has its own Classloader… May 11, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer Two problems with your code. First, it looks like you're… May 11, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer To understand how the basic injection mechanism works, take a… May 11, 2026 at 11:54 pm

Related Questions

I've been trying to highlight a search result in DataTable. First, I loop through
In order to verify the data coming from the Google Safe Browsing API ,
Going through some of my older Delphi projects and upgrading them to D2009, as
First off, according to http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx , the List.Find method is only listed as throwing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.