EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Java 11 Tutorial Java 11 String
 

Java 11 String

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.

Java 11 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Java 11 String - Public class

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 - length method

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:

Java 11 String API

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:

Java 11 String - Single Char

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:

Java 11 String isBlank

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:

Java string lines

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:

Java string strip

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:

string repeat method

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:

stripLeading method

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:

stripTrailing

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:

new method

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:

initial value

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 –

  1. Java Sending Email
  2. Java Garbage Collection
  3. Java Enum
  4. Java 8 Comparator

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW