×

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

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 LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.