Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we’ll discuss the String.valueOf() and Object.toString() methods. The String.valueOf() and Object.toString() methods both allow us to change data types into strings, but we use them differently. We’ll explore how each one is used and their differences.

2. The toString() Method

The toString() method is found in the Java Object class, the parent of all other objects in Java. That means we can call the toString() method on any object, and it will return a string representation of the class. By default, it will return the name of the class and a representation of the hashCode, but by overriding the toString() method, we can get some useful information.

It is recommended that classes override the toString() method to provide some concise but useful information about the instance. Here we have a simple class that overrides the toString() method like this:

public class Student {

    public String name;
    public int age;

    @Override
    public String toString() {
        return "Student(" + name + ", age " + age + ')';
    }
}

3. The String.valueOf() Method

String.valueOf() is a static method that we can use to turn various data types into strings. Like most valueOf() methods, it has several overloaded variants to allow it to accept any of these arguments:

  • bool or Boolean
  • Char
  • Char array
  • double or Double
  • float or Float
  • int or Integer
  • long or Long
  • Object

The String.valueOf() implementations do what we would expect. It will return the “true” or “false” string for booleans. We can convert a char or char array into a string. For numbers, it will return a string representation of that number.

We can test the result of String.valueOf() like this:

@Test
void whenCallingValueOf_thenMapToString() {
    char[] exampleCharArray = {'a', 'b', 'c'};
    Student alice = new Student("Alice", 5);

    assertEquals("true", String.valueOf(true));
    assertEquals("a", String.valueOf('a'));
    assertEquals("abc", String.valueOf(exampleCharArray));
    assertEquals("123.935", String.valueOf(123.935));
    assertEquals("2222.3", String.valueOf(2222.3f));
    assertEquals("2222", String.valueOf(2222));
    assertEquals("123456789", String.valueOf(123456789L));
    assertEquals("123456789", String.valueOf(123456789L));
    assertEquals("Student(Alice, age 5)", String.valueOf(alice));
}

Notice that when we passed our Student to object to valueOf(), we get the result of the toString() method we defined above.

One interesting case is if we pass null to valueOf(), it will return the string “null” instead of throwing a null pointer exception.

4. Which One Should We Use?

The String.valueOf() and Object.toString() methods have different use cases, so we should use both. When we create a new class, we should override the toString() method to output useful information about the instance.

When we pass an object to String.valueOf(), it will call the toString() method on that object and return the output. The toString() method allows us to customize the output when we want to convert an object to a string. The String.valueOf() allows us to safely convert objects to strings without needing to manage null values.

When we need to convert an instance to a string, we should use the String.valueOf() method to ensure null safety.

5. Conclusion

In this article, we’ve seen how the String.valueOf() and Object.toString() work together and how we can use both to convert data types into strings.

String.valueOf() and Object.toString() provide similar results, but we use them differently. The static String,valueOf(), allows us to pass all sorts of data and return a string with null safety. The Object.toString() is for us to override and provide a text representation of the instance.

All code examples can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.