java字符串和数字怎么拼接

2024-04-22

在Java中,可以使用加号(+)来连接字符串和数字。例如:

int num = 10;
String str = "The number is " + num;
System.out.println(str);

这将输出:

The number is 10

如果要将数字转换为字符串再拼接,可以使用String.valueOf()方法或者Integer.toString()方法。例如:

int num = 10;
String str = "The number is " + String.valueOf(num);
System.out.println(str);

或者:

int num = 10;
String str = "The number is " + Integer.toString(num);
System.out.println(str);