> For the complete documentation index, see [llms.txt](https://checksound.gitbook.io/corsojava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://checksound.gitbook.io/corsojava/string-is-special.md).

# String è speciale

Una `String` in Java, contiene una sequenza immutabile di caratteri unicode. A differenza del C/C++ dove una stringa è semplicemente un array di char , una `String` in Java è una classe di `java.lang`.

Una `String` in  Java è comunque spaciale. A differenza di una classe ordinaria:

* Una String è associata con una string literal che ha la forma di un testo tra doppie virgolette, esempio: *"Hello World"*. Si può assegnate una string literal direttamente a una variabile di tipo `String` direttamente, senza invocare `new` per creare un'istanza di String da asseganre alla variabile.
* l'operatore '+' è overloaded per concatenare due  operandi di tipo`String`.  l'operatore + non funziona con altri oggetti di tipo ad esempio `Point` o `Circle`.
* Un oggetto di tipo `String` è immutabile. Infatti il contenuto non può essere modificatouna volta che l'ggetto di tipo `String` è creato. Ad esempio il metodo `toUpperCase()` costruisce e ritorna una nuova `String` invece di modificare il contenuto dell'oggetto `String`.

Vedi per tutorial: [http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d\_String.html](http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaEnum.html)

Esempi: <https://github.com/checksound/StringIsSpecial>

### StringBuffer & StringBuilder

Come detto, gli oggetti di tipo String sono immutabili perchè oggetti di tipo String literals con lo stesso contenuto condividono la stessa zona di memoria nel pool delle stinghe. La modifica di una stringa causerebbe anche la modifica anche di quelle che condividono la stessa zona di memoria.

Per modificare le stringhe il JDK fornisce due classi che supportano le stringhe mutabili.  Le classi sono`StringBuffer` e `StringBuilder` (nel package core `java.lang`) .  Un oggetto di  `StringBuffer` o`StringBuilder` è come un oggetto ordinario, che sono memorizzati nello heap e non condivisi, e quindi, possono essere modificat senza effetti indesiderati su altri oggetti.

La classe `StringBuilder` fu introdotta  nel JDK 1.5. E' la stessa come funzionalità di `StringBuffer` , unica differenza che `StringBuilder`non è *synchronized* per operazioni multi-thread. Tuttavia, per programmi single-thread, `StringBuilder`, senza l'overhead della sincronizzazione, è più efficiente.


---

# 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, and the optional `goal` query parameter:

```
GET https://checksound.gitbook.io/corsojava/string-is-special.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
