DEV Community

Wellington Domiciano
Wellington Domiciano

Posted on • Edited on • Originally published at wldomiciano.com

3 2

O método toString do Java

Criou uma classe maneira e na hora de mostrar o resultado com print() viu uns números estranhos?

class User {
  private String name;
  private int age;

  User(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

public class Main {
  public static void main(String... args) {
    User user = new User("João", 29);

    System.out.println(user); // Apenas um ex.: User@1a2b3c
  }
}
Enter fullscreen mode Exit fullscreen mode

Não se preocupe, a sua classe está apenas usando o método toString() implementado na classe Object.

Nesta implementação é usado o nome da classe seguido pelo símbolo @ e finalizando com o hashcode do objeto (mas em hexadecimal).

Dá para atestar isso com o código abaixo.

class User {
  private String name;
  private int age;

  User(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

public class Main {
  public static void main(String... args) {
    User user = new User("João", 29);

    String className = user.getClass().getName();
    int hashCode = user.hashCode();
    String myToString = className + '@' + Integer.toHexString(hashCode);

    System.out.println(user.toString().equals(myToString)); // true
  }
}
Enter fullscreen mode Exit fullscreen mode

Ou olhando a documentação.

Ou olhando o código fonte.

Porém, somos livres para sobrescrever este método e deixar do jeito que quisermos.

class User {
  private String name;
  private int age;

  User(String name, int age) {
    this.name = name;
    this.age = age;
  }

  @Override
  public String toString() {
    return "User[name=" + name + ", age=" + age + "]";
  }
}
Enter fullscreen mode Exit fullscreen mode

O jeito mostrado no exemplo acima é parecido com o que algumas IDEs geram automaticamente. Mas, na minha opinião, há formas mais elegantes de obter o mesmo resultado, veja:

@Override
public String toString() {
  return String.format("User[name=%s, age=%d]", name, age);
}
Enter fullscreen mode Exit fullscreen mode

Ou, com Java 17 ou mais recente:

@Override
public String toString() {
  return "User[name=%s, age=%d]".formatted(name, age);
}
Enter fullscreen mode Exit fullscreen mode

O que achou? Não ficou muito mais fácil de ler?

Arrays também usam a implementação padrão e uma das opções mais simples que temos para mostrar o seu conteúdo é com o método Arrays.toString(), veja:

import java.util.Arrays;

public class Main {
  public static void main(String... args) {
    int[] numbers = { 1, 2, 3 };

    System.out.println(Arrays.toString(numbers)); // [1, 2, 3]
  }
}
Enter fullscreen mode Exit fullscreen mode

Se for um array de arrays, toString() não é o bastante. Precisamos do deepToString():

import java.util.Arrays;

public class Main {
  public static void main(String... args) {
    int[][] numbers = {
      { 1, 2, 3 },
      { 4, 5, 6 },
      { 7, 8, 9 }
    };

    System.out.println(Arrays.deepToString(numbers)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  }
}
Enter fullscreen mode Exit fullscreen mode

Agora estamos prontos para deixarmos os nossos objetos mais apresentáveis!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay