String

String #

There are 3 main ways to create a string in Java.

Declating a String Using New Operator #

String myString = new String("Java Tutorials Dev");

The String object is immutable, in other words it is not modifiable in the memory. When a new string is assigned to a string object Java changes the pointer to the new string and the old string stays in its old memory location.

Declating a String Without New Operator #

String myString = "Java Tutorials Dev";

Declating a String With StringBuilder #

StringBuilder str = new StringBuilder();
str.append("Java Tutorials Dev");

StringBuilder should be used when the string needs to be updated multiple times.