Strings in java

Let’s learn strings in java.

Strings in java

Java strings is a sequence of characters which represents character values. To create string in java, java.lang.String package is used.

Objects of String are immutable in java. There are two ways to create string,


char[] ch = {‘G’,‘e’,‘o’,‘r’,‘g’,‘e’};

String name = new String(ch); // string literal

String name = “George”;

strings in java

Another is using “new” keyword,

String name = new String(“Hello World”);

Here’s an example on declaring a string,

public class StringDemo 
   public static void main(String[] args) 
      // string literal java 
      String str1 = "helloworld"; 
      char[] chArray = {'c','o','r','e','j','a','v','a'};
      // convert char array to string 
      String str2 = new String(chArray);
      // creating by new keyword 
      String str3 = new String("flowerbrackets"); 



String class implements CharSequence, Serializable and Comparable interface.

StringBuffer class, StringBuilder class and String class are used to create string in java. These three classes implement CharSequence interface.

When you create a java string object, two objects are created, one in heap memory area and another in string constant pool.

In java String object reference points to heap memory area.

Java string methods

static String valueOf(int value)this overloaded method converts given type to string
char charAt(int index)this method returns char value at specified index.
String trim()this method removes leading and trailing whitespace.
int length()returns length of given string.
String toUpperCase(Locale locale)converts all characters in the given string to upper case using rules of given Locale.
static String format(String format, Object... args)returns formatted string using specified format string and arguments.
String toUpperCase()converts all the characters to upper case.
static String format(Locale l, String format, Object... args)returns formatted string using the specified locale, format string, and arguments.
String toLowerCase(Locale l)converts all the characters to lower case using rules of given Locale.
String substring(int beginIndex)returns a new string that is substring. substring begins with given beginIndex.
String substring(int beginIndex, int endIndex)returns new string that is substring with given beginIndex and endIndex.
boolean isEmpty()checks if length() is 0, otherwise false.
String concat(String str)concatenates specified string to end of given string.
boolean equals(Object anObject)Compares this string to the specified object.
String replace(CharSequence target,CharSequence replacement)replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String intern()returns a canonical representation for the string object.
int indexOf(int ch)returns index of the first occurrence of the specified character.
String toLowerCase()converts all the characters of String to lower case.
String replace(char oldChar, char newChar)replaces all occurrences of oldChar in given string with newChar.

Reference – Oracle Docs