Java String Class TutorialJava 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.
String s="Java Tutorial";
By using new keyword
String str = new String("Java Tutorial");
Difference between String literal and New String objectWhen 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 ObjectsStrings 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.
Thread-safeStrings 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 CharactersStrings 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 characterExample:
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 finalBecause 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.
- How to Get the Length of a String
- Java String charAt() Method
- String indexOf() method
- Java String replace()
- Java String contains()
- String Comparison in Java
- Java String substring()
- Java String concat() Method
- Java String split() method
- Convert String to int
- Java StringBuilder Class
- StringTokenizer in Java
- How to convert int to String in Java?