Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende ¿Qué Es String? | String
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
¿Qué Es String?

String and How to Use It

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

java

Main

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:

java

Main

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); } }

It looks simple. Well, let's move on to the challenging part then. A string is a data type that cannot be changed after initialization. This is called an immutable class. Don't get stuck on these intimidating words right now; we will discuss them in a separate course. We 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:

java

Main

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:

java

Main

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); } } }

In the next chapter, we will delve into the fundamental methods for working with strings, and you will come to understand why I mentioned that the value of a string variable cannot be altered without specific operations.

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. Will the following code compile?

Will the following code compile?

Will the following code compile?

Selecciona la respuesta correcta

Will the following code compile?

Will the following code compile?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt