DEV Community

Java para Iniciantes (Oracle)
Java para Iniciantes (Oracle)

Posted on

Strings

Strings

  • A classe String é um dos tipos de dados mais importantes em Java.

  • Diferente de outras linguagens, em Java, strings são objetos, não arrays de caracteres.

  • Ao criar um literal de string, você está criando um objeto da classe String.

  • Exemplo: em System.out.println("In Java, strings are objects.");, a string é convertida automaticamente em um objeto String.

  • O uso de String já estava presente desde o início, mesmo que implicitamente.

  • A classe String é extensa e o texto a examina de forma superficial.

  • Explorar a classe String por conta própria é recomendado.

Construindo strings

  • Um objeto String pode ser criado usando new e chamando o construtor de String.

  • Exemplo:

  • String str = new String("Hello");

  • cria um objeto String com o valor "Hello".

  • Um String também pode ser criado a partir de outro String.

  • Exemplo:

  • String str2 = new String(str);

  • cria uma cópia do String original.

  • Outra forma de criar um String é atribuindo diretamente uma sequência de caracteres.

  • Exemplo:

  • String str = "Java strings are powerful.";

  • Objetos String podem ser usados onde quer que strings entre aspas sejam permitidos.
    Image description

  • Saída:
    Java strings are objects.
    They are constructed various ways.
    They are constructed various ways.

Operando com strings

  • Exemplos de métodos que operam com strings: Image description

Image description

  • Saída:
    Length of str1: 45
    When it comes to Web programming, Java is #1.
    str1 equals str2
    str1 does not equal str3
    str1 is greater than str3
    Index of first occurrence of One: 0
    Index of last occurrence of One: 14

  • Para concatenar (unir) dois strings usando o operador + :
    String str1 = "One";
    String str2 = "Two";
    String str3 = "Three";
    String str4 = str1 + str2 + str3;

  • Inicializa str4 com o string “OneTwoThree”.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay