image

Acesse bootcamps ilimitados e +650 cursos

50
%OFF
Article image

CA

Claudio Andrade25/06/2024 06:06
Compartilhe

Comparativo entre String, StringBuffer e StringBuilder

  • #Java

Artigo em PDF com código fonte em:

https://github.com/claudio-es-andrade/Artigos/tree/main/StringStringBuilderStringBuffer

Comparativo entre String, StringBuffer e StringBuilder:

Característica: String StringBuilder StringBuffer

Lançamento: String = JDK 1.0 StringBuffer JDK 1.5 StringBuilder = JDK 1.0

Mutabilidade: String = ImutávelStringBuffer = Mutável StringBuilder = Mutável

Thread Safety

(Segurança da Trilha): String = Sim StringBuffer = Não StringBuilder =Sim

Eficiência de memória:

String = Alta; StringBuffer = Suficientemente Eficiente; StringBuilder = Menos Eficiente

Performance:

String = Alta Sem Sincronização StringBuffer = Alta Sem Sincronização StringBuilder = Baixa - Com Sincronização

Uso:

String = Quando deseja imutabilidade (não usar a memória heap e sim a “string pool”)

StringBuilder = Quando Thread Safety não é requirida

StringBuffer Quando Thread Safety é requirida

Códigos utilizados neste exemplo:

StringBuffer:

StringBuffer a1 = new StringBuffer("ABC");

StringBuffer b2 = new StringBuffer("ABC");

if (a1 == b2)

System.out.println("As STRINGSBUFFERS são iguais (COMPARAÇÃO ==)");

else

System.out.println("As STRINGSBUFFERS são diferentes (COMPARAÇÃO ==)");

System.out.println("As STRINGSBUFFER são iguais? (Método EQUALS):" + a1.equals(b2));

Resultado:

As STRINGSBUFFERS são diferentes (COMPARAÇÃO ==)

As STRINGSBUFFER são iguais? (Método EQUALS):false

Strings:

String c1 = "ABC";

String d2 = "ABC";

if (c1 == d2)

System.out.println("As STRINGS são iguais (COMPARAÇÃO ==)");

else

System.out.println("As STRINGS são diferentes (COMPARAÇÃO ==)");

System.out.println("As STRINGS são iguais? (Método EQUALS):" + c1.equals(d2));

String c01 = new String("ABC");

String d02 = new String("ABC");

if (c01 == d02)

System.out.println("As STRINGS são iguais (COMPARAÇÃO ==)");

else

System.out.println("As STRINGS são diferentes (COMPARAÇÃO ==)");

System.out.println("As STRINGS são iguais? (Método EQUALS):" + c01.equals(d02));

String c001 = "ABC";

String d002 = new String("ABC");

if (c001 == d002)

System.out.println("As STRINGS são iguais (COMPARAÇÃO ==)");

else

System.out.println("As STRINGS são diferentes (COMPARAÇÃO ==)");

System.out.println("As STRINGS são iguais? (Método EQUALS):" + c001.equals(d002));

Resultado:

Primeiro Bloco de String: apenas declarando entre parênteses (string pool):

As STRINGS são iguais (COMPARAÇÃO ==)

As STRINGS são iguais? (Método EQUALS):true

Segundo Bloco de String: declaração com a palavra new nas duas variáveis:

As STRINGS são diferentes (COMPARAÇÃO ==)

As STRINGS são iguais? (Método EQUALS):true

Terceiro Bloco de String: uma variavel declarando entre parênteses e outra com a declaração NEW:

As STRINGS são diferentes (COMPARAÇÃO ==)

As STRINGS são iguais? (Método EQUALS):true

image

StringBuilder:

StringBuilder e1 = new StringBuilder("ABC");

StringBuilder f2 = new StringBuilder("ABC");

if (e1 == f2)

System.out.println("As STRINGSBUILDER são iguais (COMPARAÇÃO ==)");

else

System.out.println("As STRINGSBUILDER são diferentes (COMPARAÇÃO ==)");

System.out.println("As STRINGSBUILDER são iguais? (Método EQUALS):" + e1.equals(f2));

Resultado:

As STRINGSBUILDER são diferentes (COMPARAÇÃO ==)

As STRINGSBUILDER são iguais? (Método EQUALS):false

Compartilhe
Comentários (0)