Table of contents
1.
Introduction
2.
What is String valueOf() Function in Java?
3.
Syntax of String valueOf() in Java
4.
Parameter valueOf() in Java
5.
Return Value of String valueOf() in Java
6.
Exceptions of Java valueOf()
7.
Implementation of Java String valueOf() Function
8.
Examples of String valueOf() in Java
9.
Example 1: Java String valueOf() for Numbers
10.
Example 2: Convert Char Array to String
11.
Example 3: Convert Char Subarray to String
12.
Example 4: Convert List Object to String
13.
Frequently Asked Questions
13.1.
What is the purpose of the String valueOf() function in Java?
13.2.
How does get() work in Java?
13.3.
What is Integer.valueOf() in Java?
13.4.
What is the difference between toString() and valueOf() in Java?
14.
Conclusion
Last Updated: Nov 22, 2024
Easy

String valueOf() in Java

Author ANJU JAISWAL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When it comes to programming, strings are the most basic data structures. A character list, or more precisely, an "array of characters," is what they're made up of. With the help of an example, we will learn how to use the function String valueOf() in this article.

String valueOf() in Java

What is String valueOf() Function in Java?

In Java, the String valueOf() method is a built-in function that returns the string representation of the boolean, char, char array, int, long, float, and double arguments. We have different versions of this method for each type of argument

Syntax of String valueOf() in Java

For various data types, the String valueOf() function has the following syntax:

String.valueOf(char c)
String.valueOf(long l)
String.valueOf(double d)
String.valueOf(float f)
String.valueOf(int b)
String.valueOf(boolean b)
String.valueOf(Object o)
String.valueOf(char[] data)
You can also try this code with Online Java Compiler
Run Code

 

Function valueOf() is a static method in this case. The valueOf() method is called using the class name as follows: String.valueOf(b);

Parameter valueOf() in Java

The function valueOf() method only accepts one argument: the data to be converted to a string.

Return Value of String valueOf() in Java

String valueOf()  method returns the string representation.

Exceptions of Java valueOf()

Here are the exceptions thrown by Java's valueOf() method in pointer format:

  • NullPointerException:

This exception is thrown if the argument passed to valueOf() is null and the method is called for an object type like Integer.valueOf(null).

 

  • NumberFormatException:

This exception is thrown when converting a string to a numeric type, and the string is not a valid representation of the number. For example, Integer.valueOf("abc") will throw this exception.
 

  • IllegalArgumentException:

In some specific implementations like enums, valueOf() can throw IllegalArgumentException if the provided name does not match any declared enum constant.


These exceptions should be handled to prevent runtime errors during the execution of the program.

Implementation of Java String valueOf() Function

import java.util.*;
import java.io.*;
import java.lang.*;

class CodingNinjas {
   public static void main(String[] args) throws java.lang.Exception {
       char ch = 'C'; //char value
       long l = 242 L; //long value
       double d = 4242.24; //double value
       float f = 24.24 f; //float value
       int i = 24; //int value

       char array[] = { 'c', 'o', 'd', 'i', 'n', 'g' }; //char array
       
       //converting char to String
       String str1 = String.valueOf(ch);
       //converting long to String
       String str2 = String.valueOf(l);
       //converting double to String
       String str3 = String.valueOf(d);
       //converting float to String
       String str4 = String.valueOf(f);
       //converting int to String
       String str5 = String.valueOf(i);

       //converting char array to String
       String str6 = String.valueOf(array);
       //Subarray of a char Array to String
       char cha[] = { 'p', 'r', 'o', 'g', 'r', 'a', 'm' };
       int offset = 2;
       int length = 4;
       String str7;
       // subarray {'o', 'g', 'r', 'm'} is converted to string
       str7 = String.valueOf(cha, offset, length);
       //Convert Object to String
       ArrayList < String > languages = new ArrayList < String > ();
       languages.add("Cpp");
       languages.add("Kotlin");
       languages.add("Python");
       String str8;
       // Output: "[cpp, Kotlin, Python]"
       str8 = String.valueOf(languages);

       System.out.println(str1);
       System.out.println(str2);
       System.out.println(str3);
       System.out.println(str4);
       System.out.println(str5);
       System.out.println(str6);
       System.out.println(str7); // "ogrm";
       System.out.println(str8);
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

C
242
4242.24
24.24
24
coding
ogra
[Cpp, Kotlin, Python]

Check out this problem - Subarray With 0 Sum

Examples of String valueOf() in Java

Example 1: Java String valueOf() for Numbers

In this example, the valueOf() method converts the integer 100 into a string. The result, "100", is then printed. This method is useful when you need to represent numbers as strings in your program.

public class StringValueOfExample {
   public static void main(String[] args) {
       int num = 100;
       String str = String.valueOf(num);
       System.out.println("String value: " + str);
   }
}
You can also try this code with Online Java Compiler
Run Code


Output:

String value: 100

Example 2: Convert Char Array to String

Here, the valueOf() method takes a character array {'J', 'a', 'v', 'a'} and converts it into a string "Java". This method is helpful when you need to convert an array of characters to a single string.

public class CharArrayToStringExample {
   public static void main(String[] args) {
       char[] charArray = { 'J', 'a', 'v', 'a' };
       String str = String.valueOf(charArray);
       System.out.println("String from char array: " + str);
   }
}
You can also try this code with Online Java Compiler
Run Code


Output

String from char array: Java

Example 3: Convert Char Subarray to String

In this example, the valueOf() method is used to convert a subarray of characters into a string. It starts from index 6 of the character array and takes 4 characters to form the string "Java".

public class CharSubArrayToStringExample {
   public static void main(String[] args) {
       char[] charArray = { 'H', 'e', 'l', 'l', 'o', ' ', 'J', 'a', 'v', 'a' };
       String str = String.valueOf(charArray, 6, 4);
       System.out.println("String from char subarray: " + str);
   }
}
You can also try this code with Online Java Compiler
Run Code


Output:

String from char subarray: Java

Example 4: Convert List Object to String

In this case, the valueOf() method converts a list object into a string representation. The list [Hello, World, Java] is displayed as a string, which shows all the elements enclosed within square brackets.

import java.util.Arrays;
import java.util.List;
public class ListToStringExample {
   public static void main(String[] args) {
       List<String> list = Arrays.asList("Hello", "World", "Java");
       String str = String.valueOf(list);
       System.out.println("String from list: " + str);
   }
}
You can also try this code with Online Java Compiler
Run Code


Output:

String from list: [Hello, World, Java]

Frequently Asked Questions

What is the purpose of the String valueOf() function in Java?

You can convert  long to String, int to String, double to String, character to string, float to String, boolean to string, object to String, and char array to string using the String valueOf()  method.

How does get() work in Java?

The get() method in Java is used to retrieve an element from data structures like ArrayList, HashMap, or Optional. For example, ArrayList.get(index) returns the element at the specified position in the list.

What is Integer.valueOf() in Java?

Integer.valueOf() converts a string or primitive int to an Integer object. It caches values between -128 and 127 to improve performance by reusing commonly requested objects instead of creating new ones each time.

What is the difference between toString() and valueOf() in Java?

toString() converts an object into its string representation. In contrast, valueOf() can convert primitive data types and objects into a string. Additionally, valueOf() is null-safe, while toString() will throw a NullPointerException if the object is null.

Conclusion

This article taught us about String valueOf() in Java and its implementation. The Java String valueOf() method returns the String representation of the boolean, char, char array, int, long, float, and double arguments. We have also seen some examples to understand better how this method works.

Recommended problems -

Live masterclass