Goseeko blog

What is a Java String?

by Bhumika

A Java string is a java.lang object that contains a collection of characters. Strings are created and manipulated using Java’s string class. The value of a string cannot be changed once it has been created.

A string in Java is an object that represents a collection of char values. A character array works in the same way as a Java string.

Creating Java Strings

The simplest approach to make a string is to type.

String greeting = “Hello Glossaread”;

When the compiler encounters a string literal in your code, it creates a String object with the value “Hello Glossaread!”

String constructor

​​In Java APIs, the String class provides a variety of constructors.

The constructors of the String class that are most typically uses, as follows:

  • String()
  • String(String str)
  • String(char chars[ ])
  • String(char chars[ ], int startIndex, int count)
  • String(byte byteArr[ ])
  • String(byte byteArr[ ], int startIndex, int count)

String() – We’ll use the default constructor to make an empty String. The following is the usual syntax for creating an empty string in a Java program:

String s = new String();

It will generate a string object with no value in the memory region.

String(String str) – In the heap region, it will build a string object and store the specified value in it. The following is the standard syntax for creating a string object with a provided string str:

String st = new String(String str);

String(char chars[ ]) – The array of characters is stores in a string object create by this constructor. The following is the basic syntax for creating a string object with a provided array of characters:

String str = new String(char char[])

String(char chars[ ], int startIndex, int count) – With a subrange of a character array, this constructor builds and initializes a string object.

The startIndex and count arguments specify the index at which the subrange begins and the number of characters to be copied, respectively.

The following is the standard syntax for creating a string object with the specified subrange of a character array:

String str = new String(char chars[ ], int startIndex, int count);

String(byte byteArr[ ]) – This constructor creates a new string object by decoding the given array of bytes (i.e., ASCII values into characters) using the system’s default character set.

String(byte byteArr[ ], int startIndex, int count) – The ASCII values are decoded using the system’s default character set, and a new string object is created.

String Length 

This method returns the string’s length. The length of the string is equal to the number of 16-bit Unicode characters.

Syntax 

public int length()

Example 

​​public class Stringoperation5  

{  

public static void main(String ar[])  

{  

String s=”Sachin”;    

System.out.println(s.length());//6    

}  

}  

Output 

6

Interested in learning about similar topics? Here are a few hand-picked blogs for you!

  1. What is JDBC connectivity?
  2. Describe programming?
  3. What is object oriented programming?
  4. Explain Recursion?

You may also like