«

»

Jul
12

Inside String Working in JVM

Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory. Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it  does not create a new String and it uses reference to the existing String Object. JVM does this type of checking internally for String literal but not for String object which it creates through ‘new’ keyword. You can explicitly force JVM to do this type of checking for String objects which are created through ‘new’ keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present.

So the conclusion is, JVM maintains unique String objects for String literals internally. Programmers need not bother about String literals but they should bother about String objects that are created using ‘new’ keyword and they should use intern() method to avoid duplicate String objects in heap memory which in turn improves java performance. see the following section for more information.

Have a look at the following code snippet.


		String s1 = new String("Java");
		String s2 = new String("Java");

		System.out.println(s1 == s2);//false
		System.out.println(s1.equals(s2));//true

		String t1= "JavaSE";
		String t2 = "JavaSE";

		System.out.println(t1 == t2); //true
		System.out.println(t1.equals(t2));//true

		String p1 = new String("JavaEE");
		p1 = p1.intern();//Forced to use string literal value, from String Pool
		String p2 = "JavaEE";
		p2 = p2.intern();//Forced to use string literal value, from String Pool

		System.out.println(p1 == p2);
		System.out.println(p1.equals(p2));

		String m = new String("JavaFX");
		m = m.intern();//Forced to use string literal value, from String Pool
		String n = new String("JavaFX");
		n = n.intern();//Forced to use string literal value, from String Pool

		System.out.println(m == n);//true
		System.out.println(m.equals(n));//true
VN:F [1.9.13_1145]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.13_1145]
Rating: +1 (from 1 vote)
Inside String Working in JVM, 10.0 out of 10 based on 1 rating

About the author

Faisal Basra

Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006. Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet. I have taken many initiatives while working with teams. Some of includes Automated Build & Release Management system via Hudson, Maven, Archiva & SVN. Blogging is my hobby and I also initiated blog at corporate level from setting up complete blog for company, content generation strategy and visibility over the Internet by Internet Marketing. Framworks & Technologies: JSF, Richfaces, Primefaces, Openfaces, Struts, Hibernate, Spring, ORMLite Tools & Servers: jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogic Mobile Development: Google Android Marketing: Internet Marketing, Mobile App Marketing

Permanent link to this article: http://www.javaplex.com/blog/inside-string-working-in-jvm/

5 comments

  1. Evelynn Corridoni (http://avafx NULL.tk) says:

    hi!, thanks for the information, this post was really useful ! avafx forex broker (http://www NULL.avafx NULL.tk)

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  2. Shanna Thomes (http://www NULL.healthassurancenetwork NULL.com) says:

    Foarte bine lucrurile.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  3. Elisabeth Treatment Employment (http://spotify NULL.tweetmeme NULL.com/search/?q=fixyourcv) says:

    Remember that your Curriculum Vitae is a promotional tool that helps you in marketing yourself in front of your potential employer. A well-created document will persuade your recruiter to invite you for the interview session. So, you need to be extra careful while formulating a Curriculum Vitae.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  4. Yudhi Karunia Surtan (http://brainmasterexperience NULL.com) says:

    Yeah i agree, but how significant will the improvement does?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  5. Kailachetan9 says:

    from many days i have doubts regarding string object, your explanation clear all that.

    Thanks 

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>