English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, nous allons apprendre à convertir une variable entière (int) en variable de type long.
Pour comprendre cet exemple, vous devriez comprendre ce qui suitProgrammation JavaThème :
class Main { public static void main(String[] args) { //Create int variable int a = 25; int b = 34; //Conversion de int en long //Utilisation de la conversion de type long c = a; long d = b; System.out.println(c); // 25 System.out.println(d); // 34 {} {}
Dans l'exemple ci-dessus, nous avons les variables de type int a et b. Notez la ligne,
long c = a;
Ici, la variable de type int est automatiquement convertie en type long. C'est parce que le type long est un type de données de plus haut niveau, tandis que le type int est un type de données de plus bas niveau.
Therefore, data truncation will not occur, and it is called from conversion int to long.Widening Conversion. For more information, please visitJava Type Conversion.
We can convert an int type variable to an object of the Long class. For example,
class Main { public static void main(String[] args) { //Create int variable int a = 251; //Convert to Long object //Using valueOf() Long obj = Long.valueOf(a); System.out.println(obj); // 251 {} {}
In the above example, we used Long.valueOf() to convert the variable a to an object of the Long class.
Here, Long is a wrapper class in Java. For more information, please visitJava Wrapper class.