Mark As Completed Discussion

Strings

Strings in Java are objects that represent a sequence of char values. A String can be created in two ways:

  1. Using a literal

  2. Using new keyword

TEXT/X-JAVA
1String str = "Hello World"; //using literal
2
3String website = new String("AlgoDaily"); //using new keyword

Note that the java.lang.String class implements Serializable, Comparable, and CharSequence interfaces. Since the String object is immutable(unable to be changed) in nature Java provides 2 utility classes:

  1. StringBuilder, which is a mutable class that is not thread-safe but is faster and used in single-threaded

  2. StringBuffer, which is a mutable class that is thread-safe and synchronized.

Below are some of the most useful String methods: