intern « string « Java Data Type Q&A

1. Is there a way to get apache-digester to intern strings for certain attributes?

I am a big fan of using apache-digester to load XML files into my object model. I am dealing with large files that contain many duplicates (event logs), and would therefore like ...

2. Is it good practice to use java.lang.String.intern()?

The Javadoc about String.intern() doesn't give much detail. (In a nutshell: It returns a canonical representation of the string, allowing interned strings to be compared using ==)

  • When would I ...

3. When to use intern()

I see a lot of legacy code like this:

class A {
    public static final String CONSTANT = "value".intern();
I don't see any reason for the ...

4. When should we use intern method of String?

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be ...

5. How is Java's String#intern() method implemented?

I tried to look at Java's String#intern() method, but it's public native String intern(); In general, how is interning implemented? In String's case?

6. Real Life, Practical Example of Using String.intern() in Java?

I've seen many primitive examples describing how String intern()'ing works, but I have yet to see a real-life use-case that would benefit from it. The only situation that I can dream up ...

7. Java string intern and literal

Are the below two pieces of code the same?

String foo = "foo";
String foo = new String("foo").intern();

8. Which debugging tool can list strings internalized?

I am looking to a debugging tool that can list the strings that have been internalized? Ideally, I would like to put a mark and have a list of the ...

9. How can I avoid string.intern() contention and keep the memory footprint low?

I am parsing a rather large (200 MB) XML file that results in a tree of objects each defining a bunch of parameters (key=value). This data structure is running in a ...

10. intern() behaving differently in jdk6 and jdk7

class Test
 public static void main(String... args)
  String s1="Good";

  String s2="Goodmorning";
    System.out.println("both are equal");

11. use of Intern() method of String

This would be important if the String had been created using the new operator and therefore was out on the heap - not a good place to be if you want to optimize by re-using the same object in multiple places. For efficiency you might force that object into the pool using intern(). Now it is available to be referenced for ...

12. String intern method

13. String's intern method

Welcome to JavaRanch! The API Documentation is a good place to look first for information such as what a class or method does. The documentation for String tells us that the intern method Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if ...

14. What is String.intern ?

The idea behind intern() is that there is one and only one interned string with a particular value. So if you intern a bunch of strings, you can later compare them using == rather than equals(). The API documentation explains this more clearly. [ October 13, 2002: Message edited by: Ron Newman ]

15. String intern

Hi, This is a program from wrox jdk1.5 book ..regarding string comparison ********************************************* public class MatchStrings { public static void main(String[] args) { String string1 = "Too many "; String string2 = "cooks"; String string3 = "Too many cooks"; // Make string1 and string3 refer to separate strings that are identical string1 += string2; // Display the contents of the strings ...

16. Confusion over intern of String class

String.intern() returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings. String.intern() returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this ...

17. String intern()?

18. String's intern() - details

Hello, After learning quite a bit about Strings here at Javaranch, I have some questions. When I decompile the String class, I see: public native String intern(); There is no method body, I assume some native code is linked in somehow? Can anyone tell me how this code looks, or where I can find information on it (I know functionally what ...

19. String Intern

20. question regarding String.intern()

From java specification (3.10.5) String literals : package testPackage; class Test { public static void main(String[] args) { String hello = "Hello", lo = "lo"; System.out.print((hello == "Hello") + " "); System.out.print((Other.hello == hello) + " "); System.out.print((other.Other.hello == hello) + " "); System.out.print((hello == ("Hel"+"lo")) + " "); System.out.print((hello == ("Hel"+lo)) + " "); System.out.println(hello == ("Hel"+lo).intern()); } } class ...

21. query with string intern method

22. String Class intern method

class TestStringObjects { public static void main(String[] args) { String literal1 = "alpha"; String literal2 = "alpha"; System.out.println("Literals point to same object: " + (literal1 == literal2)); String newStr1 = new String("alpha"); String newStr2 = new String("alpha"); System.out.println("New strings point to same object: " + (newStr1 == newStr2)); System.out.println("Literal same as uninterned new: " + (literal1 == newStr1)); System.out.println("Literal same as ...

23. String intern

24. A replacement for String.intern() ?

I use intern() in my code because I can a have a large number of duplicate strings, I dont make use of the quick == matching of intern() Im just using intern() to reduce total memory consumption. But the trouble is that it uses memory in Non-Heap memory (PermGen) instead of heap, and getting the right permgen size set on a ...

25. doubt about string.intern()

Why do you need to optimize it? What happens if you don't optimize by "even 1 millisecond"? I would avoid the whole stupid 100 line if-else by doing a Map of type->object and then just clone() the object. But maybe that might not be suitable or "fast enough" for you. Explain your actual problem you're trying to solve, with actual cases. ...

27. what String.intern() actually improve

1. What is a 'message'? Is it one word? If yes and you are assuming that it always part of the set then you might save time. If no or it might not be part of the set, then I would guess that performance would be impacted far more by the parsing algorithm in the first case and by the lookup ...