String constants

String str = "Hello " + "World";
If I write like the above, how many string objects are created? One or Three? I am asking this because somewhere I found an advice that we should use a StringBuffer and its append() method to concatenate strings in order to reduce the number of string constants generated when we want to create a long string and it does not fit into a single line (hence break it up into multiple lines) or dynamically create a string depending on several conditions. Like the following:
String sql = "INSERT INTO MYTAB "
+ "( x, y, z)"
+ " VALUES(1,2,3)";
The above can be done as below:
StringBuffer sb = new StringBuffer();
sb.append( "INSERT INTO MYTAB " );
sb.append( "( x, y, z)" );
sb.append( " VALUES(1,2,3)" );
It was advised that, in such cases, StringBuffer should be used since it maintains an array internally and does not create a new string every time a new string constant is appended.

maity wrote:
Well, thanks. One more question, is there any way that I can count the number of objects created by my java program when it is running?I would use a profiler if I wanted to know that. But why do you need to know?

Similar Messages

  • What is the difference between String Constant and Empty String Constant

    What is the difference between string constant which does not contain any value and the Empty string constant?
    While testing a VI which contain a normal string constant in VI analyzer, it gives error to change string constant with the empty string constant?
    Please Reply
    prabhakant
    Regards
    Prabhakant Patil

    Readability.
    Functionally, they are the same. From a coding standpoint, the Empty String Constant is unambiguous.
    It is empty and will always be; good for initialization. Also, because you can not type a value into and Empty String Constant, someone would need to conciously replace it to set a 'default' value that is something other than NULL.
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • How do I input multiple string constants into VISA Write

    I am trying to put in multiple string commands into VISA Write to control a motor arm. However, when I use "concatenate strings" to link together my multiple string constants, only the first command is executed, and neglects the other commands below it. Should I be using something other then "concatenate strings" or do I need to use a delay so that the next command executes when the one before it is finished? Help.

    Concatenate strings should work or just use one string constant with multiple lines. The problem you're having could be that the instrument requires some sort of delay but without details on the instrument, only you or the vendor can answer that. Some instruments will have a queue and others won't. The other possiblity is that you need some kind of termination character between commands. This might be a carriage return for a serial instrument or the ; character for a GPIB one. Check the instrument manual or provide the make and model and maybe someone here has some experience with it.

  • When you run Muse 2014 get an error unterminated string constant. Reinstalling does not help. System: Win8.1RU. Help!!!

    When you run Muse 2014 get an error unterminated string constant. Reinstalling does not help. System: Win8.1RU. Help!!!

    Is this occurring on launch? Can you post a screenshot? This isn't an error I recall being reported before.

  • Whats a String constant pool?

    I was reading an article about weak references and came across a peice of code
       Map<String,String> map = new WeakHashMap<String, String>();
          map.put(new String("Scott"), "McNealey");They also said
    if you don't call new String(), the reference for the map key will be to the system's string constant pool. This never goes away, so the weak reference will never be released. To get around this, new String("Scott") creates a reference to the reference in the string constant pool. The string contents are never duplicated. They stay in the constant pool. This simply creates a separate pointer to the string constant in the pool.
    I never heard of String constant pool could any one enlighten me. I didnt get the point of what above paragraph says also.
    Jubs

    No it won't: it will only be removed when the garbage collector rears
    its ugly head. Until then the reference will be present in the map.Okay it was unprecise. The item MAY be removed immediately after it
    has been inserted because the key object is only referenced from the
    Map itself.Yes, that is true, but mind though: any object that still has a strong
    reference to it will not be removed from that map. I use such weak maps
    for localization reasons a lot. When a localization changes (while the
    application is running), a whole lot of visual components must have
    their text part changed. They are all stored in such a weak reference map.
    Of course quite some visual components can be garbage collected
    at some time (think of labels etc. in JDialogs after the dialog has been
    disposed).
    These maps are ideal for those purposes.
    kind regards,
    Jos

  • Question about String constant pool

    Is there is any function available to find how many Sring literal in String Constant pool

    Well, there's stuff like BCEL.
    But it may not do what you want.
    Why do you want to find out what's in the constant pool?

  • Lifetime and behavior of the String Constant Pool

    If you 'inline' a String constant in Java, I'm aware in the VM spec that it says this String is put in the constant pool.
    What is the lifetime of the constant pool and what is it tied to? Is there one constant pool per Class, per Class instance, or per VM?
    If an instance is GC'd, do it's constants previously moved to the constant pool get destroyed, or do they live on until I create another instance of that Class?
    Thanks, Kevin

    Is the constant pool created at compile time and written into the Class file?Yes.
    Each class has it's own constant pool which contains constant values and references to other classes and fields.
    It is created a compile time.
    A class (definition) is loaded by a classloader at runtime.
    In theory you could have the same class loaded twice by different classloaders and each would have a copy of the class definition.
    As far as I know, a class definition can only be removed from memory by nulling/garbage collecting the classloader that loaded it.
    regards,
    Owen

  • What is the little square box in the string constant input?

    Hello LabVIEW World -
      I am attempting to understand a LabVIEW code that I did not write.  At one point, there is a string (Equal?) comparison that is comparing a string from a VISA Read to a string constant input.  The string constant has a little square box inside of it.
      Does anyone know what this little square box is?  (See attached screenshot)
      Thanks.
    DS
    Attachments:
    LittleSquare.JPG ‏69 KB

    When I place string constants like this I will display then either as hex or in the codes mode. Either of these will make the code more readable. Using the default ASCII string display is useless other than letting you know it is an unprintable character.
    Mark Yedinak
    "Does anyone know where the love of God goes when the waves turn the minutes to hours?"
    Wreck of the Edmund Fitzgerald - Gordon Lightfoot

  • Question about string constant in Hex display format

    how to get a plus of 2 string constant which is display in Hex display like attahced, for example I have string "28" and "5D" in Labview which has been in Hex format, the plus of this 2 string should be "85" in Hex. pls help on this, thanks
    Attachments:
    1.jpg ‏21 KB

    This'll work, too.  I'm afraid of casting types...
    Jm
    Jim
    You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
    Attachments:
    hex.png ‏2 KB
    hex.vi ‏7 KB

  • Richt Click Mouse with user32.dll and String Constant for Escape Key

    Hi, could some one provide the input parameters to use with Mouse_Event function in user32.dll to Right Click the Mouse?   Also, what is the string constant to send if one want to type the Escape Key (Esc)?  
    I have searched this forrum but only find the parameters for Left Mouse Click.  Thanks for any help.

    Hi,
    The mouse_event function has been superseded by the sendinput command.  To learn more about the function calls with user32.dll, have a look at the msdn.com website.  The user32.dll website is shown here.  The syntax for the mouse_event function is shown below:
    Syntax
    VOID mouse_event(      
        DWORD dwFlags,
        DWORD dx,
        DWORD dy,
        DWORD dwData,
        ULONG_PTR dwExtraInfo
    );I hope this helps,
    Regards,
    Nadim
    Applications Engineering
    National Instruments

  • 'private static final String' constants vs inline constants

    Other than source code readability and maintainability, is there any runtime benefit from declaring String constants as 'private static final' in your code? Does this effect the way the String is stored in memory in the constant pool?
    Thanks, Kevin

    I guess my question really is, is this String declared inline:
    System.out.println("some constant");treated any different internally in memory at runtime compared with this:
    System.out.println(SOME_CONSTANT);
    private static final String SOME_CONSTANT = "some constant";Since both Strings end up in the Constant Pool, how is declaring it 'static final' of any benefit in terms of performance?

  • Enum or EnumMap to store String constants using enums?

    I have decided to use enums to store some String
    constants. What are the pros and cons of using a
    simple enum versus using EnumMap? What are
    the differences?
    If going with the simple enum option, I would just
    do as in the following example:
    // The typesafe enum pattern
    public class Suit {
        private final String name;
        private Suit(String name) { this.name = name; }
        public String toString()  { return name; }
        public static final Suit CLUBS =
            new Suit("clubs");
        public static final Suit DIAMONDS =
            new Suit("diamonds");
        public static final Suit HEARTS =
            new Suit("hearts");
        public static final Suit SPADES =
            new Suit("spades");
    }A System.out.println(CLUBS); prints out "clubs".
    Or is EnumMap a better choice? If so, why? If not,
    then why?

    jverd wrote:
    sunfun99 wrote:
    +3.1459378192030194732612839423...+ is a constant, but not a named constant. If I give the constant a name, like "PI", then I can use that name to access the constant (instead of having to repeat the long constant literal every time I want to use it). Now, I want to use a number of constants across various classes, so I give each of these constants a name. What I then have is a number of named constants. To name a constant means that I map a name to a value:That's not what "mapping" means in this context. Really? Even the Collections tutorial says it, "Map � an object that maps keys to values". And - not surprising - the classes/interfaces "Map", "Set", etc. are named so because they mimic these mathematical concepts.
    It appears you don't know what java.util.Map is for. java.util.Map is "an object that maps keys to values". It's an interface that also provides various methods that allows for various dealings with the data, such as removing or adding mappings, provide the value given the name, give the total number of mappings, etc.
    Then define an enum.As I said earlier, I believe you, and that's what I will do.
    What makes you think that EnumMap is a "more advanced implementation" of enums? (It's not. It's not an implementation of enums at all.) What does "more advanced" even mean here?Sorry, my mistake. It's not an implementation of enums. However, it takes enums as parameters. In that sense, using EnumMap would indirectly imply a more complicated way of using enums, since not only do I create enums, but then I have to put the into EnumMap.

  • Deprecated conversion from string constant to 'char*'

    Hi all
    I am working with strings and i cant figure out why the following
    warning appears at time of build.
    warning: deprecated conversion from string constant to 'char*'
    It appears for the line
    char *myName = "Apple.txt";
    Is there anyone who can help me?
    Help is welcome.
    Thanks in advance.

    Any reason why you aren't using NSString in place of char?
    char *myName = "Apple.txt";
    NSString *myName = @"Apple.txt";

  • Remove Recessed Box on String Constant

    Hello -
      I have created a subvi that will present a "hint" whenever the "show hint" button is pressed.  I want this hint (a simple string constant) to simply appear when called, and then disappear when the "hide hint" button is pressed.  To do this, I am using the visible property node for a string control.
      Here's the problem - When I show the hint, I can see the Recessed Box behind the words...  Is there a way to remove the Recessed Box from the String Constant?
      Or - Is there a better / more professional-looking way to do this type of thing?
      I have attached my code to this post.  I have also attached a *.jpg that points to the outline/border that I would like to remove.
      Thanks!
    Dan
    Attachments:
    Hint_Pic.JPG ‏31 KB
    HINT.vi ‏13 KB

    Excellent points - Thanks for the corrections.  I have re-posted the code with the changes that you suggested, just in case others wanted to see it.
    Is there an easy way to change the format style of what I am trying to do to look something more like the "Show Details" button that you see in Windows?  (see example attached image).  I suppose that I could use a picture of the little downward arrow (as used in my ShowDetails.JPG attachment), overlay that with a transparent circular boolean button that says "show details", and use a subvi that appears and presents the details when called, but I feel like there should be a better alternative - perhaps this type of thing already exists....
    Thanks in advance for your suggestions! 
    Attachments:
    HINT.vi ‏13 KB
    ShowDetails.JPG ‏14 KB

  • Given a multi-line string I want to append a string constant to the beginning of each line.

    Thankyou.

    You could just use "search and replace" with the search string being "\n" (in \codes display) and the replace string as "constant\n". (See attached image).
    If the string you want at the beginning of each line is variable (e.g. line number), you should use e.g. "pick line" or "scan strings for tokens" in a loop, then rebuild the new string.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    AppendConstant.gif ‏9 KB

Maybe you are looking for