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.
How to create a Java String object?
There are two ways to create Java String object:
- By string literal
- By new keyword
Java String literal is created by using double quotes.
By using new keyword
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.
The output of the above program is 'true' .
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.
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:
- The manner in which you obtain strings and elements of strings is consistent across all strings and all systems.
- Because of the programming interface for the String classes is well-defined, Java Strings function predictably every time.
- The Java String class does extensive runtime checking for boundary conditions and catches errors.
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.
Strings literals in Java accepts a set of escape characters which are translated into special characters in the created String. They are:
Innstead of '\' use '\\'
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.
- String is safe to use in multi-threading programming and developers don't need any synchronization.
- Because of String immutability, string pool is possible.
- Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader.