Understanding Strings in Java: length and concat Methods

In Java, a string is a sequence of characters. It is represented by the String class, which provides various methods for manipulating and working with strings. In this tutorial post, we will explore two essential methods of the String class: length and concat.

What is a String in Java?

A string in Java is an immutable sequence of characters. It can be created by enclosing characters within double quotes, like this:

String message = “Hello, World!”;

Strings are commonly used to store and manipulate textual data in Java programs. They can contain letters, digits, special characters, and even Unicode characters.

Let’s look at a few examples of strings:

String name = "John Doe";
String city = "New York";
String code = "ABC123";

Strings can hold various types of data, making them versatile for a wide range of applications.

The length Method

The length method in Java is used to give a count of characters present in a string. It provides an integer value that signifies the length of the string. Here’s an example illustrating the usage of the length method:

String message = "Hello, World!";
int length = message.length();
System.out.println("The length of the string is: " + length);

Output

The length of the string is: 13

Let’s explore a few more examples:

String name = "Alice";
int nameLength = name.length(); // Output 5
String sentence = "The quick brown fox";
int sentenceLength = sentence.length(); //Output 20
String empty = "";
int emptyLength = empty.length(); // Output 0

The concat Method

The concat method is used to concatenate or join two strings together. It takes another string as an argument and returns a new string that is a concatenation of the two. Here’s how you can use the concat method:

String str1 = "Hello";
String str2 = "World";
String result = str1.concat(str2);
System.out.println(result);

Output

HelloWorld


String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName);
System.out.println(fullName);

Output

John Doe


String text = "Java";
String version = "8";
String fullText = text.concat(" SE ").concat(version);
System.out.println(fullText);

Output

Java SE 8


String word = "Hello";
String repeatedWord = word.concat(word).concat(word);
System.out.println(repeatedWord);

Output

HelloHelloHello


String str1 = "Hello";
String str2 = "World";
String result = str1.concat(", ").concat(str2).concat("!");
System.out.println(result);

Output

Hello, World!


String text = "Java";
String version = "11";
String fullText = "Welcome to " + text.concat(" SE ").concat(version);
System.out.println(fullText);

Output

Welcome to Java SE 11


Conclusion

In this tutorial post, we explored the String class in Java, focusing on the length and concat methods. We learned that strings are sequences of characters used to represent textual data. The length method allows us to determine the length of a string, and the concat method enables us to concatenate two strings together.

By utilizing the length and concat methods, you can manipulate, validate, and combine strings effectively in your Java programs.