Java .valueOf()
Java’s .valueOf() method of the String class converts various data types like integers, floats, booleans, and objects into their string representations. This helps with string manipulation, logging, and displaying data in a readable format.
Syntax of Java .valueOf()
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(char[] data)
public static String valueOf(char[] data, int offset, int count)
public static String valueOf(Object obj)
Parameters:
i: The integer value to be converted to a stringl: The long value to be converted to a stringf: The float value to be converted to a stringd: The double value to be converted to a stringb: The boolean value to be converted to a stringc: The char value to be converted to a stringdata: The character array to be converted to a stringoffset: The starting index in the character arraycount: The number of characters to include from the arrayobj: The object to be converted to a string
Return value:
The method returns a string that represents the input data. If the input is null, it returns the text “null”.
Example 1: Basic Integer Conversion Using .valueOf() in Java
This example demonstrates the basic usage of .valueOf() to convert an integer to a string representation:
public class ValueOfExample {public static void main(String[] args) {// Declare an integerint number = 42;// Convert integer to string using valueOf()String result = String.valueOf(number);// Display the resultSystem.out.println("Integer value: " + number);System.out.println("String representation: " + result);System.out.println("Data type: " + result.getClass().getSimpleName());}}
The output produced by this code is:
Integer value: 42String representation: 42Data type: String
The code converts the integer 42 to its string representation "42". This is useful when there is a need to display numeric values in user interfaces or concatenate them with other strings.
Example 2: Using .valueOf() for User Input Processing
This example shows how .valueOf() can be used in a real-world scenario where user input needs to be processed and formatted for display:
public class UserDataProcessor {public static void main(String[] args) {// Simulate user data from a formString userName = "Alice";int userAge = 25;double userSalary = 75000.50;boolean isActive = true;// Convert all data to strings for report generationString ageStr = String.valueOf(userAge);String salaryStr = String.valueOf(userSalary);String statusStr = String.valueOf(isActive);// Create formatted user reportString userReport = "User Profile:\n" +"Name: " + userName + "\n" +"Age: " + ageStr + " years\n" +"Salary: $" + salaryStr + "\n" +"Status: " + statusStr;System.out.println(userReport);}}
The output produced by this code is:
User Profile:Name: AliceAge: 25 yearsSalary: $75000.5Status: true
This example demonstrates converting multiple data types to strings for creating a formatted user report, which is common in business applications.
Example 3: Database Query Builder using .valueOf()
This example illustrates using .valueOf() in a practical scenario where dynamic SQL queries are constructed using different data types:
public class QueryBuilder {public static void main(String[] args) {// Database query parametersint userId = 1001;double minSalary = 50000.0;boolean isActive = true;char department = 'A';// Build dynamic SQL query using valueOf()String query = "SELECT * FROM employees WHERE " +"user_id = " + String.valueOf(userId) + " AND " +"salary >= " + String.valueOf(minSalary) + " AND " +"active = " + String.valueOf(isActive) + " AND " +"department = '" + String.valueOf(department) + "'";System.out.println("Generated SQL Query:");System.out.println(query);// Demonstrate null handlingObject nullValue = null;String nullResult = String.valueOf(nullValue);System.out.println("\nNull value conversion: " + nullResult);}}
The output generated by this code is:
Generated SQL Query:SELECT * FROM employees WHERE user_id = 1001 AND salary >= 50000.0 AND active = true AND department = 'A'Null value conversion: null
This example shows how .valueOf() can be used to build dynamic database queries by converting various parameter types to strings, including proper null handling.
Frequently Asked Questions
1. What is the difference between .toString() and .valueOf() in Java?
The main difference is that .toString() is an instance method that can be called on objects, while .valueOf() is a static method of the String class. Additionally, .valueOf() can handle null values safely by returning “null”, whereas calling .toString() on a null object throws a NullPointerException.
2. Can .valueOf() handle null values?
Yes, String.valueOf() can safely handle null values. When you pass a null object to .valueOf(), it returns the string “null” instead of throwing an exception.
3. Can .valueOf() be used with arrays?
Yes, .valueOf() has overloaded methods for character arrays. For char[], it converts the entire array to a string. For other array types passed as Object, it calls the array’s toString() method, which typically returns the array’s memory address.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours