Java String Class Tutorial

Java String class represents character strings. The java.lang.String class provides a lot of methods to work on string. Java String is not a primitive data type like int and long. It is basically an object that represents sequence of char values . It is like an array of characters works same as java string.
char[] chr={'W','E','L','C','O','M','E'};

How to create a Java String object?

There are two ways to create Java String object:

  1. By string literal
  2. By new keyword

String Literal

Java String literal is created by using double quotes.

String s="Java Tutorial";

By using new keyword

String str = new String("Java Tutorial");

Difference between String literal and New String object

When you use new String( "Java Tutorial" ), it explicitly creates a new and referentially distinct instance of a String object. It is an individual instance of the java.lang.String class. String s="Java Tutorial"; may reuse an instance from the string constant pool if one is available (String Pool is a pool of Strings stored in Java heap memory ). Moreover, it is a Java language concept and when you use a string literal the string can be interned. Example:
String a = "JAVA"; String b = "JAVA"; System.out.println(a == b);
The output of the above program is 'true' .
String c = new String("JAVA"); String d = new String("JAVA"); System.out.println(c == d);
The output of the above program is 'false' . It is better you should use the string literal notation when possible. It is easier to read and it gives the compiler a chance to optimize your code. Passing a null argument to a String constructor or String method will cause a NullPointerException to be thrown. Also in terms of good coding practice: do not use == to check for String equality, use .equals() instead.

First-Class Objects

Strings in Java are First-Class Objects. By convention, in programming language 'C' and 'C++' strings are null-terminated array of characters and there is no real entity in 'C' and 'C++' that is a string. But in Java, strings are first-class objects and it provides several advantages to the developers, they are:
  1. The manner in which you obtain strings and elements of strings is consistent across all strings and all systems.
  2. Because of the programming interface for the String classes is well-defined, Java Strings function predictably every time.
  3. The Java String class does extensive runtime checking for boundary conditions and catches errors.

Thread-safe

Strings in Java are immutable . An immutable object is an object that will not change its state after its construction You can't modifying the String, you're just pointing to another value. Hence it's thread-safe and can be safely used in multi-threaded environment.

Escape Characters

Strings literals in Java accepts a set of escape characters which are translated into special characters in the created String. They are:
\\ Translated into a single \ character \t Translated into a single tab character \r Translated into a single carriage return character \n Translated into a single new line character
Example:
String path = "D:\\Java\\JavaProjects";

Innstead of '\' use '\\'

String str = "\tJava Tutorial\r\n";

The above String starts with a tab character and ends with a carriage return and a new line character.

String is immutable or final

Because String is immutable or final , there are several advantage in programming.
  1. String is safe to use in multi-threading programming and developers don't need any synchronization.
  2. Because of String immutability, string pool is possible.
  3. Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader.