×

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

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.