Java - String String Basics

What is a String?

A sequence of zero or more characters is known as a string.

Java string is represented by an object of the java.lang.String class.

String class is immutable. The contents of a String object cannot be modified after it has been created.

StringBuilder and StringBuffer

The String class has two companion classes, java.lang.StringBuilder and java.lang.StringBuffer.

The companion classes are mutable.

For example, to accumulate a string value to create a longer string value, you should use StringBuilder or StringBuffer;

The following code creates a String object and output its value

Demo

public class Main {
  public static void main(String[] args) {
    String s = "book2s.com";
    System.out.println(s);//w  ww  .  ja  va 2 s .  co  m
  }
}

Result

Exercise