Contents

Convert to String (with.Java)

   Nov 15, 2023     0 min read

This is a post about the ā€œConvert to Stringā€ problem.

Weā€™re going to learn about it by solving coding test problems, reflecting on the problems we solved, and exploring other ways to solve them.

Letā€™s start with the problem.

Problem

Complete the solution function so that, given an integer n, it converts n to a string and returns it.

Example input and output
nresult
123ā€œ123ā€
2573ā€œ2573ā€

My solution to the problem

class Solution {
    public String solution(int n) {
        String answer = String.valueOf(n);
        return answer;
    }
}
Explanation of the solution

We used the String.valueOf function, which converts an integer datatype to a string datatype, and returned the value in answer.