Page 1 :
In Java, string is basically an object that represents sequence of char values., An array of characters works same as Java string. For example:, String s=new String(ch);, String s="javatpoint";, , Java String class provides a lot of methods to perform operations on strings such as, compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring(), etc., (The java.lang.* Package), , What is String in Java?, Generally, String is a sequence of characters. But in Java, string is an object that, represents a sequence of characters. The java.lang.String class is used to create a, string object., , How to create a string object?, There are two ways to create String object:, 1. By string literal, 2. By new keyword, , 1) String Literal, Java String literal is created by using double quotes. For Example:, 1. String s="welcome";, Each time you create a string literal, the JVM checks the "string constant pool" first. If, the string already exists in the pool, a reference to the pooled instance is returned. If, the string doesn't exist in the pool, a new string instance is created and placed in the, pool. For example:, , 1. String s1="Welcome";
Page 2 :
2. String s2="Welcome";//It doesn't create a new instance, , 2) By new keyword, 1. String s=new String("Welcome");//creates two objects and one reference varia, ble, , In such case, JVM, will create a new string object in normal (non-pool) heap memory, and the literal "Welcome", will be placed in the string constant pool. The variable s will refer to the object in a heap, (non-pool)., , Java String Example, StringExample.java, public class StringExample{, , public static void main(String args[]){, String s1="java";, , String s2=new String(“strings”);, , String s3=new String("example");//creating Java string by new keyword, System.out.println(s1);, System.out.println(s2);, System.out.println(s3);, }}, , Output:, java, strings, example, , Immutable String in Java, A String is an unavoidable type of variable while writing any application program., String references are used to store various attributes like username, password, etc. In, Java, String objects are immutable. Immutable simply means unmodifiable or, unchangeable.
Page 3 :
class Testimmutablestring{, , public static void main(String args[]){, String s="Sachin";, , s.concat(" Tendulkar");//concat() method appends the string at the end, }, , System.out.println(s);//will print Sachin because strings are immutable objects, , }, , Test it Now, , Output:, Sachin, , Now it can be understood by the diagram given below. Here Sachin is not changed, but a new object is created with Sachin Tendulkar. That is why String is known as, immutable., As you can see in the above figure that two objects are created but s reference, variable still refers to "Sachin" not to "Sachin Tendulkar"., But if we explicitly assign it to the reference variable, it will refer to "Sachin, Tendulkar" object., For example:, Testimmutablestring1.java, class Testimmutablestring1{, , public static void main(String args[]){, String s="Sachin";, , s=s.concat(" Tendulkar");, }, , System.out.println(s);, , Output:, Sachin Tendulkar, , Java String compare
Page 4 :
We can compare String in Java on the basis of content and reference., It is used in authentication (by equals() method), sorting (by compareTo(), method), reference matching (by == operator) etc., There are three ways to compare String in Java:, 1. By Using equals() Method, 2. By Using == Operator, 3. By compareTo() Method, , 1) By Using equals() Method, The String class equals() method compares the original content of the string. It, compares values of string for equality. String class provides the following two, methods:, o, , public boolean equals(Object another) compares this string to the specified object., , o, , public boolean equalsIgnoreCase(String another) compares this string to another, string, ignoring case., , Teststringcomparison1.java, , class Teststringcomparison1{, 1., , 2., , public static void main(String args[]){, , String s1="Sachin";, , 3., , String s2="Sachin";, , 5., , String s4="Saurav";, , 7., , System.out.println(s1.equals(s3));//true, , 4., , String s3=new String("Sachin");, , 6., 8., 9., , System.out.println(s1.equals(s2));//true, , }, , 10. }, , System.out.println(s1.equals(s4));//false, , Test it Now, , Output:
Page 5 :
true, true, false, , In the above code, two strings are compared using equals() method of String class., And the result is printed as boolean values, true or false., Teststringcomparison2.java, 1. class Teststringcomparison2{, 2., , 3., , public static void main(String args[]){, , String s1="Sachin";, , 4., , String s2="SACHIN";, , 6., , System.out.println(s1.equals(s2));//false, , 5., 7., 8., , }, , System.out.println(s1.equalsIgnoreCase(s2));//true, , 9. }, , Test it Now, , Output:, false, true, , In the above program, the methods of String class are used. The equals() method, returns true if String objects are matching and both strings are of same, case. equalsIgnoreCase() returns true regardless of cases of strings., Click here for more about equals() method, , 2) By Using == operator, The == operator compares references not values., Teststringcomparison3.java, 1. class Teststringcomparison3{, 2., , 3., , public static void main(String args[]){, , String s1="Sachin";