×

Java String valueOf() method

Java String valueOf() method converts different types of values into String.

Such as : int to String, long to String, boolean to String, character to String, float to String, double to String, object to String and char array to String.

Syntax:

public static String valueOf(boolean b) 
public static String valueOf(char c) 
public static String valueOf(char[] c) 
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(Object o)

Returns:

It returns String, representation of given value.

Java String valueOf(char c) method example

public class JavaStringValueOfEx1
{
      public static void main(String[] args)
 {
            // declare a sample string value
            String strValue = "=ROY|N.K.=";
            // convert strValue to character array
            char[] values = strValue.toCharArray();
            // for each element on char array
            for(char c:values){
                  // print the String value of char c
                  System.out.println(String.valueOf(c));
            }
      }
}

Output:

=
R
O
Y
|
N
.
K
.
=

Java String valueOf() method example 2

public class JavaStringValueOfEx2
{ 
    public static void main(String args[])
    { 
        int value=10; 
        String s1=String.valueOf(value); 
        System.out.println(s1+10);//concatenating string with 10 
    }
  }

Output:

1010

Java String valueOf(boolean bol) method example 3

public class JavaStringValueOfEx3
{ 
    public static void main(String[] args)
    {         
        // Boolean to String 
        boolean bol = true;   
        boolean bol2 = false;   
        String s1 = String.valueOf(bol);   
        String s2 = String.valueOf(bol2); 
        System.out.println(s1); 
        System.out.println(s2); 
    } 
}

Output:

true
false

Java String valueOf(float f) and valueOf(double d) Example 4

public class JavaStringValueOfEx4 { 
    public static void main(String[] args) { 
        // Float and Double to String 
        float f  = 15.45f;   
        double d = 15.32; 
        String s1 = String.valueOf(f);   
        String s2 = String.valueOf(d); 
        System.out.println(s1); 
        System.out.println(s2); 
    } 
}

Output:

15.45
15.32

Java String valueOf Example 5

public class JavaStringValueOfEx5 { 
    public static void main(String[] args) { 
        boolean b1=true; 
        byte b2=11;   
        short sh = 12; 
        int i = 13; 
        long l = 14L; 
        float f = 15.5f; 
        double d = 16.5d; 
        char chr[]={'t','u','t','o','r','i','a','l','a','n','d','e','x','a','m','p','l','e'};     
        String s1 = String.valueOf(b1);   
        String s2 = String.valueOf(b2);   
        String s3 = String.valueOf(sh);   
        String s4 = String.valueOf(i);   
        String s5 = String.valueOf(l);   
        String s6 = String.valueOf(f);   
        String s7 = String.valueOf(d);   
        String s8 = String.valueOf(chr);         
        System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
        System.out.println(s6); 
        System.out.println(s7); 
        System.out.println(s8);      
    } 
}

Output:

true
11
12
13
14
15.5
16.5
tutorialandexample

Related Topics

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Math random() Method

The random() method of Math class returns a double value with a positive sign, less than 1 and greater than or equal to 0.0. This method is properly synchronized with...

1 minute read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.