In Sachin because strings are immutable objects}}Output: helloThough

In general thinking, Strings are sequences of character. But in Java it is something more. We no Java is totally object oriented and here String is an object that represents a sequence of characters. The java.lang.String is used to create string object.Creating String:In java there are two ways to create a string object:By String iteral: String s=”hello”;By new keyword:String s= new String(“hello”); Creating string using new keyword creates two objets and one reference variable. The objects are in string pool and in heap. The variable will refer to the heap.Handling java Strings: In java, strings objects are immutable. Immutable means unmodifiable or unchangeable. Once string object is created its data or state can’t be changed but a new string object is created. For example let’s see the code bellow:class Testimmutablestring{public static void main(String args){String s=”hello”;s.concat (”  world”); //concat() method appends the string at the endSystem.out.println(s); //will print Sachin because strings are immutable objects}}Output: helloThough the concat method was to add world  after the hello but the original string remain unchanged. To understand lets check the code bellow: Public class Testimmutablestring{public static void main(String args){String s=”hello”;s=s.concat (”  world”);System.out.println(s); }}Output: hello worldThis is because when we create the string s=hello, it is immutable. When we cancat anything with it the concatenated string is created in the string heap but the variable points to the initial string. So to get the modified string it is to be assigned to the variable manually. This can be easily understood from the following figure:String s heaphellohello worldMethods of Java String String.length():The length method is used to find the length of the string. It returns the total number of the characters in the string. For example:public class TestLength{public static void main(String args{ String s1=”hello”; System.out.println(“string length is: “+s1.length());  }}Output: 5as the number of character in the strings is 5.String.compareTo(): compareTo() function compare the given string with the current string and returns a positive number or a negative number or 0.public class TestCompareTo{ public static void main(String args){ String s1=”hello”;String s2=”hello”; String s3=”hemlo”; String s4=”flag”;System.out.println(s1.compareTo(s2)); // 0 because both are equalSystem.out.println(s1.compareTo(s3)); //-1 because “l” is only one time lower than “m” System.out.println(s1.compareTo(s4)); // 2 because “h” is 2 times greater than “f”}}If s1 > s2, it returns a positive numberIf s1 < s2, it returns a negative numberIf s1==s2, It returns 0String.concate():This method combines a given string to the end of the string and outputs the combined string. This is like appending: public class TestConcate{public static void main(String args){String s="hello";S=s.concat ("  world");System.out.println(s); }}Output: hello worldStirng.IsEmpty(): this method checks whether the string is empty or not. If the string is empty it returns true otherwise false. For example:public class Testempty{public static void main(String args){String s1="hello";String s2="";System.out.println(s1.IsEmpty()); //trueSystem.out.println(s2.IsEmpty()); //false}}String.trim():trim() checks and removes the leading and trailing spaces from a string. It checks for Unicode value for the space character. If it exists it removes the space character. Example:  public class Testrim{public static void main(String args){String s1="  hello  ";System.out.println(s1+ "this is Alise"); System.out.println(s1.trim()+ "this is Alise"); }}Output:  "hello this is alise"    "hellothis is alise"String.toLowerCase():   This method converts all the characters of the strings to lowercase letters.String.ToUpper(): This method converts all the characters of the string to UPPERCASE character. String.replace():replace() method replaces all the occurrences of a character in the string to the given character. For example:    public class TesReplace{public static void main(String args){String s1="hello world ";S1.replace('o'.'t');System.out.println(s1); }}Output:  "hellt wtrld"The replace method will replace all the occurance of 'o' with 't';String.contains(): This method searches the sequence of characters in a string. If found returns true else false. public class TesContains{public static void main(String args){String s1="hello world ";System.out.println(s1.contains("hello")); //returns trueSystem.out.println(s1.contains("there"));  //returns false}}String.equals(): this method is used to compare two strings. If the two strings are equal returns true. Else returns false.public class TesEquals{public static void main(String args){String s1="hello world ";System.out.println(s1.equals("hello world")); //returns trueSystem.out.println(s1.equals("hello"));  //returns false}}String.equalsIgnorecase(): This method does the same function as string.equals(). But here it checks the string ignoring the case of the charecters.. public class TesEqualsignorecase{public static void main(String args){String s1="hello world ";String s2="hEllo World ";System.out.println(s1.equalsIgnoreCase(s2)); //returns trueSystem.out.println(s1.equals(s2));  //returns false}}String.toCharArray(): This method converts the string into a character array i.e first it will calculate the length of the given Java String including spaces and then create an array of char type with the same content. public class TesToCharArray{public static void main(String args){String s1="hello world ";char ch = s1.toCharArray();for( int i=0; i


I'm Mary!

Would you like to get a custom essay? How about receiving a customized one?

Check it out