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

About 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 experience of building mission critical, large scale application in banking, finance & telecom field.

, ,

20 Responses to Inside String Working in JVM

  1. Evelynn Corridoni (http://avafx NULL.tk) May 2, 2010 at 9:30 am #

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

  2. Shanna Thomes (http://www NULL.healthassurancenetwork NULL.com) July 19, 2010 at 9:43 pm #

    Foarte bine lucrurile.

  3. Elisabeth Treatment Employment (http://spotify NULL.tweetmeme NULL.com/search/?q=fixyourcv) September 15, 2010 at 6:30 am #

    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.

  4. Yudhi Karunia Surtan (http://brainmasterexperience NULL.com) April 12, 2011 at 11:19 am #

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

  5. Kailachetan9 August 25, 2011 at 7:08 am #

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

    Thanks 

Trackbacks/Pingbacks

  1. kizi (http://www NULL.kizinew NULL.net) - December 6, 2012

    kizi…

    It�s really a cool and helpful piece of information. I�m glad that you simply shared this helpful info with us. Please stay us informed like this. Thank you for sharing….

  2. jfd98ayhcim (http://jfdg98ayhcdi4gmf NULL.com/) - December 18, 2012

    So cool…

    The information provided in the article are some of the best available…

  3. lida hizli zayiflama (http://www NULL.lidahizlizayiflama NULL.com) - December 18, 2012

    Websites you should visit…

    Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose…

  4. lida zayiflama (http://www NULL.lidailezayiflama NULL.com) - December 18, 2012

    Online Article…

    Every once in a while we choose blogs that we read. Listed underneath are the latest sites that we choose…

  5. lida (http://www NULL.lida-slimming NULL.com) - December 19, 2012

    So cool…

    The information mentioned in the article are some of the best available…

  6. lida (http://www NULL.lidazayiflama NULL.info) - December 19, 2012

    Blogs you should be reading…

    please visit the sites we follow, including this one, as it represents our picks from the web…

  7. lida (http://www NULL.zayiflamailaci NULL.gen NULL.tr) - December 20, 2012

    Sources…

    Here are some of the sites we recommend for our visitor…

  8. jfd98ayhcfim (http://jfdg98ayhcdi4gkgf NULL.com/) - December 20, 2012

    Sources…

    Here are some of the sites we recommend for our visitor…

  9. training management systems (http://www NULL.getadministrate NULL.com) - December 20, 2012

    Great website…

    please visit the sites we follow, including this one, as it represents our picks from the web…

  10. Low Rate Merchant Accounts (http://www NULL.CAKMerchants NULL.com) - December 21, 2012

    Great website…

    please visit the sites we follow, including this one, as it represents our picks from the web…

  11. Cash Flow (http://www NULL.AJMerchants NULL.com) - December 21, 2012

    Websites you should visit…

    Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose…

  12. Processing Equipment (http://www NULL.TMMerchants NULL.com) - December 21, 2012

    Cool sites…

    Here are some of the sites we recommend for our visitor…

  13. Cash Flow (http://www NULL.BeaverCreekMerchants NULL.com) - December 21, 2012

    Great Content…

    we like to honor many other web sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out…

  14. Maximize your Profits (http://www NULL.mygreenmerchantservices NULL.com/214086) - December 21, 2012

    Great Content…

    we like to honor many different web sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out…

  15. Make Money (http://www NULL.LucindaMerchants NULL.com) - December 21, 2012

    Websites you should visit…

    Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose…

Leave a Reply