BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String valueOf() method explained with examples

By Chaitanya Singh | Filed Under: java

Java String valueOf() method 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 arguments.

Different variants of java string valueOf() method

public static String valueOf(boolean b): Used for converting boolean value to a String
public static String valueOf(char c): char to String
public static String valueOf(int i): int to String
public static String valueOf(long l): long to String
public static String valueOf(float f): float to String
public static String valueOf(double d): double to String

Java String valueOf() simple example

Lets take a simple example to understand the usage of this method. In this example we are concatenating the double nines to the end of the given value. The given value is an integer, in order to append 99 at the end of the integer we must need to convert the given integer to the string first. We are using valueOf() method to convert the number to the equivalent string str and then we are concatenating the 99 at the end of converted string.

public class JavaExample{  
   public static void main(String args[]){  
	int number = 23;  
	String str = String.valueOf(number);  
	System.out.println(str+99); 
   }
}

Output:
java string valueof method example

Method valueOf() example 2

In this example, we are converting an array to a string using valueOf() method.

public class JavaExample{  
   public static void main(String args[]){  
	char vowel[] = {'A', 'E', 'I', 'O', 'U'}; 
	String str = String.valueOf(vowel);  
	System.out.println(str); 
   }
}

Output:
String valueOf() method in Java Example

Java String valueOf() Example

Lets take an example, where we are using all the variants of valueOf() method. In this example we are using valueOf() method to convert the integer, float, long, double, char and char array to the String.

public class Example{  
   public static void main(String args[]){  
	int i = 10; //int value
	float f = 10.10f; //float value
	long l = 111L; //long value
	double d = 2222.22; //double value
	char ch = 'A'; //char value
	char array[] = {'a', 'b', 'c'}; //char array
		
	//converting int to String
	String str1 = String.valueOf(i); 
		
	//converting float to String
	String str2 = String.valueOf(f); 
		
	//converting long to String
	String str3 = String.valueOf(l);
		
	//converting double to String
	String str4 = String.valueOf(d);
		
	//converting char to String
	String str5 = String.valueOf(ch);
		
	//converting char array to String
	String str6 = String.valueOf(array);
	System.out.println(str1);
	System.out.println(str2);
	System.out.println(str3);
	System.out.println(str4);
	System.out.println(str5);
	System.out.println(str6);
   }
}

Output:

10
10.1
111
2222.22
A
abc

Reference: String valueOf() method – JavaDoc

❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap