> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/ud1-introduccion-a-programacion/execucion-secuencial/conversion-de-tipos.md).

# Conversión de tipos

O **casting** en Java é o proceso de **converter un tipo de dato en outro**. Existen dous tipos principais de *casting*: **casting implícito** e **casting explícito**. Aquí che explico ambos:

O **Casting Implícito**, tamén coñecido como **promoción automática**, ocorre cando Java converte automaticamente un tipo de dato menor a un tipo maior. Isto é seguro porque non se perderán datos.

```java
int numeroEntero = 100;
double numeroDouble = numeroEntero; // Casting implícito de 'int' a 'double'
```

Neste exemplo, o `int` `numeroEntero`<i class="fa-copy">:copy:</i> é convertido automaticamente a `double`<i class="fa-copy">:copy:</i> porque `double`<i class="fa-copy">:copy:</i> ten un rango maior.

O **Casting Explícito** é necesario cando se converte un tipo de dato maior a un tipo menor. Neste caso, pode haber perda de datos, por iso debes indicar explicitamente que desexas realizar a conversión utilizando parénteses.

```java
double numeroDouble = 9.78;
int numeroEntero = (int) numeroDouble; // Casting explícito de 'double' a 'int'
```

Neste exemplo, o valor `9.78` será truncado a `9` durante a conversión a `int`.

## Reglas de promoción numérica

1. Se hay `double` → resultado é `double`.
2. Se hay `float` → resultado é `float`.
3. Se hay `long` → resultado é `long`.
4. Se hay `int` → resultado é `int` (incluso con `short`, `byte`, `char` que se promocionan a `int`).
5. `byte`, `short`, `char` promociónanse a `int` ao operar entre eles ou con outros tipos, excepto cando xa hai un tipo maior (`long`, `float`, `double`).
6. As literais con punto decimal son `double` por defecto, a non ser que teñan sufixo `f` (`float`).
7. As literais enteiras son `int` por defecto, a non ser que teñan sufixo `L` (`long`).

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/ud1-introduccion-a-programacion/execucion-secuencial/conversion-de-tipos.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
