top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

String Class in Java

Introduction


The use of strings is essential to programming. Java has a String class that allows for the creation and manipulation of strings. The character sequence can also be represented using an interface called Char Sequence. One of the classes that implement this interface is the String class. Thus, in Java, a string is an object that is essentially a sequence of characters.

For instance, the word “hello” is made up of a string of the characters “h,” “e,” “l,” “l,” and “o.” Additionally, we have a variety of String methods that make it simple to interact with String in Java.

What is String Class in Java

String refers to a group of characters. String objects in Java are immutable, which simply means they can never be modified after they have been created.


only supports operator overloading for the String class. The + operator allows us to combine two strings. For instance, “a”+”b”=”ab.” StringBuilder and StringBuffer are two helpful classes in Java that can be used to manipulate strings.

Create a String in Java:


class Main {
public static void main(String[] args) {
// create strings
String first = “Java”;
String second = “Python”;
String third = “JavaScript”;
// print strings
System.out.println(first); // print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
}
}
OUTPUT-java -cp /tmp/m2Ed9NVyqB Main

Java

Python

JavaScript

In the above example, we have created three strings named first, second, and third. Here, we are directly creating strings like primitive types.

However, there is another way of creating Java strings (using the new keyword).

Note: Strings in Java are not primitive types (like int, char, etc). Instead, all strings are objects of a predefined class named String.

And, all string variables are instances of the String class.

Java String Operations

Java String provides various methods to perform different operations on strings. We will look into some of the commonly used string operations.

1. Get length of a String

To find the length of a string, we use the length() method of the String. For example,


class Main {
public static void main(String[] args) {
// create a string
String greet = “Hello! World”;
System.out.println(“String: “ + greet);
// get the length of greet
int length = greet.length();
System.out.println(“Length: “ + length);
}
}
Output
String: Hello! World
Length: 12
In the above example, the length() method calculates the total number of characters in the string and returns it.

2. Join Two Java Strings

We can join two strings in Java using the concat() method. For example,


class Main {
public static void main(String[] args) {
// create first string
String first = “Java “;
System.out.println(“First String: “ + first);
// create second
String second = “Programming”;
System.out.println(“Second String: “ + second);
// join two strings
String joinedString = first.concat(second);
System.out.println(“Joined String: “ + joinedString);
}
}

Output

First String: Java

Second String: Programming

Joined String: Java Programming

In the above example, we have created two strings named first and second. Notice the statement,

String joinedString = first.concat(second);

Here, the concat() method joins the second string to the first string and assigns it to the joined string variable.

We can also join two strings using the + operator in Java.

3. Compare two Strings

In Java, we can make comparisons between two strings using the equals() method. For example,


class Main {
public static void main(String[] args) {
// create 3 strings
String first = “java programming”;
String second = “java programming”;
String third = “python programming”;
// compare first and second strings
boolean result1 = first.equals(second);
System. out.println(“Strings first and second are equal: “ + result1);
// compare first and third strings
boolean result2 = first.equals(third);
System. out.println(“Strings first and third are equal: “ + result2);
}
}

Output

Strings first and second are equal: trueStrings first and third are equal: false

In the above example, we have created 3 strings named first, second, and third. Here, we are using the equal() method to check if one string is equal to another.

he equals() method checks the content of strings while comparing them.

strings named first, second, and third. Here, we are using the equal() method to check if one string is equal to another.

The equals() method checks the content of strings while comparing them. To learn more, visit Java String equals().

Note: We can also compare two strings using the == operator in Java. However, this approach is different than the equals() method.

Escape character in Java Strings:

The escape character is used to escape some of the characters present inside a string.

Suppose we need to include double quotes inside a string.

// include double quote

String example = “This is the “String” class”;

Since strings are represented by double quotes, the compiler will treat “This is the “ as the string. Hence, the above code will cause an error.

To solve this issue, we use the escape character \ in Java. For example,

// use the escape character

String example = “This is the \”String\” class.”;

Now escape characters tell the compiler to escape double quotes

Java Strings are Immutable

In Java, strings are immutable. This means, once we create a string, we cannot change that string.

To understand it more deeply, consider an example:

// create a string

String example = “Hello! “;

Here, we have created a string variable named example. The variable holds the string “Hello! “.


Conclusion


That's all about String Class in Java. The string is very special in java. It has some unique features like immutability, concatenation, support, and caching.


19 views0 comments

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page