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 6 of 6

Thread: String Vs StringBuffer

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb String Vs StringBuffer

    Hi!

    I am working in web application project,
    I have a doubt,when should use String and String Buffer, I know the concept String in immutable and StringBuffer is muttable,So every time i am assign the value into the String its taking separate memory,....

    My doubt is I have a executeQuerymanager Class,...So when i want to execute the query so i jst put the query in String and call the executeQuerymanager Class,So in my Class if i want to execute two the String So i am Using Same String variable and Calling the Class,.....

    So shall i make it StringBuffer instead of String?.....

    Hope u underStand my doubt,...

    eg:

     
    String query = "";
    ResultSet rs = null;
     
    query = "select * from XXX";
     
    rs = executeQueryManager(query);
     
    query = "select * from YYY";
     
    rs = = executeQueryManager(query);

    Hope U understand my example....

    Thanks,

    kalees


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: String Vs StringBuffer

    Usually you'd have a number of static queries I believe, so if I was you I'd create some static variables holding the constants for your queries.

    private static final String SELECT_ALL_USERS = "select username,password,email from userstable";

    And then you just pass that into your queryManager.

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Re: String Vs StringBuffer

    Thanks,

    But i am having different querys in my action class,each time i calling queryManager,i am jst passing the query,....

    eg :

     
                            query ="select userName,userPass from userTable";
                              rs = queryManager(query);
                            query = "select price from priceTable;
                             rs = queryManager(query);

    Some time May have a 4 more also,So in this kind of situation what to use String or StringBuffer?.....

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: String Vs StringBuffer

    I meant you should have a list of queries.

    private static final String QUERY1 = "select username,password,email from userstable";
    private static final String QUERY2 = "select * from someothertable";
    private static final String QUERY3 = "select * from userstable WHERE username=?";

    And just add all your queries like this. How does your queryManager actually call these?

    // Json

  5. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Re: String Vs StringBuffer

    Yes!

    I am doing web application banking project,in struts frame work,
    in my action class i am getting query result from different tables,so my QueryManager will return
    the query result set,So my doubt is,

                         private static final String QUERY1 = "select username,password,email from userstable";
                         private static final String QUERY2 = "select * from someothertable";
                         private static final String QUERY3 = "select * from userstable WHERE username=?";

    if i create the query with different string,
    will it be create more object then it takes more memory right?
    So if i use StringBuffer then it will use the same memory right?.....

    What is the benefit of creating different string obj for different query?.....

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: String Vs StringBuffer

    When you declare all your queries as statics they will of course take up memory like that but you will have a reference to the same strings so it will be dead quick.

    When you use the StringBuffer you will create a lot of strings anyways and they all get stored into the string pool anyways so in effect they will take up memory.

    Using a stringbuilder or even stringbuffer which is slower will take more time in the long end. In my opinion there's no reason to use a StringBuilder if you know that the strings will be the exactly same every time.

    Otherwise you can create a properties file in your web-inf/classes folder called databaseQueries.properties and load that into memory and just get the query you need from it.

    The amount of memory taken up by your queries will be nothing compared to the objects you will be putting on the session anyways I believe.

    // Json

Similar Threads

  1. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM