EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java 11 Tutorial Java 11 String
Secondary Sidebar
Java 11Tutorial
  • Java 11 Tutorial
    • Java 11 String
    • Java 11 openJDK
    • Java 11 Windows
    • Java 11 var
    • Java 11

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more