Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

String valueOf() in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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() Function

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

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)

 

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

Parameter

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

Return Value

String valueOf()  method returns the string representation.

Examples

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation

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);
   }
}

 

Output:

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

Check out this problem - Subarray With 0 Sum

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.

What is String in Java?

The String is a Java class found in the Java.lang package. It isn't a primary data type like int or long. A string is a sequence of characters. Strings in Java are immutable, and the JVM stores all String objects in a String Pool.

What is the return type of String valueOf()  in Java?

String valueOf() method returns the string representation.

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.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.
 

Recommended problems -

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Topics covered
1.
Introduction
2.
String valueOf() Function
2.1.
Syntax
2.2.
Parameter
2.3.
Return Value
2.4.
Examples
3.
Implementation
4.
Frequently Asked Questions
4.1.
What is the purpose of the String valueOf() function in Java?
4.2.
What is String in Java?
4.3.
What is the return type of String valueOf()  in Java?
5.
Conclusion