Use Java's String.valueOf() function to convert other types into strings
In Java development, you often encounter the need to convert other data types into strings. To meet this need, Java provides the String.valueOf() function to implement type conversion. This article explains how to use the String.valueOf() function to convert other types to strings and provides code examples.
First, let’s look at how to convert basic data types to strings. Basic data types in Java include int, long, float, double, char, boolean, etc. Values of these basic data types can be converted to strings using the String.valueOf() function.
The sample code is as follows:
int num = 10; String strNum = String.valueOf(num); System.out.println(strNum); // 输出结果为"10" double pi = 3.14159; String strPi = String.valueOf(pi); System.out.println(strPi); // 输出结果为"3.14159" char ch = 'A'; String strCh = String.valueOf(ch); System.out.println(strCh); // 输出结果为"A" boolean flag = true; String strFlag = String.valueOf(flag); System.out.println(strFlag); // 输出结果为"true"
In addition to basic data types, Java also has reference data types, such as String, Array, List, etc. These reference data types can also be converted to strings using the String.valueOf() function.
The sample code is as follows:
String str = "Hello"; String strValue = String.valueOf(str); System.out.println(strValue); // 输出结果为"Hello" int[] array = {1, 2, 3}; String arrayStr = String.valueOf(array); System.out.println(arrayStr); // 输出结果为"[I@1f32e575",表示数组的内存地址 List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); String listStr = String.valueOf(list); System.out.println(listStr); // 输出结果为"[1, 2, 3]"
It should be noted that when converting an array into a string, the memory address of the array is output. If you need to output an array as a string in a specific format, you can use the toString() method of the Arrays class.
In addition to basic data types and reference data types, we can also convert custom types to strings. It should be noted here that custom types need to override the toString() method to convert correctly.
The sample code is as follows:
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + ''' + ", age=" + age + '}'; } } Person person = new Person("张三", 20); String personStr = String.valueOf(person); System.out.println(personStr); // 输出结果为"Person{name='张三', age=20}"
In the above example, we defined a Person class and overridden the toString() method. Then call the String.valueOf() function to convert the Person object to a string.
Through the above code example, we learned how to use Java's String.valueOf() function to convert other types to strings. Whether it is a basic data type, a reference data type or a custom type, just call this function to complete the conversion. Using the String.valueOf() function can quickly and easily achieve type conversion, which is very popular in actual development.
The above is the detailed content of Convert other types to string using java's String.valueOf() function. For more information, please follow other related articles on the PHP Chinese website!