Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen What Is String? | Section
Java Fundamentals

bookWhat Is String?

String and How to Use It

In Java, a String is a data type used to represent text. Its syntax looks like this:

Main.java

Main.java

copy
1
String name = "string";

You've already encountered a String when you displayed something on the screen using double quotation marks (""). A string can also be output like any other variable, for example:

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { // Initializing a String variable String hello = "Hello world!"; // Printing the String variable System.out.println(hello); } }

A string is a data type that cannot be changed after initialization. This is called an immutable class. You can directly modify the value of an already initialized string variable, but in doing so, we simply create a new object in memory. For example:

Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { // Initializing a String variable String hello = "Hello world!"; // Changing the reference to a new object in heap memory hello = "Hello c<>definity"; // Printing the updated String variable System.out.println(hello); } }

We can also create arrays of strings:

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { String[] array = {"Hello", ", ", "c<>definity"}; for (String element : array) { System.out.print(element); } } }

Structure of a String

In Java, strings are sequences of characters stored as objects of the String class. Each character in a string is assigned a specific position, called an index, starting from zero.

The image illustrates how the string "Codefinity" is broken down into characters with their corresponding indices. Indexing starts at zero, where C is at index 0, and the last character, y, is at index 9.

This structure makes it easy for developers to work with individual characters in a string using methods from the String class, which we will explore in the next chapter.

1. Will the following code compile?

2. Can we create an array of String data type?

question mark

Will the following code compile?

Select the correct answer

question mark

Can we create an array of String data type?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 29

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookWhat Is String?

Swipe um das Menü anzuzeigen

String and How to Use It

In Java, a String is a data type used to represent text. Its syntax looks like this:

Main.java

Main.java

copy
1
String name = "string";

You've already encountered a String when you displayed something on the screen using double quotation marks (""). A string can also be output like any other variable, for example:

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { // Initializing a String variable String hello = "Hello world!"; // Printing the String variable System.out.println(hello); } }

A string is a data type that cannot be changed after initialization. This is called an immutable class. You can directly modify the value of an already initialized string variable, but in doing so, we simply create a new object in memory. For example:

Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { // Initializing a String variable String hello = "Hello world!"; // Changing the reference to a new object in heap memory hello = "Hello c<>definity"; // Printing the updated String variable System.out.println(hello); } }

We can also create arrays of strings:

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { String[] array = {"Hello", ", ", "c<>definity"}; for (String element : array) { System.out.print(element); } } }

Structure of a String

In Java, strings are sequences of characters stored as objects of the String class. Each character in a string is assigned a specific position, called an index, starting from zero.

The image illustrates how the string "Codefinity" is broken down into characters with their corresponding indices. Indexing starts at zero, where C is at index 0, and the last character, y, is at index 9.

This structure makes it easy for developers to work with individual characters in a string using methods from the String class, which we will explore in the next chapter.

1. Will the following code compile?

2. Can we create an array of String data type?

question mark

Will the following code compile?

Select the correct answer

question mark

Can we create an array of String data type?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 29
some-alt