{"id":1269,"date":"2016-04-12T04:32:02","date_gmt":"2016-04-12T04:32:02","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1269"},"modified":"2016-04-12T04:32:02","modified_gmt":"2016-04-12T04:32:02","slug":"string","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/string","title":{"rendered":"String In JAVA With Example"},"content":{"rendered":"<p>String is the most commonly used class in Java programming language. In java every string we create is an object of String Class that implements\u00a0<em>Serializable<\/em>,\u00a0<em>Comparable<\/em>\u00a0and\u00a0<em>CharSequence<\/em>\u00a0interfaces. It is nothing but a character array for example \u201cAbhiAndroid\u201d is a string of 11 characters as shown.<\/p>\n<pre>char[] test= { 'A' , 'b' , 'h' , 'i' , 'A' , 'n' , 'd' , 'r' , 'o' , 'i' , 'd' };\r\nString\u00a0 testString= new String(test);<\/pre>\n<p>Strings are Immutable in nature, which means once string is created its value cannot be altered, but we can create a new Instance of it.<\/p>\n<p><strong>Immutable<\/strong>: Any object whose state cannot be altered once created are called Immutable objects. String, Integer, Byte, Short, all other wrapper class objects are immutable in nature.<\/p>\n<hr \/>\n<h4><strong>Creating a String In JAVA:<\/strong><\/h4>\n<p>There are two ways by which String can be created:<\/p>\n<ol>\n<li>Using String literal<\/li>\n<li>Using New keyword<\/li>\n<\/ol>\n<p><strong><u>Using String Literal<\/u><\/strong><\/p>\n<p>Strings can be created very easily, by assigning a string value to the string literal as shown:<\/p>\n<pre>String string1 = \"Welocome to AbhiAndroid\";\r\nString string2 = \"Welocome to AbhiAndroid\";<\/pre>\n<p>As, discussed above String is a class, but we have not created any object above using new keyword, so how new object is created? Don\u2019t worry compiler did your task. But problem here is that, if the object is already present in the memory compiler do not create a new object rather it assigns the same old object to the new instance created, which means even though we have two string instances above string1 and string2, compiler only creates one string object having the value \u201cWelocome to AbhiAndroid\u201d and assigns the same to both the instances String1 and String2.<\/p>\n<p><strong><u>Using New keyword<\/u><\/strong>:<\/p>\n<p>As we saw above, by using only String literal compiler assigned the same string object to two different string literals, To overcome this approach we can create string using new keyword as shown<\/p>\n<pre>String string1 = new String(\" Welocome to AbhiAndroid \");\r\nString string2 = new String(\" Welocome to AbhiAndroid \");<\/pre>\n<p>In this approach compiler will create two different objects in memory having the same value.<\/p>\n<hr \/>\n<h4><strong>String Important Points In JAVA: <\/strong><\/h4>\n<ol>\n<li>Strings in Java unlike C, C++ are not null Terminated as String is a Class which is backed by Character array.<\/li>\n<li>Strings are Immutable and Final in Java, which means once created we cannot modify its content.<\/li>\n<li>Strings are maintained in a String pool, which is a special memory location. Any time we create a new string using String Literal, JVM first checks\u00a0String pool and if any object which is similar to the content is available, then it returns the same and doesn&#8217;t create a new object. However JVM doesn&#8217;t perform String pool check if object is created using new operator.<\/li>\n<li>For comparing two String objects or we can say to check whether they are same or not, equals()method is used instead of equality(==) operator. String class overrides equals() method that provides content equality, which checks characters, case and order.<\/li>\n<li>Avoid storing sensitive data in String like passwords, bank account information etc. As Strings are immutable, there is no way to erase the content from String because data is stored in java heap for longer time and which exposes risk of being seen by anyone who has access to Java memory, like reading from memory dump.<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>String Quick Revision Points:<\/strong><\/h4>\n<ul>\n<li>String is a sequence of characters, or we can say similar to character array<\/li>\n<li>String is a java is not null terminated<\/li>\n<li>Strings are immutable and final<\/li>\n<li>Strings are maintained in a String pool<\/li>\n<li>To compare contents of String equals() method is used instead of == operator<\/li>\n<\/ul>\n<p>Use character array for storing sensitive information likes passwords instead of Strings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String is the most commonly used class in Java programming language. In java every string we create is an object of String Class that implements\u00a0Serializable,\u00a0Comparable\u00a0and\u00a0CharSequence\u00a0interfaces. It is nothing but a character array for example \u201cAbhiAndroid\u201d is a string of 11 characters as shown. char[] test= { &#8216;A&#8217; , &#8216;b&#8217; , &#8216;h&#8217; , &#8216;i&#8217; , &#8216;A&#8217; &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/string\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">String In JAVA With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1269","page","type-page","status-publish","hentry"],"psp_head":"<title>String In JAVA With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"String is the most commonly used class in Java programming language. In java every string we create is an object of String Class that implements Serializable, Comparable and CharSequence interfaces.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/string\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1269"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1269\/revisions"}],"predecessor-version":[{"id":1494,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1269\/revisions\/1494"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}