Strings are interned in Java, so this is taken care of automatically. I.e. all "42" strings are the same instance. See, e.g. http://en.wikipedia.org/wiki/String_intern_pool http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#19369
/Fredrik On 22 Mar, 06:41, "Kapil.dalwani" <kapildalw...@gmail.com> wrote: > I would like to give an example where I want space optimization using > Strings in Java. > > I have a function > > String key (int i, int j){ > > return new String.valueOf(i) + "-" + String.valueOf(j) > > } > > Now, the number of possible values of i and j are 1-10, thus possible > values of the String return by the function is > 10 * 10 = 100. > > But, I have to call this function say 1,00,000 times and I don't want > it to return 1,00,000 memory locations of String. > My question is how can I optimize this function to return only > possible 100 memory locations of String > > Some ideas which I have is > a) Use a HashMap<Integer,String> where the key is i * j and value is > my required String "i" + "-" +j > > b) Can keywords like final or Static in return type of key function > help. > > c) Any other great suggestion? > > Thanks > -Kapil --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en -~----------~----~----~----~------~----~------~--~---