How to convert an integer value to string?

前端 未结 5 1736
遥遥无期
遥遥无期 2021-02-14 06:26

How do I convert an integer variable to a string variable in Java?

5条回答
  •  梦毁少年i
    2021-02-14 07:11

    There are at least three ways to do it. Two have already been pointed out:

    String s = String.valueOf(i);
    
    String s = Integer.toString(i);
    

    Another more concise way is:

    String s = "" + i;
    

    See it working online: ideone

    This is particularly useful if the reason you are converting the integer to a string is in order to concatenate it to another string, as it means you can omit the explicit conversion:

    System.out.println("The value of i is: " + i);
    

提交回复
热议问题