convert whole to java string

Asked

Viewed 889 times

2

How do I convert the Integer to String type in Java?

The variable matricula is declared as whole, but to show it on the screen it asks to convert it to String.

txtmatricula.setText(txtmat.getMatricula()));
  • 1

    Integer.toString(txtmat.getMatricula());

  • @Augustovasques thank you

  • txtmat.getMatricula() + "";

1 answer

3

Use String.valueOf()

String matriculaString = String.valueOf(txtmat.getMatricula())
txtmatricula.setText(matriculaString);

Perks

  • Also for the types: Double, Float or Long;
  • It has the purpose of being used in conversions, so many classes have this method, such as: String, Integer, Double, Bigdecimal, etc.
  • In the case of Integer, the valueOf() cacheia numbers frenquently used from -127 to 128;
  • Delegate the conversion of String to the respective class method, in the case of Integer, delegate to Integer.toString(), which leaves the number representation in String, consistent;

Source in English: https://www.java67.com/2012/10/best-way-to-convert-numbers-to-string-in-java-example.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.