codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

10 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Strings
☕Java Programming

Java Strings

Updated 2026-05-12
15 min read

Java Strings

In Java, a String is a sequence of characters used to represent text. Understanding how to work with strings is fundamental in any programming language because almost every application deals with some form of textual data. In this tutorial, we'll explore various aspects of Java strings, including string methods such as length, toUpperCase, and indexOf, string concatenation, and escape sequences.

Introduction

Strings are immutable sequences of characters. This means that once a string is created, its value cannot be changed. Instead, any operation that modifies a string actually creates a new string object. Understanding this immutability is crucial for efficient memory management in Java applications.

In this tutorial, we'll cover the following topics:

  • String methods like length, toUpperCase, and indexOf.
  • How to concatenate strings using both the + operator and the concat method.
  • Common escape sequences used in strings.

Core Content

1. String Methods

a. Length of a String

The length() method returns the number of characters in a string.

StringLength.java
1public class StringLength {
2 public static void main(String[] args) {
3 String text = "Hello, World!";
4 int length = text.length();
5 System.out.println("The length of the string is: " + length);
6 };
7}
Output
The length of the string is: 13

b. Convert to Uppercase

The toUpperCase() method converts all characters in a string to uppercase.

StringToUpper.java
1public class StringToUpper {
2 public static void main(String[] args) {
3 String text = "Hello, World!";
4 String upperCaseText = text.toUpperCase();
5 System.out.println("Uppercase: " + upperCaseText);
6 }
7}
Output
Uppercase: HELLO, WORLD!

c. Find the Index of a Character

The indexOf() method returns the index of the first occurrence of a specified character or substring.

StringIndexOf.java
1public class StringIndexOf {
2 public static void main(String[] args) {
3 String text = "Hello, World!";
4 int index = text.indexOf('o');
5 System.out.println("Index of 'o': " + index);
6 }
7}
Output
Index of 'o': 4

2. String Concatenation

String concatenation is the process of joining strings together.

a. Using the + Operator

The + operator can be used to concatenate strings.

StringConcatPlus.java
1public class StringConcatPlus {
2 public static void main(String[] args) {
3 String firstName = "John";
4 String lastName = "Doe";
5 String fullName = firstName + " " + lastName;
6 System.out.println("Full Name: " + fullName);
7 }
8}
Output
Full Name: John Doe

b. Using the concat Method

The concat() method is another way to concatenate strings.

StringConcatMethod.java
1public class StringConcatMethod {
2 public static void main(String[] args) {
3 String firstName = "John";
4 String lastName = "Doe";
5 String fullName = firstName.concat(" ").concat(lastName);
6 System.out.println("Full Name: " + fullName);
7 }
8}
Output
Full Name: John Doe

3. Escape Sequences

Escape sequences are special characters that represent certain non-printable or special characters in strings.

Escape SequenceDescription
\nNew line
\tTab
\Backslash
\'Single quote
\"Double quote
StringEscapeSequences.java
1public class StringEscapeSequences {
2 public static void main(String[] args) {
3 String text = "Hello,
4World! This is a backslash: ";
5 System.out.println(text);
6 }
7}
Output
Hello,
World!	This is a backslash:

Practical Example

Let's create a simple program that takes two strings, concatenates them with a space in between, converts the result to uppercase, and prints it.

StringPracticalExample.java
1public class StringPracticalExample {
2 public static void main(String[] args) {
3 String firstName = "john";
4 String lastName = "doe";
5
6 // Concatenate with a space
7 String fullName = firstName + " " + lastName;
8
9 // Convert to uppercase
10 String upperCaseFullName = fullName.toUpperCase();
11
12 System.out.println("Uppercase Full Name: " + upperCaseFullName);
13 }
14}
Output
Uppercase Full Name: JOHN DOE

Summary

  • String Methods: length(), toUpperCase(), and indexOf() are commonly used string methods.
  • Concatenation: Strings can be concatenated using the + operator or the concat method.
  • Escape Sequences: Special characters in strings are represented using escape sequences.

What's Next?

Now that you've learned about Java strings, it's time to explore mathematical operations and calculations with numbers. In the next tutorial, we'll dive into Java Math, where you'll learn how to perform various arithmetic operations and use math-related classes and methods. Stay tuned!


PreviousJava OperatorsNext Java Math

Recommended Gear

Java OperatorsJava Math