Definition of Java 11 String
Java 11 string class represents the string of characters. All literals of string in java are implemented by using the class of the instance. Values are constant; while creating the value of a string, we cannot change it after execution. Java string buffers support the string that was mutable because the object of the string is immutable. We cannot share the object of the string.
Introduction to Java 11 String
We are using string widely in java and its classes; it contains the sequence of characters. The java string is nothing but the sequence of characters that we have defined for a specified object. We need to declare a string to the specified object.
At the time of creating a literal string, the JVM will be checking the constant pool first. If the string already exists in a pool, then the reference of the instance will be returned. If string did not exist in the pool, then a new instance of string pool is created and placed same into the pool. We are creating the string object by using two ways first is a string literal, and the second is a new keyword.
Java 11 String Class
We are creating a string in java 11 by creating the object. In the below example, we have created the object as st at the time of creating the string as follows:
Example:
Code:
String st = "ABC";
At the time string will encounter in our code, Compiler creates the string object by using a string value. We can also create the string object by using a new keyword. The string class contains the 11 constructors that allow us to provide a value of the string by using multiple sources.
The below example shows the java string class; in the below example, we have provided the initial value to the string.
Code:
public class java_string {
public static void main(String[] args)
{
char[] har = { 'A', 'B', 'P', 'Q', 'C', '.' };
String hst = new String (har);
System.out.println (hst);
}
}
Output:
We are using methods for obtaining information on the object known as methods of an ancestor. We are using the ancestor method with the length() method, which returns characters.
The below example shows the length() method.
Code:
public class java_string {
public static void main(String[] args)
{
String pal = "stud name is ABC";
int len = pal.length ();
System.out.println ( "Length : " + len );
}
}
Output:
Java 11 String Class API
We are defining the API of the java string class by using different methods. In the below example, we are defining the java class API by using a new keyword.
Code:
public class java_string {
public static void main(String[] args)
{
String st = new String ("Java 11 string class API");
int len = st.length ();
System.out.println (" length'" + st + "' is : " + len);
}
}
Output:
To extract the single character, we need to define the below example.
Code:
public class java_string
{
public static void main(String[] args)
{
String st = "Stud details";
char c1 = st.charAt (0);
char c2 = st.charAt (2);
char c3 = st.charAt (3);
char c4 = st.charAt (5);
System.out.println ("Char at 0 idx: " + c1);
System.out.println ("Char at 2 idx: " + c2);
System.out.println ("Char at 3 idx: " + c3);
System.out.println ("Char at 5 idx: " + c4);
}
}
Output:
Methods
Java 11 added the below new methods into the string for the handling of the string.
1. Java string isBlank() method
The isBlank method returns the true value if the string is empty; else returns the false value.
Syntax:
public boolean isBlank()
Example:
Code:
public class java_string
{
public static void main(String[] args) {
String st = "stud name";
boolean nm = st.isBlank();
System.out.println (nm);
st = "";
nm = st.isBlank ();
System.out.println(nm);
}
}
Output:
2. Java string lines() method
The java string lines method is used to get a stream from lines.
Syntax:
Public stream<str> lines ()
Example:
Code:
import java.util.stream.Stream;
public class java_string {
public static void main(String[] args) {
String st = "name \n is ABC \r pune \n A+";
Stream<String> lines = st.lines();
lines.forEach(System.out::println);
}
}
Output:
3. Java string strip method
This method is used to remove trailing and leading spaces from a string.
Syntax:
public String strip()
Example:
Code:
import java.util.stream.Stream;
public class java_string {
public static void main(String[] args) {
String st = " Stud name ABC";
System.out.println (st);
String str2 = st.strip ();
System.out.println (str2);
}
}
Output:
4. Java string repeat method
We are using this method to repeat the string from the specified time.
Syntax:
public String repeat (int count)
Example:
Code:
public class java_string {
public static void main(String[] args) {
String st = "1";
System.out.println(st);
String st2 = st.repeat(10);
System.out.println(st2);
st = "Win";
System.out.println(st);
st2 = st.repeat(15);
System.out.println(st2);
}
}
Output:
5. Java string stripLeading method
Below is the syntax of java string stripLeading method as follows.
Syntax:
public String stripLeading()
Example:
Code:
public class java_string
{
public static void main(String[] args) {
String st = " Stud name";
System.out.println(st);
String st2 = st.stripLeading();
System.out.println(st2);
}
}
Output:
6. Java string stripTrailing method
Below is the syntax of java string stripLeading method as follows.
Syntax:
public String stripTrailing()
Example:
Code:
public class java_string {
public static void main(String[] args) {
String st = " Stud name";
System.out.println (st);
String st2 = st.stripTrailing ();
System.out.println (st2);
}
}
Output:
Examples of Java 11 String
Below are the examples mentioned:
Example #1
In the below example, we are using the new method as follows.
Code:
public class java_string {
public static void main(String[] args) {
String st = new String ("Java 11 string examples");
int len = st.length ();
System.out.println (" len'" + st + "' is : " + len);
}
}
Output:
Example #2
In the below example, we are providing the initial value to the string.
Code:
public class java_string {
public static void main(String[] args)
{
char[] ar = { 'P', 'Q', 'R', 'A', 'B', '.' };
String st = new String (ar);
System.out.println (st);
}
}
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of java 11 string?
Answer: Java string is used in java programming for a character sequence. We are treating an object as a string.
Q2. How many methods we are using in the java 11 string?
Answer: We are using six new methods. Those methods contain multiple features of it.
Q3. Which methods are newly added in the java 11 string?
Answer: Java 11 added new method name as strip(), stripLeading(), stripTrailing(), lines(), isBlank() and repeat() method.
Conclusion
The java string is nothing but the sequence of characters that we have defined for a specified object. We need to declare a string to the specified object. Java string buffers support the string that was mutable because the object of the string is immutable; we cannot share the object of the string.
Recommended Articles
This is a guide to Java 11 String. Here we discuss the introduction, java 11 string class, class API, methods, and examples. You can also look at the following articles to learn more –