String in Java

In this tutorial, we will learn about String in Java. Some students think String is a primitive data type like int or char but In java String is a very smart and immutable class which is part of java.lang package. When we right a char sequence it represents the object of String class. Check the following example to know the syntax to define a string in java.

String name="Owlbuddy";

As in syntax you can see first of all we have to write String(Class name) then the reference variable then = operator and any string in double quotes(” “);

Define a String in Java:

There are two ways to define String is Java.

  • Using String literal: Strings with the same char sequence will refer to the same object.
  • Using the new keyword: All the strings will refer to different objects even in case of the same char sequence.

Example Program(Using String literal):

class Example{
  public static void main(String args[]){
      String name="Owlbuddy";
      System.out.println(name);
  }
}

Example Program(Using the new keyword):

class Example{
  public static void main(String args[]){
      String name=new String("Owlbuddy");
      System.out.println(name);
  }
}

The string in Java is an Immutable Class:

The string is an immutable class means once we created an object of this class we can not change it. To understand is clearly check out the following example?

Example Program:

class Example{
  public static void main(String args[]){
      String name="Owlbuddy";
      String name1="Owlbuddy";
      String name2="Owlbuddy";
      System.out.println(name);
  }
}

Before explaining the above example here is a question for you. How many String objects present in the above example? if your answer is one then you are right. In the above example, we have only one string object name,name1 and name2 are reference variables which are pointing out to String object “Owlbuddy”. Check the following illustration to understand it clearly.

Now we will see what will happen if we will try to change String object in Java(Which is not possible because String is an immutable class).

Example Program:

class Example{
  public static void main(String args[]){
      String name="Owlbuddy";
      String name1="Owlbuddy";
      String name2="Owlbuddy.com";
      System.out.println(name);
  }
}

In the above example, you can see we have changed the char sequence in name2 variable. It will not change the previous object “Owlbuddy”. Actually it will create a new String Object and will start to point out new object. Check the following illustration to understand it more wisely.

String class methods:

We know String is a class so that’s why there are a lot of methods which are available in String class. which we can use while working with string objects.

Method Description
charAt(int index) This method returns char value for the particular index of String
length() This method returns the length of the String
format(String format, Object… args) This method returns a formatted string.
format(Locale l, String format, Object… args) This method returns formatted string with given locale.
substring(int beginIndex) This method returns returns substring form given starting index.
substring(int beginIndex, int endIndex) This method returns substring between given starting index and end index.
contains(CharSequence s) This method returns true or false according to string contain given Char Sequence or not
join(CharSequence delimiter, CharSequence… elements) It method returns a joined string.
join(CharSequence delimiter, Iterable<? extends CharSequence> elements) It method returns a joined string.
equals(Object another) This method is used to match two strings.
isEmpty() This method returns true checks if string is empty.
concat(String str) This method is to concatenate the specified string with the string object.
replace(char old, char new) This method is used to replaces all occurrences of the specified char value in the string.
replace(CharSequence old, CharSequence new) This method is used to replaces all occurrences of the specified CharSequence in String.
equalsIgnoreCase(String another) This method is also used to match two strings. But it doesn’t check case(Upper Case or Lower Case).
split(String regex) This method returns a split string matching regex.
split(String regex, int limit) This method returns a split string matching regex and in this method, you can also pass limit.
intern() This method is used returns an interned string.
indexOf(int ch) This method is used to get the index number of a particular character in the string.
indexOf(int ch, int fromIndex) This method returns the specified char value index starting with given index.
indexOf(String substring) This method is used to get the specified substring index.
indexOf(String substring, int fromIndex) This method is used to get the specified substring index starting with given index.
toLowerCase() This method returns a string in lowercase.
toLowerCase(Locale l) This method returns a string in lowercase using specified locale.
toUpperCase() This method returns a string in uppercase.
toUpperCase(Locale l) This method returns a string in uppercase using specified locale.
trim() This method used to remove unwanted spaces at the beginning and ending of this string.
valueOf(int value) This method used to convert given type into a string.
Spread the love
Scroll to Top