Loading

Java String

  • A string is a group of characters surrounded by double quotes (“ ”). 
  • In Java, a string is an object of the String class, that represents a sequence of characters. 

Ways to create String object are: -   

  1. By string literal 
  2. By new keyword

1. By string literal: - It uses double quotes (“ ”) to create a string.

Example: 

String s = “QuipoHouse”;

 2.By new keyword: - It uses the “new” keyword to create a string.

Example:

String s = new String(“QuipoHouse”);

Example: 

package quipohouse;
public class Java_String
{
	public static void main(String []args)
	{
		String s = “Hi”;
		char c = {‘Q’,’u’,’i’,’p’,’o’,’H’,’o’,’u’,’s’,’e’};
		String s1 = new String(c);
		String s2 = new String(“Team”);
		System.out.println(s);
		System.out.println(s1);
		System.out.println(s2);
	}
}

Output: 

Hi
QuipoHouse
Team

String Methods: - Some methods that are supported in the characters string class are :

S.No.

Method

Working

1

Char charAt(int Index)

Return character at specified index

2

boolean isEmpty()

Check if string is empty

3

String toLowerCase()

Return the string in lowercase

4

String concat(String str)

Concatenate two strings

5

String toUpperCase()

Return the string in uppercase

6

String replace(char old, char new)

Replace all old characters with a new character

7

int indexOf(int ch)

Return index of specified character

8

String trim()

Remove starting and ending spaces of this string

9

int length()

Return the length of the string

10

boolean equalsIgnoreCase(String anotherString)

It compare two strings, ignoring their case considerations


StringBuffer

  • It is the same as the String class but is used to create mutable objects.
  • All methods are synchronized.
  • Multiple threads can use it.
  • It is initialized using the ‘new’ keyword.

Syntax: 

StringBuffer sb = new StringBuffer("string");

Example:

package quipohouse;
class String_Buffer
{
    public static void main(String args[])
    {
        StringBuffer sb = new StringBuffer("Quipo");
        sb.append("House");
        System.out.println(sb);
    }
}

Output:

QuipoHouse

StringBuilder

  • It is also used to create mutable objects.
  • It is the same as the StringBuffer class but it is non-synchronized.
  • It is not safe to use in multiple threads.
  • It is initialized using the ‘new’ keyword.

Syntax:

StringBuilder sbr = new StringBuilder("string");

Example:

package quipohouse;
class String_Buffer
{
    public static void main(String args[])
    {
        StringBuilder sbr = new StringBuilder("Quipo");
        sbr.append("House");
        System.out.println(sbr);
    }
}

Output:

QuipoHouse

String vs StringBuffer vs StringBuilder

Topic

String

StringBuffer

StringBuilder

Storage

It uses Heap Memory and String Constant Pool

It uses Heap Memory

It uses Heap Memory

Object

It creates an immutable object

It creates a mutable object

It creates a mutable object

Memory

It occupies a large amount of memory

It occupies less amount memory

It occupies less amount memory

Thread safety

It is not thread-safe

Methods are synchronized so it is thread-safe

Methods are not synchronized so it is not thread-safe

Performance

Its performance is slow

Its performance is fast as compared to String

Its performance is fast as compared to StringBuffer

Use

If data does not change

If data changes frequently

If data changes frequently