0
0
Javaprogramming~15 mins

String creation in Java

Choose your learning style8 modes available
menu_bookIntroduction

Strings hold text in programs. Creating strings lets you work with words, sentences, or any text data.

When you want to store a name or label.
When you need to display messages to users.
When you want to read or write text data.
When you need to compare or manipulate text.
When you want to build sentences from smaller parts.
regular_expressionSyntax
Java
String variableName = "text";

Strings are enclosed in double quotes.

Use the String class to create text variables.

emoji_objectsExamples
line_end_arrow_notchThis creates a string variable named greeting with the text Hello.
Java
String greeting = "Hello";
line_end_arrow_notchThis creates an empty string with no characters.
Java
String empty = "";
line_end_arrow_notchThis string holds multiple words separated by a space.
Java
String multiWord = "Java programming";
code_blocksSample Program

This program creates a string with a name and then combines it with other text to make a welcome message. It prints the message to the screen.

Java
public class Main {
    public static void main(String[] args) {
        String name = "Alice";
        String welcomeMessage = "Welcome, " + name + "!";
        System.out.println(welcomeMessage);
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Strings in Java are objects, so you can use many built-in methods to work with them.

line_end_arrow_notch

Strings are immutable, meaning once created, their content cannot change.

list_alt_checkSummary

Strings store text using double quotes.

Create strings with the String class and assign text to variables.

You can combine strings using the + operator.