Convertir un entier en chaîne de caractères | Java

Dans ce tutoriel nous allons découvrir différents façons pour convertir un entier en une chaîne de caractères en Java.

  • String.valueOf
  • toString()

 
 

Exemple 1: String.valueOf

Exemple pour convertir Integer en String en utilisant la méthode String.valueOf().

public class Main {
    public static void main(String[] args) {
		Integer n = 25;
		String str = String.valueOf(n);
		System.out.println(str);
    }
}

 
Sortie:

25

 
Exemple pour convertir int en String en utilisant la méthode String.valueOf().

public class Main {
    public static void main(String[] args) {
		int n = 25;
		String str = String.valueOf(n);
		System.out.println(str);
    }
}

 
Sortie:

25

 
 

Exemple 2 : toString()

Exemple pour convertir Integer en String en utilisant la méthode toString().

public class Main {
    public static void main(String[] args) {
		Integer n = 25;
		String str = n.toString();
		System.out.println(str);
    }
}

 
Sortie:

25

 
Exemple pour convertir int en String en utilisant la méthode toString().

public class Main {
    public static void main(String[] args) {
		int n = 25;
		String str = Integer.toString(n);
		System.out.println(str);
    }
}

 
Sortie:

25

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *