将 Long 转换为 String 的Java程序
当我们必须在 GUI 应用程序中显示长数字时,通常需要将 long 类型转换为字符串类型,因为所有内容都以字符串形式显示。
给定一个 Long 数字,任务是在Java中将其转换为 String。
例子:
Input:Long = 20L
Output:"20"
Input:Long = 999999999999L
Output:"999999999999"
A. 使用 +运算符
要将任何数据类型转换为字符串类型,我们可以简单地添加/+ 由双引号(“”)指示的空字符串。
句法:
String str = l+" ";
Java
// Java program to convert Long to String
// using + operator
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = varLong+" ";
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "+str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Java
// Java program to convert Long to String
// using valueOf() Method
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = String.valueOf(varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Java
// Java program to convert Long to String
// using toString() method
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = Long.toString(varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Java
// Java program to convert Long to String
// using long constructer
import java.util.*;
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = new Long(varLong).toString();
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Java
// Java program to demonstrate
// working of format() method
class Gfg {
public static void main(String args[])
{
long varLong = 9999999L;
String str = String.format("%d", varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Java
// Java program to convert long to
// string using StringBuilder
class Gfg {
public static void main(String args[])
{
long varLong = 9999999L;
String str = new StringBuilder().append(varLong).toString();
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Converted type : java.lang.String
999999999999
B. 使用 String.valueOf()
valueOf()方法将数据从其内部形式转换为人类可读的形式。它是一个静态方法,在所有 Java 内置类型的字符串中重载,以便每种类型都可以正确转换为字符串。
当需要某种其他类型数据的字符串表示时调用它 - 例如在连接操作期间。您可以使用任何数据类型调用此方法并获得合理的字符串表示。
句法:
String str = String.valueOf(varLong);
Java
// Java program to convert Long to String
// using valueOf() Method
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = String.valueOf(varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Converted type : java.lang.String
999999999999
C. 使用 Long.toString()
对象类包含 toString() 方法。我们可以使用 toString() 方法来获取对象的字符串表示。每当我们尝试打印对象引用时,就会在内部调用 toString() 方法。如果我们没有在你的类中定义 toString() 方法,那么对象类的 toString() 方法将被调用,否则我们实现/覆盖的 toString() 方法将被调用。
句法:
String str = Long.toString(varLong);
Java
// Java program to convert Long to String
// using toString() method
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = Long.toString(varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Converted type : java.lang.String
999999999999
D.新龙(长升)
这个构造函数在Java 9 中不是 vald。
句法:
String str = new Long(varLong).toString();
Java
// Java program to convert Long to String
// using long constructer
import java.util.*;
public class GFG {
// main method
public static void main(String args[])
{
// create a Long
Long varLong = 999999999999L;
// convert into String
String str = new Long(varLong).toString();
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
由于这个版本的Java不支持这个构造函数。因此,在运行程序时,我们将得到错误显示
Note: prog.java uses or overrides a deprecated API.
E. 使用 String.format()
Java 字符串 format()方法使用给定的语言环境、指定的格式字符串和参数返回一个格式化的字符串。
句法:
long varLong = 9999999L;
String str = String.format("%d", varLong);
Java
// Java program to demonstrate
// working of format() method
class Gfg {
public static void main(String args[])
{
long varLong = 9999999L;
String str = String.format("%d", varLong);
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Converted type : java.lang.String
9999999
F. 使用 StringBuilder、StringBuffer 对象
StringBuffer是String的对等类,它提供了字符串的大部分功能。字符串表示固定长度的、不可变的字符序列,而 StringBuffer 表示可增长和可写的字符序列。
句法:
long varLong = 9999999L;
String str = new StringBuilder().append(varLong).toString();
Java
// Java program to convert long to
// string using StringBuilder
class Gfg {
public static void main(String args[])
{
long varLong = 9999999L;
String str = new StringBuilder().append(varLong).toString();
// printing the type of str to
// show that long has been converted to string
System.out.println("Converted type : "
+ str.getClass().getName());
// print Long as String
System.out.println(str);
}
}
Converted type : java.lang.String
9999999