String In Java

String in Java, It is the most usable object in Java. It is an object that represents the collections of character. String is used for storing the text and if you want to store a text into a string variable then put the text inside the double-quotes.

String in Java is immutable which means it can not be changed when once it has been created. It is a non-primitive data type because it’s size is not fixed.

String is a class that contains lot of methods to performs operation on string such as toUpperCase(), toLowerCase(), isEmpty(), concat(), replace(), length() etc.

String class is final and it extends the Object and implements CharSequence, Serializable, and Comparable interface.

public final class String extends Object implements CharSequence, Serializable,Comparable{
 // data
}

To create string, there are three classes:-

  1. String
  2. StringBuffer
  3. StringBuilder

We can create string by using above classes.

Way of Creating a String

There are two way to create a string.

  1. String literal
  2. Using new keyword

String literal Example

String str= "Study4Geeks";

Using new keyword

String str= new String("Study4Geeks");

Java String Example

public class StringExample{  
public static void main(String args[]){  
String str="Study4Geeks";  
String str1=new String("Study4Geeks");  
System.out.println(str);  
System.out.println(str1);  
}}  

Output:

Study4Geeks
Study4Geeks

Java String Methods

Do you know?
String In Java This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.