📖 Sobre o Java 18
Java 18 introduziu mudanças importantes para internacionalização com UTF-8 como charset padrão, eliminou inconsistências entre plataformas. Também trouxe um Simple Web Server para desenvolvimento e Code Snippets melhorados em JavaDoc, além de avanços em Vector API e Foreign Function & Memory API.
💡 O que são JEPs?
Java 18 incluiu 9 JEPs focados em consistência multiplataforma, ferramentas de desenvolvimento e refinamento de APIs incubator.
🌐 Internacionalização Padronizada
🔹 JEP 400: UTF-8 por Padrão
❓ O que é?
UTF-8 se tornou o charset padrão para todas as operações de I/O, eliminando dependência do sistema operacional.
⚠️ Por que é importante?
Garante comportamento consistente entre diferentes plataformas e elimina problemas de encoding em aplicações distribuídas.
// UTF-8 por padrão no Java 18 - comportamento consistentepublic class UTF8DefaultExample {
// Antes do Java 18 - comportamento dependia do SO public static void beforeJava18() throws IOException { // No Windows: dependia da localização (ex: Windows-1252) // No Linux: geralmente UTF-8 // Comportamento inconsistente! Files.writeString(Path.of("before.txt"), "Olá, café ☕"); }
// Java 18+ - sempre UTF-8 por padrão public static void afterJava18() throws IOException { // Sempre UTF-8, independente do sistema operacional String multilingualText = """ English: Hello World! Português: Olá Mundo! 中文: 你好世界! العربية: مرحبا بالعالم! Emoji: 🌍🚀☕🎉 """;
// Escrever arquivo - sempre UTF-8 Files.writeString(Path.of("multilingual.txt"), multilingualText);
// Ler arquivo - sempre UTF-8 String content = Files.readString(Path.of("multilingual.txt")); System.out.println("Content: " + content);
// FileWriter também usa UTF-8 por padrão try (FileWriter writer = new FileWriter("output.txt")) { writer.write("Default charset: " + Charset.defaultCharset()); writer.write("\n" + multilingualText); } }
// Demonstração de caracteres problemáticos agora funcionando public static void demonstrateConsistency() throws IOException { String[] problematicStrings = { "Café com açúcar", "naïve résumé", "Ñoño español", "你好世界", "🎉🚀☕" };
for (String text : problematicStrings) { Path tempFile = Files.createTempFile("test", ".txt");
Files.writeString(tempFile, text); String readBack = Files.readString(tempFile);
boolean consistent = text.equals(readBack); System.out.printf("%-15s | Consistent: %s%n", text.substring(0, Math.min(text.length(), 15)), consistent ? "✅" : "❌");
Files.deleteIfExists(tempFile); } }}
📚 **Saiba mais**: [UTF-8 Charset JEP](https://openjdk.org/jeps/400)
---
## 🌐 Ferramentas de Desenvolvimento
### 🔹 JEP 408: Simple Web Server
#### ❓ O que é?Servidor HTTP simples e minimalista para prototipagem, testes ad-hoc e servir arquivos estáticos.
#### ⚠️ Por que é importante?Elimina dependências externas para tarefas simples de desenvolvimento e facilita testes locais.
```java// Simple Web Server - uso básicopublic class SimpleWebServerExample {
// Uso via linha de comando public static void commandLineUsage() { /* # Servir diretório atual na porta 8000 jwebserver
# Especificar porta e diretório jwebserver -p 9000 -d /path/to/directory
# Bind específico jwebserver -b 127.0.0.1 -p 8080 */ }
// Servidor programático public static void programmaticServer() throws IOException { var server = SimpleFileServer.createFileServer( new InetSocketAddress(8000), Path.of("."), SimpleFileServer.OutputLevel.VERBOSE );
System.out.println("Server: http://localhost:8000"); server.start();
// Aguardar entrada para parar System.in.read(); server.stop(0); }
// Servidor para desenvolvimento SPA public static void developmentServer() throws IOException { Path webDir = Path.of("webapp"); createWebApp(webDir);
var server = SimpleFileServer.createFileServer( new InetSocketAddress(3000), webDir, SimpleFileServer.OutputLevel.INFO );
System.out.println("Development server: http://localhost:3000"); server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { server.stop(0); })); }
private static void createWebApp(Path webDir) throws IOException { Files.createDirectories(webDir);
String html = """ <!DOCTYPE html> <html> <head> <title>Java 18 Simple Web Server</title> <meta charset="UTF-8"> </head> <body> <h1>🚀 Java 18 Simple Web Server</h1> <p>UTF-8 support: Café ☕ 你好 🌟</p> <script> console.log('Java 18 development server running!'); </script> </body> </html> """;
Files.writeString(webDir.resolve("index.html"), html); }}
📚 **Saiba mais**: [Simple Web Server JEP](https://openjdk.org/jeps/408)
---
## 📚 Documentação Aprimorada
### 🔹 JEP 413: Code Snippets em JavaDoc
#### ❓ O que é?Nova tag `@snippet` para incluir código de exemplo em JavaDoc com syntax highlighting e validação.
#### ⚠️ Por que é importante?Melhora qualidade da documentação com exemplos validados e melhor apresentação.
```java/** * Utility class demonstrating Java 18 features. * * @since 18 */public class Java18Features {
/** * Processes text files with UTF-8 default charset. * * {@snippet : * // Read file with UTF-8 (default in Java 18+) * String content = Files.readString(Path.of("example.txt")); * * // Process multilingual content * if (content.contains("café")) { * System.out.println("Found Portuguese content"); * } * * // Write with UTF-8 (default) * Files.writeString(Path.of("output.txt"), content.toUpperCase()); * } * * @param filePath the file path * @return processed content */ public static String processFile(Path filePath) throws IOException { return Files.readString(filePath).trim().toUpperCase(); }
/** * Creates a development web server. * * {@snippet : * // Create simple file server * var server = SimpleFileServer.createFileServer( * new InetSocketAddress(8000), // @highlight substring="8000" * Path.of("."), * SimpleFileServer.OutputLevel.VERBOSE * ); * * server.start(); // @highlight substring="start()" * System.out.println("Server started on http://localhost:8000"); * } * * @param port the server port * @return configured server */ public static HttpServer createServer(int port) { // Implementation return null; }
/** * Vector operations example. * * {@snippet : * var species = FloatVector.SPECIES_256; * float[] a = {1, 2, 3, 4, 5, 6, 7, 8}; * float[] b = {2, 3, 4, 5, 6, 7, 8, 9}; * float[] result = new float[8]; * * var va = FloatVector.fromArray(species, a, 0); * var vb = FloatVector.fromArray(species, b, 0); * var vr = va.add(vb); // @highlight substring="va.add(vb)" * vr.intoArray(result, 0); * } */ public static void vectorExample() { // Implementation }}
📚 **Saiba mais**: [Code Snippets JavaDoc JEP](https://openjdk.org/jeps/413)
---
## 🚀 Performance Melhorada
### 🔹 JEP 417: Vector API (Segunda Incubação)
#### ❓ O que é?Segunda iteração da Vector API com melhorias de performance e usabilidade.
```java// Vector API melhorada no Java 18import jdk.incubator.vector.*;
public class VectorAPI18 {
private static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
public static void basicOperations() { float[] a = {1, 2, 3, 4, 5, 6, 7, 8}; float[] b = {2, 3, 4, 5, 6, 7, 8, 9}; float[] result = new float[8];
// Operação vetorizada var va = FloatVector.fromArray(SPECIES, a, 0); var vb = FloatVector.fromArray(SPECIES, b, 0);
// Operação complexa: (a * b) + (a / 2) - 1 var multiplication = va.mul(vb); var division = va.div(2.0f); var finalResult = multiplication.add(division).sub(1.0f);
finalResult.intoArray(result, 0);
System.out.println("Result: " + Arrays.toString(result)); }
public static void maskedOperations() { float[] data = {-5, 3, -2, 8, -1, 6, -4, 7}; float[] result = new float[8];
var vector = FloatVector.fromArray(SPECIES, data, 0);
// Máscara para valores positivos var positiveMask = vector.compare(VectorOperators.GT, 0.0f);
// sqrt para positivos, abs para negativos var positiveResult = vector.lanewise(VectorOperators.SQRT, positiveMask); var negativeResult = vector.abs().blend(positiveResult, positiveMask);
negativeResult.intoArray(result, 0);
System.out.println("Masked result: " + Arrays.toString(result)); }}🎯 Impacto e Importância
Java 18 trouxe consistência e ferramentas essenciais:
- ✅ UTF-8 padrão eliminou inconsistências entre plataformas
- ✅ Simple Web Server facilitou desenvolvimento local
- ✅ Code Snippets melhoraram qualidade da documentação
- ✅ Vector API evoluiu para melhor performance
- ✅ Foreign Memory API avançou interoperabilidade nativa
📅 Informações da Versão
- 📅 Lançamento: 22 de março de 2022
- 🔧 Tipo: Feature Release
- ⚡ Suporte: Terminado em setembro de 2022
- 🎯 Status: Consistência multiplataforma estabelecida
- 🔄 Migração: Simples - principalmente melhorias internas
🔗 Links Úteis
📚 Documentação Oficial
🌐 UTF-8 Default
🌐 Simple Web Server
📚 Code Snippets
🚀 Vector API
💻 Exemplos e Práticas
Arquitetura, código e aprendizado contínuo.