In Java, a String is a pre-defined class that is present in java.lang package. This class is final which implies that no class can extend this. The string class is also immutable. This means that once created and initialized, cannot be changed on the same reference.
Now, we will learn how to create the object of the string class in java–


Creating String Class Object
A simple String is created using the string literal enclosed between double quotes. An example of this is shown below :
String s = “My Name is James”;
One of the important points about String is that, if more than one same set of characters with the same sequence has been assigned to a different string, then they share the same reference in the memory.
Example –
String s1 = “My Name is James”; String s2 = “My Name is James”; String s3 = “My Name” + “is James”;
In this, all the string references s1, s2 and s3 denote the same String object.
One more way of creating the string is with the help of a new keyword. But, in this case, even if the same set of characters with the same sequence is passed, a new reference is created.
Syntax –
String s5 = new String(“My Name is James”);
Concatenation Operator
In Java, String objects can be used with the += and + operators for concatenation. In the concatenation operator, to concatenate between two objects, one of them must be a string and the other can be any.
Given below, is a simple program based on the string operators –
Class Example{ Public static void main(String[] args){ String s1 = “computer”; String s2 = “computer”; String s3 = new String(“computer”); System.out.println(“Result 1”+(s1 == s2)); System.out.println(“Result 2”+(s1.equals(s2)); System.out.println(“Result 3”+(s1 == s3)); System.out.println(“Result 4”+s1.equals(s3)); } }
Output –
True True False True
String class methods in Java
Case Conversion
toUpperCase()
toLowerCase()
We will understand these with the example given below –
public class Example{ public static void main(String args[]){ String s1 = new String(“hello world”); System.out.println(s1); S1.toUpperCase(); System.out.println(s1); S1.toLowerCase(); System.out.println(s1); } }
Output –
hello world HELLO WORLD Hello world
Indexof() method –
public class Example{ public static void main(String[] args){ String s1 = new String(“Computer”); int i = s1.indexOf(‘m’); System.out.println(“Index is “+ i); } }
Output –
3
Some other versions of IndexOf() functions are –
indexOf(int ch) – To search the index of a character in a String
indexOf(int ch, int fromIndex) – To search the index of a character from a specific position
indexOf(String str) – To search the position of a substring in a string
indexOf(String str, int fromIndex) – To search the position of a substring from a specific position
lastIndexOf(int ch) – To search the index of a character from last of the string
lastIndexOf(int ch, int fromIndex) – To search the index of a character from the last and a specific position
lastIndexOf(String str) -To search the index of a substring from last
lastIndexOf(String str, int fromIndex) – To search the index of a substring from the last and from a specific position
Comparing Strings
Two String can be compared using the equals function. The functions used to compare strings are given below –
equals() public class Example{ public static void main(String[] args){ String s1 = new String(“Process”); String s2 = new String(“process”); if(s1.equals(s2)) System.out.println(“They are equal”); else System.out.println(“They are not equal”);}}
Output –
They are not equal
Explanation –
The strings will not be considered equal because of the difference in the first character.
But, if we use the equalsIgnoreCase() method in java, the comparison is case insensitive. Thus, this will return true.
Example –
public class Example{ public static void main(String[] args){ String s1 = new String(“Process”); String s2 = new String(“process”); if(s1.equalsIgnoreCase(s2)) System.out.println(“They are equal”); else System.out.println(“They are not equal”);}}
Similarly, there is one more version of the compare method, which is Compareto() method. We will understand this with the help of the example given below –
public class Example{ public static void main(String[] args){ String s1 = new String(“Process”); String s2 = new String(“process”); int i = s1.compareTo(s2)); if(i == 0) System.out.println(“Strings are equal”); else if(i>0) System.out.println(“They are opposite to dictionary order”);}} else System.out.prinln(“Dictionary Order”);
Split Method()
This method divides the string and returns an array of substrings.
Example –
class Main { public static void main(String[] args) { String text = "We are learning to program in Java"; String[] result = text.split(" "); System.out.print("result = "); for (String str : result) { System.out.print(str + ", "); } }}
Output –
Result = We, are, learning, to, program, in, Java
length() Method
This method returns the length of the string.
Example –
Class Main { public static void main(String[] args) { String str1 = “Java is fun”; System.out.println(str1.length()); }}
Output –
11
Replace() Method
This method replaces each matching occurrence of the old character with the new one.
Example –
class Main { public static void main(String[] args) { String str1 = "bat ball"; System.out.println(str1.replace('b', 'h')); } }
Output –
Hat hall
Substring() Method
This method extracts a substring from a string and returns it.
Example –
class Main { public static void main(String[] args) { String str1 = “java is fun”; System.out.println(str1.substring(0, 4)); } }
Output –
Java
isEmpty() Method
This method checks whether the string is empty or not.
Example –
class Main { public static void main(String[] args) { String str1 = "Java Programming"; String str2 = ""; System.out.println(str1.isEmpty()); System.out.println(str2.isEmpty()); } }
Output –
False True
String toCharArray() method
This method converts the string to array of char, and returns it.
Example –
class Main { public static void main(String[] args) { String str = "Java Programming"; char[] result; result = str.toCharArray(); System.out.println(result); } }
Output –
Java Programming
So, in this article, we learned about the String and String methods used in them in detail.